1. notes * *
1.1 single line comment
# The first python annotation
1.2 Multi-line Notes
The first one python Notes' '
**
2. Specify the spacer sep**
When there is no sep, the default spacer is a space
>>> print(1,2,3) 1 2 3
You can specify spacers when there is sep
For example, sep="+" specifies the spacer as+
>>> print(1,2,3,sep="+") 1+2+3
3. line indentation *
if a>60: #With a colon at the end, the next line is indented with a Tab key print("pass")
4. Data type*
Variable type | List, dictionary, set |
---|---|
Immutable type | Numbers, strings, sets |
4.1 figures
int - Integer
float-floating-point type
bool - Boolean (1 and 0)
4.2 String (str)
str(999) #Will automatically convert to string'999' str(6)*6 #This is equivalent to six replications, unlike int (6)*6. str(6)+str(6) #Equivalent to two characters'6'
>>> str(999) '999' >>> str(6)*6 '666666' >>> str(6)+str(6) '66'
>>> int(6)*6 36 >>> int(6)+int(6) 12
4.3 list
list1[1] #A list of elements list2[] #Null list list3[1,2,3]
4.4 tuple
t1=(1,2) t2=(1,) #When there is only one element in a tuple, the comma at the end cannot be dropped t3=tuple() #Empty ancestor
4.5 Sets
set1={1,2} set2={1} set3=set() #Empty set
4.6 Dictionary (dict) - Difference from Collection: Colon in Dictionary
d1={"key1":1,"key2":2} #The dictionary is unique and cannot be renamed. If the name is the same, the first one is read by default. d2={1:2} d3={} #The dictionary is empty.
**
5. Arithmetic Operators*
5.1 Mathematical Operator
Except (/)
Dividing (//)
Balance (%)
Power (**)
>>> 2/3 0.6666666666666666 >>> 2//3 0 >>> 2%3 2 >>> 2**3 #The third power equivalent to 2 8
5.2 Comparing Operator - Return Value is Boolean
Equivalent to (==) - Distinguished from the assignment statement, it is equal to an equal sign, and assignment is a single equal sign.
Not equal to (! =)
Greater than (>)
Less than (<)
Greater than or equal to (>=)
Less than or equal to (<=)
5.3 assignment operator
5.3.1 and operation (&)
>>> bin(12) #Binary representation of 12 '0b1100' #The binary of 12 is 1100, and 0b in front represents binary. >>> bin(25) '0b11001' >>> 12&25 #The sum operations of 12 and 25, bitwise and, are 1:00 and the rest are 0. 8 >>> bin(8) '0b1000'
5.3.2 or operation (|)
>>> 12|25 #Or operations - bitwise or, when both are zero, 0, and the rest are 1 29 >>> bin(29) '0b11101'
5.3.3 Non-operation (~)
>>> ~12 #Non-Operational --- Take the inverse and subtract one (12 takes the inverse as - 12, - 12-1= - 13) -13 >>> bin(-13) '-0b1101'
5.3.4 XOR (^)
>>> bin(12) '0b1100' >>> bin(25) '0b11001' >>> 12^25 #XOR Operations - Bit XOR, Same 0, Different 1 21 >>> bin(21) '0b10101'
5.3.5 left shift (< <)
>>> 12<<1 #Moving one bit to the left is equivalent to multiplying 2 --- 12*2 24 >>> 12<<2 #Move two left --- 12*2*2=48 48
5.3.6 Right Shift (*)
>>> 12>>1 #To move one bit to the right is equivalent to dividing it by 2 --- 12/2 = 6 6 >>> 12>>2 #Move two to the right --- 12/2/2 = 3 3
5.4 Logic Operator
5.4.1 and -- Pre-true, returns the value behind; Pre-false, returns the value before.
>>> a=3 >>> b='pass' >>> c=[] >>> print(a and b) #Front Truth, Return Back Value pass >>> print(c and b) #Pre-vacation, returns the previous value []
5.4.2 or -- Pre-true, returns the value of the front; Pre-false, returns the value of the back
>>> a=3 >>> b='pass' >>> c=[] >>> print(a or b) #Front Truth, Returns Front Value 3 >>> print(c or b) #Pre-leave, returns the following value pass
5.4.3 not
>>> a=3 >>> print(not a) #It turned out to be true not and then False. False >>> c=[] >>> print(not c) #True after false not s True
5.5 member operator
5.5.1 in
>>> 1 in [1,2,3] True >>> 4 in [1,2,3] False >>> 1 in {1:2,2:3,4:1} True >>> 3 in {1:2,2:3,4:1} #Look in the dictionary for the first half False
5.5.2 not in - Contrary to in
>>> 1 not in [1,2,3] False >>> 4 not in [1,2,3] True >>> 1 not in {1:2,2:3,4:1} False >>> 3 not in {1:2,2:3,4:1} True
5.6 Identity Operator
5.6.1 is
>>> a=1 >>> b=1 >>> a is b #Numbers are immutable types True >>> a=[1] >>> b=[1] >>> a is b #Lists are variable types False
5.6.2 is not -- contrary to is
>>> a=1 >>> b=1 >>> a not is b False >>> a=[1] >>> b=[1] >>> a not is b True