# # Sequential structure
-The most basic structure is the execution of a statement in the order from top to bottom.
# # Branching structure
If condition:
Statement
Statement
...
Elif Condition>:
Statement
Statement
...
Else
Statement
Statement
...
-If statement can be nested, but not recommended, try to keep the code concise
-Python does not have a switch-case statement
-Python conditional statements allow the following notation
x = 10
Print (1 < x <) #-True
Print (1 < x < < +) #-True
-The following data will be judged as false:
False, None, 0, 0.0, "", [], (), {}, set ()
# # Loop structure
-Write loop program should pay attention to the initial value of the loop variable, the loop condition and the increment of the cyclic variable, collectively called the three elements of the loop
-While Loop
Count = 0 #-loop variable
While Count < 5: #-Loop conditions
Print (count, end= ",") #-0,1,2,3,4,
Count + = 1 #-loop variable increment, modify the loop variable
-For iteration
-in languages such as C or Java, the For Loop combines the three elements of the loop in a single line of statements: probably as follows:
for (int i = 0; i < i++) {loop body}
-but the for loop in Python is relatively concise
words = ["and", "or", "not"]
For word in words:
Print (Word, end= ",") # and, or, not,
-Lists, tuples, dictionaries, collections, strings are objects that can be iterated
-The iteration of the dictionary can be:
A_dict = {"Name": "Stanley", "Age": "22"}
For K, V in A_dict.items ():
Print ("{0}: {1}". Format (k, V))
#-Name:stanley
Age:22
-the key or value of a separate iteration dictionary can use the keys () or the values () function of the dictionary
-Break keyword
-Using the break keyword in the loop body, the entire loop is immediately and unconditionally stopped
Count = 0
While Count < 5:
if Count = = 2:
Break
Print (count, end= "")
Count + = 1
# 0 1
#--Because when count equals 2 o'clock, the IF statement goes to break, so the loop ends and the unfinished loop is no longer executed
-Continue keyword
-Using the Continue keyword in the loop body, the loop unconditionally stops and the loop after execution
For I in range (0, 5):
if i = = 2:
Continue
Print (I, end= "")
# 0 1 3 4
#-When I equals 2 o'clock enter the IF statement, execute continue, this cycle skips, into the next loop
-Use the Else with loops
For I in range (0, 5):
Print (I, end= "")
Else
Print ("Loop End")
# 0 1 2 3 4 end of cycle
-Executes code in else when the loop is completely closed (not interrupted by break and cuntinue)
-Else also applies to the while loop
-Using a zip () parallel iteration
numbers = [1, 2, 3, 4]
words = ["One", "one", "one", "three", "four"]
days = ["Mon.", "Tues.", "Wed.", "Thur."]
For number, word, day in zip (numbers, words, days):
Print (number, word, day)
Output:
1 One Mon.
2 Tues.
3 Three Wed.
4 four Thur.
-The ZIP () function automatically stops after the elements in the least-length parameter are exhausted, and elements that are not used by other parameters are omitted unless the other shorter parameter lengths are manually extended
-The return value of the zip () function is not a list or tuple, but an iterative variable that is integrated together
List (zip (words, days))
#-[(' One ', ' Mon. '), (' Tues '), (' Three ', ' Wed '), (' Four ', ' Thur. ')]
Book: [American]bill Lubanovic "Python language and its application"
Python code Structure--order, branching, looping