Python Learning Process Control Statement details, python statements
###################### Branch statement python3.5 ############### #
# It is very important to control the code indent format with four spaces.
# Determine the program running direction based on the logical value (True, Flase)
# Ture: non-empty values (String, tuple tuples, list, set, and dictonary), all non-zero numbers
# False: 0, None, null
# Logical expressions can contain logical operators and or not
If:
###################################### If ## ###################################### if 1 <2: print ("less than"); print ("<"); print ("indentation is important"); # demodef func01 (): return 1; if func01 (): print (func01 ()); ###################################### if else ######################################## if 1 <2: print ("less than"); else: print ("not less "); ####################################### if elif ###################################### num = int (input ("enter a number: "); if num> = 90: print (" A "); elif num> = 80: print (" B "); elif num> = 70: print ("C"); elif num> = 60: print ("D"); else: print ("E "); ######################################## # if and or not ################################## if 1 and 0: print ("1"); else: print ("0"); # output 0if 1 or 0: print ("1"); # output 1 else: print ("0") if not 1: print ("1"); else: print ("0"); # output 0
Loop and control statement:
############################## For loop ######## ################################# for str in "abcdefg ": print (str, "Hello"); for arr in (4.5, 3,): print (arr ); ################################ range () generation sequence ################################# in python2.x, range returns a list # in python3.x, range returns an iteration value for x in range (10): print ("range:", x); # If you want to 3. A list from 1 to 10 is generated in array x, which can be list (range (1, 10) range = list (range (1, 10); print (range); # [1, 2, 3, 4, 5, 6, 7, 8, 9] ################################### use enumerate # ############################ index and value must be used to traverse a list. you can use enumerate, the parameter is a traversal sequence app_list = [1234,567 7, 8899] for index, app_id in enumerate (app_list): print (index, app_id ); ##################################### use index Traversal ############################### str = "abcde "; # print (str [0]); # print (range [len (str)]); # for v in range (len (str )): # print (str [x]); # Traceback (most recent call last): File "E:/workSpace/pythonWork/function/com/round. py ", # line 24, in <module> for v in range (len (str): TypeError: 'LIST' object is not callable # This error persists ########################### ########### dictionary traversal ########################## ####### dic_map = {"": "AAAA", "B": "BBBB", "c": "CCCCC", "d": "DDDD"}; for x in dic_map: print (x, dic_map [x]); print (dic_map.items (); # [('A', 'aaa'), ('B', 'bbbbb')] for k, j in dic_map.items (): print (k); # key value print (j ); # Value ##################################### ###################################### dic_map2 = {1: "AAAA", 2: "BBBB", 3: "CCCCC", 4: "DDDD"}; for k, j in dic_map2.items (): print (k ); print (j); else: print ("the loop ends normally (no break is encountered)"); print ("###### break" * 10); for k, j in dic_map2.items (): print (k); print (j); if k = 2: break; # terminate loop else: print ("the loop ends normally (no break is encountered) "); print (" ###### continue "* 10); for k, j in dic_map2.items (): if k = 3: continue; # terminate the current loop print (k); print (j); if k = 2: exit (); # terminate the entire program else: print ("the loop ends normally (no break is encountered )");
Loop, control statement while:
######################################## # While loop ##################################### #### determine whether the program continues the Loop Based on the expression result. m = 0; while True: print ("hello"); m + = 1; print (m); if m> 10: break; f = 10; while f> 5: print ("Word"); f-= 1; # Control Condition expressions ########################### while else ####### #################### n = 10; while n> 5: print ("Word"); n-= 1; # Control Condition expression if n = 5: continue; print ("test continue", n ); else: print ("the loop ends normally (no break is encountered )");
The above detailed explanation of the python learning process control statement is all the content that I have shared with you. I hope to give you a reference and support for the help house.