程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python learning ---day2

編輯:Python

Operator

Comparison operator

>, <, ==( And so on ), !=( It's not equal to ), >=, <=
notes :python The comparison size in is the comparison size between data of the same type
-------> Comparing sizes yields Boolean values

print(3 > 4)

expand — ask : Why? python Ability to compare sizes ?
python The code value used is called the universal code :unicode, It is ASCII Expansion of coding

  • Numbers 0-9 Corresponding decimal coded value :48-57

  • A-Z Corresponding decimal code :65-90

  • a-z Corresponding coding value :97-122

print('A' < 'a')print(0 != 1)print(0 == 1)

>= 、 <=

print(2 >= 2)

Example :
Determine if the year is a leap year :

year = 2004result = (year % 4 ==0 and year % 100 != 0) or (year % 400 == 0)print(result)

Assignment operator

=, += ,-= , =, /=, //=, %=,
Count the right side of the equal sign first , Assign a value to the left of the equal sign

a = 10a += 5# a = a + 5a **= 2# a = a ** 2print(a)a %= 2# a = a % 2print(a)# Compound assignment operator a *= a + 3# a = a*(a+3)print(a)

Operator priority
a. First calculate the parentheses as a whole
b. Arithmetic operator :* * > *,/,//,% > +,-
c. Arithmetic operator > Comparison operator > Logical operators > Assignment operator
practice :
1、 The conversion between Fahrenheit and temperature :C = (F - 32)/1.8

C = 36F = round(C * 1.8 + 32, 2)print(f' Centigrade {C} Equal to degrees Fahrenheit {F}')F = 109C = round((F - 32) / 1.8, 2)print(f' Fahrenheit {C} Equal to centigrade {F}')

Add :
round( Parameters 1, Parameters 2)----> Parameters 1 Equal to the value , Parameter 2: the number of decimal places to be reserved
2、 According to the specified radius of the circle , Calculate the circumference and area of a circle .

import mathr = 3c = round(2 * r * math.pi, 2)s = round(r ** 2 * math.pi, 2)print(f' The circumference of the circle is :{c}')print(f' The area of the circle is :{s}')

Simple data type conversion

----- Numbers 、 Boolean data type conversion

One 、 Boolean conversion

All data can be converted to Boolean values (bool)

# bool()print(bool(0), bool(1), bool(-1))print(bool(''), bool([]), bool({}), bool(None)) # An empty string 、 An empty list 、 An empty dictionary 、 Null value 

notes
a.0 Convert to False, Not 0 Is full of Ture
b. All null objects are False

Two 、 Digital conversion

1. integer (int)

a. Boolean values can be converted to integers , Can only turn 0 and 1:False—0,Ture—1

print(int(bool(100)))

b. floating-point (float) Can be converted to an integer : Round to the small

print(int(0.515646))#---->0print(int(.1))#---->0print(int(1.))#---->1print(int(9.64631653))#---->9

c. character string ( A string that is an integer without quotation marks ) Can be converted to an integer

print(int(input(' Please enter an integer :')))print(int('156486'))

2. floating-point (float)

a. Integer to floating point

print(float(1))

b. Strings that are numbers without quotation marks can be converted to floating-point type

print(float('.9'))#--->0.9

c. Boolean values can be converted to floating point type :False—>0.0;Ture—>1.0

print(float(False))

notes :bool() int() float() list() etc. ----> Constructor Syntax

Branching structure

Branch structure keywords :if、else、elif、

1. Single branch structure :if、else

Single branch structure grammar :
if Conditions :
Code segment 1
else:
Code segment 2
Case study : Account password login

username = input(' Please enter a user name :')password = input(' Please input a password :')if username == 'admin' and password == '123456': print(' Landing successful ')else: print(' Login failed ')

notes : Indent : Generally think of indentation as four spaces , The indentation of a program must be consistent
practice : Judge whether a year is a leap year ?

year = int(input(' Please enter a year :'))if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0): print(f'{year} Year is a leap year ')else: print(f'{year} Year is a normal year ')

2. Multi branch structure elif

Multi branch structure grammar :
if Conditions :
Code segment 1
elif Conditions 2:
Code segment 2

else:
Code segment n
practice : Judge whether a year is a leap year ?

year = int(input(' Please enter a year :'))if year % 4 == 0 and year % 100 != 0: print(f'{year} Year is a leap year ')elif year % 400 == 0: print(f'{year} Year is a leap year ')else: print(f'{year} Year is a normal year ')

3. Ternary operator

example : Judging whether a person is an adult
Common writing :

age = int(input(' Please enter age :'))if age < 18: print(' A minor ')else: print(' adult ')

Ternary operator syntax :
Variable = ‘ result 1’ Conditions else ‘ result 2’
Conditions established , Result is result 1, The opposite is the result 2
Improved writing :

result = ' adult ' if age >= 18 else ' A minor 'print(result)

Today's exercises

practice 1: Enter three side lengths , If you can form a triangle, calculate the perimeter and area .

a = float(input(' Please input the side length a:'))b = float(input(' Please input the side length b:'))c = float(input(' Please input the side length c:'))C = a + b + c # Perimeter s = 0.5*CS = (s*(s-a)*(s-b)*(s-c))**0.5# Helen's formula :( Half the circumference of a triangle )*( Half the circumference of a triangle -a)*( Half the circumference of a triangle -b)*( Half the circumference of a triangle -c)**0.5if a + b > c and a + c > b and b + c > a: print(" Can form triangles ") print(f' The perimeter of the triangle is {round(C,2)}, Area is {round(S,2)}.')# round() Keep the decimal places else: print(' Can't make a triangle ')

practice 2: Conversion of a 100% grade to a graded grade .

requirement : If the score entered is in 90 More than ( contain 90 branch ) Output A;80 branch -90 branch ( Not included 90 branch ) Output B;
70 branch -80 branch ( Not included 80 branch ) Output C;60 branch -70 branch ( Not included 70 branch ) Output D;60 The output is as follows E.

score = int(input(' Please enter the grade to be converted :'))if score >= 90: print(' This grade is :A')elif score >= 80: print(' This grade is :B')elif score >= 70: print(' This grade is :C')elif score >= 60: print(' This grade is :D')else: print(' This grade is :E')

practice 3: English units inches and metric units centimeters are interchangeable .

num = float(input(' Please input the data to be converted :'))unit = input(' Please enter the unit to be converted :')if unit == 'in' or unit == ' feet ': num1 = num * 2.54 print(f'{num} centimeter ={num1} feet ')elif unit == 'cm' or unit == ' centimeter ': num2 = num / 2.54 print(f'{num} feet ={num2} centimeter ')else: print(' Please enter the correct unit ')

author : Have a strong sense of self-management .

Game programming , A game development favorite ~

If the picture is not displayed for a long time , Please use Chrome Kernel browser .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved