Today is the second day of school python, is the main learning content process control: IF selection structure, While loops, for loops.
1. Select Structure
1.1 if
if condition:
pass
elif:
pass
else:
pass
if/else
Grammatical structure is such that any number can be added to the intermediate elif
, it may not be band else
according to a given demand. python is not switch
, it can only be used for the selection of multi-branch structure elif
, but this is very clear that some of the more simple, in fact, I was learning Java is rarely used switch selection structure.
if
The judgment conditions require a Boolean value, true or false will do, but be careful Frue
and False
use capital letters, otherwise it will not be recognized as a keyword.
1.2 switch
python and did not switch
2. loop structure
2.1 while loop
while expression:
pass
else:
pass
while
Loop syntax structure shown above, can not add else
, Boolean value determination condition remains.
You can be nested if
recycled continue
and the break
keyword, for example:
while expression:
if True:
continue
else:
break
else:
pass
Which continue
keywords can skip the current cycle subsequent statements directly into the next cycle, break the cycle can out of the follow-up statement to perform the entire cycle structure.
2.2 for loop
for target_list in expression_list:
pass
else:
pass
In python for
loop is only used as an iterator, it can be used with range()
the function to form a structure similar to the Java foreach
for i in list:
for i in range(1, 10, 1):
for i in range(1,10):
print(i)
Equivalent to
public class Test {
public static void main(String[] args) {
for (int i : list)) {
System.out.println(i);
}
for (int i = 0; i < 10; i++) {
System.out.println(i);
}
}
}
Obviously python in for
integrated syntax for foreach is simple, with range()
function but also has the power of the Java standard for statement, part-time is much to not work.
- Another:
range()
function can have up to three parameters,range(1,10,1)
means from 1 to 9, steps of an array 1
a = []
b = []
i = 1
while i < 10:
a.append(i)
i += 1
print(a)
for i in range(1, 10, 1):
b.append(i)
print(b)
The output is:
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
3. Summary
Process control and other development language syntax similar, but more simple, smaller code size, appears to reduce the number of features, such as cut off switch, for can only be used as an iterator, While as the main logic loop. However, in actual use, in fact, does not require much complex applications, the most common use is to count loop, iteration, or deploy multiple environments script used to reduce the amount of code. More single function but can improve the readability of the code. Moreover, the mix of various functions or methods, fully loud glory!