Operators and conditional statements
Arithmetic operators
operator |
Description |
Example |
+ |
Add-Two objects added |
A + B output result 30 |
- |
Minus-get negative numbers or one number minus the other |
-B Output Result-10 |
* |
Multiply by two number or return a string that is repeated several times |
A * b output result 200 |
/ |
Except-X divided by Y |
B/A Output Results 2 |
% |
Modulo-Returns the remainder of the division |
B% A output result 0 |
** |
Power-Returns the Y power of X |
A**b is 10 of 20, output 100000000000000000000 |
// |
Divide-Returns the integer part of the quotient |
9//2 output result 4, 9.0//2.0 output 4.0 |
Comparison operators
operator |
Description |
Example |
== |
Equals-compares objects for equality |
(A = = B) returns FALSE. |
!= |
Not equal-compares two objects for unequal |
(A! = B) returns TRUE. |
<> |
Not equal-compares two objects for unequal |
(a <> B) returns True. This operator is similar to! =. |
> |
Greater than-returns whether x is greater than Y |
(A > B) returns FALSE. |
< |
Less-Returns whether x is less than Y. All comparison operators return 1 for true, and return 0 for false. This distinction is equivalent to the special variable true and false. |
(A < B) returns TRUE. |
>= |
Greater than or equal-returns whether X is greater than or equal to Y. |
(a >= B) returns FALSE. |
<= |
Less than or equal-returns whether X is less than or equal to Y. |
(a <= B) returns True. |
1>>> A = 22>>> B = 23>>> A = =b #判断a, b Two objects are equal, and the returned result is true, indicating that the objects referenced by the two variables are equal4 True5>>> A isb6 True7>>>ID (a), id (b) #a and B refer to the same object8(54354784L, 54354784L)9>>> da ={"Lang":["python", c],"Teacher":"cc"}Ten>>>ImportCopy One>>> db =copy.deepcopy (DA) #深拷贝 A>>>DB -{'Lang': ['python', set ([0, 1, 3, 5, 6])],'Teacher':'cc'} ->>>ID (DA), ID (db) #是不同的两个对象 the(54489976L, 64755912L) ->>> da isDB #判断是否为同一个对象 - False ->>> da = =DB #两个对象相等 + True ->>> da! =DB #两个对象不相等 + False A>>> b =2,3 at>>> a!=b - True ->>> a<>b - True ->>> 321>1234 - False in>>>"321">"1234" - True to>>>
logical operators
operator |
Logical Expressions |
Description |
Example |
and |
X and Y |
Boolean "and"-if x is False,x and y returns FALSE, otherwise it returns the computed value of Y. |
(A and B) returns 20. |
Or |
X or Y |
Boolean "or"-if X is non-0, it returns the value of x, otherwise it returns the computed value of Y. |
(A or B) returns 10. |
Not |
Not X |
Boolean "Non"-returns False if X is True. If X is False, it returns TRUE. |
Not (A and B) returns False |
BOOL () Determines whether an object is true or False
A and B if a ==true:return bool (B) if a = = False:return false; A is executed first, and if A is true, the result of B is returned. If A is false, it returns false directly
A or b:if a ==true:return true else:return bool (B) First executes a, if a is true, returns True, otherwise returns the bool value of B
1>>> A ="python"2>>> B =[]3>>>BOOL (a)4 True5>>>bool (b)6 False7>>>bool (0)8 False9>>> BOOL (1)Ten True One>>> 4>3 and4<9 A True ->>> 4<3 and4>9 - False the>>> 4>3or4>9 - True ->>> 4<3or4<9 - True +>>> not(4>3) - False +>>> 4>3 and4>9or4<5 #先执行4 >3 is true, then the right side of and is executed, or 4>9 is false, the execution 4<5 to the right of True,and is true, so this returns true A True at>>>
Conditional statements
Review statements
Print, import, and assignment statements
1 Print " Hello World " #print语句 2HelloWorld3import math #import语句 4 >>> a = 2 #赋值语句
Conditional statements
1 ImportRandom #引入随机数模块2A =random.randint (0,100) #随机0到100的整数3 ifA >50:4 Printa #注意: There are four spaces in front of print5 Print "A is bigger than"6 elifA==50:7 Printa8 Print "a is"9 Else:Ten Printa One Print "A is smaller than"
Ternary operator
A = y if x else Z means a =y, assign Y this object to a, if X is true, assign Y to a, otherwise assign Z to a
>>> x = 2>>> y = 8>>> a ="python"ifelse" Web"If X>y assigns Python to a, the Web is assigned a value. >>> a'web'>>>
Python Learning notes (11) Python Statement (i)