Expression
Expression is a sequence of operator s and operand s
>>> 1 + 1 2 >>> a = [1,2,3] >>> 1 + 1 + 1 + 1 4 >>> 1 + 2 * 3 7 >>> 1 * 2 + 3 5 >>> a = 1 + 2 * 3 >>> a = 1 >>> b = 2 >>> c = a and b or c >>> c = int('1') + 2
Operator priority
For example, the priority of and in logical operators is higher than or
>>> a = 1 >>> b = 2 >>> c = 3 >>> a + b * c 7 >>> 1 or 2 1 >>> 1 and 3 3 >>> a or b and c 1 >>> a or (b and c) 1 >>> a or 3 1 >>> (a or b) and c 3 >>> (a or b) and (c + 1) //Two brackets of the same level, left combined 4 >>> a = 1 >>> b = 2 >>> c = a + b //When assignment symbols appear, combine right >>> print(c) 3 >>> c = a or b >>> print(c) 1 >>> a = 1 >>> b = 2 >>> c = 2 >>> not a or b + 2 == c False >>> ((not a) or ((b + 2) == c)) //Priority: not > and > or False
Writing Python code in a text file
The python script is a file with the suffix. py, which is executed through the command line "python filename.py"
Recommended IDE: pycharms, vscodes, pycharms for large projects, vscodes for learning, plug-ins recommended in vscodes: python, Terminal, Vim, vsCode icons
Notes
For single line annotation#
For multiline annotation```
Process control statement
Mainly conditional control (if else), circular control (for while), branch
Conditional control (if else)
# encoding: utf-8 mood = False if mood : print('go to left') # print('back away') # print('back away') else : print('go to right') a = 1 b = 2 c = 2 # if can be followed by not only a Boolean value, but also an expression if a or b + 1 == c : print('go to left') # print('back away') # print('back away') else : print('go to right')
# encoding: utf-8 """ //A small program """ # Constant constant recommends all uppercase ACCOUNT = 'hughie' PASSWORD = '123456' # python variables are recommended to be lowercase, separated by underscores, and named without humps print('please input account') user_account = input() print('please input password') user_password = input() if ACCOUNT == user_account and PASSWORD == user_password: print('success') else: print('fail')
# encoding: utf-8 # snippet fragment if condition: pass else: pass a = True if a: # pass empty statement / occupation statement pass else: print('') if True: pass if False: pass # Nested branch if condition: if condition: pass else: pass else: if condition: pass else: pass # Code block if condition: code1 code11 code22 code333 code444 code5555 code6666 code2 code3 else: code1 code2 code3
Overwrite with elif
# encoding: utf-8 """ a = x a = 1 print('apple') a = 2 print('orange') a = 3 print('banana') print('shopping') """ a = input() print('a is' + a) if a == 1: print('apple') else: if a == 2: print('orange') else: if a == 3: print('banana') else: print('shopping') # Overwrite with elif a = input() print(type(a)) print('a is ' + a) a = int(a) if a == 1: print('apple') elif a == 2: print('orange') elif a == 3: print('banana') else: print('shopping')
Loop (while for)
# encoding: utf-8 # loop # Loop statement # while for # CONDITION = True # while CONDITION: # print('I am while') counter = 1 # Recursive common while while counter <= 10: counter += 1 print(counter) else: print('EOF')
# encoding: utf-8 # It is mainly used for traversing / looping sequences or collections, dictionaries # a = ['apple', 'orange', 'banana', 'grape'] # for x in a: # print(x) # a = [['apple', 'orange', 'banana', 'grape'], (1, 2, 3)] # for x in a: # for y in x: # # print(y, end='') # print(y) # else: # print('fruit is gone') # a = [1, 2, 3] # for x in a: # if x == 2: # # break will terminate when x==2 and print out 1 # # break # # continue to skip when x==2 and print out 1, 3 # continue # print(x) # else: # print('EOF') a = [['apple', 'orange', 'banana', 'grape'], (1, 2, 3)] for x in a: # if 'banana' in x: # break for y in x: if y == 'orange': # After the internal loop jumps out, the external loop is still executing break print(y) else: print('fruit is gone')
# encoding: utf-8 # for (i=0; i<10; i++){} # The above for loop is implemented in python # for x in range(0, 10): # # range(0,10) represents 10 numbers starting from 0, excluding 10 # print(x) # for x in range(0, 10, 2): # # range(0,10,2) 2 for step size # print(x, end=' | ') # # Print result: 0 | 2 | 4 | 6 | 8| for x in range(10, 0, -2): print(x, end=' | ') # Print results: 10 | 8 | 6 | 4 | 2|
# encoding: utf-8 a = [1, 2, 3, 4, 5, 6, 7, 8] # for i in range(0, len(a), 2): # print(a[i], end=' | ') # 1 | 3 | 5 | 7 | b = a[0:len(a):2] print(b) # [1, 3, 5, 7]