1. Conditional judgment
age = 3if age >= 18: print(‘adult‘)elif age >= 6: print(‘teenager‘)else: print(‘kid‘)
Elif is an else if abbreviation and can have more than one elif.
2. Cycle
There are two types of Python loops,
One is the for...in loop, which sequentially iterates through each element of the list or tuple.
names = ["geg","gege","geege"];for name in names: print(name)
If you want to calculate the sum of 1-100 integers, it would be tiring to write a list using the exhaustive method, but if Python provides a range () function, you can generate an integer sequence and then convert the sequence to a list by using the list () function.
list(range(5));[0,1,2,3,4]//注意这里是0开始到4.
The other is a while loop, which, as long as the condition is satisfied, loops continuously and the condition does not satisfy the exit loop.
sum = 0;n = 99;while n > 0: sum = sum + n n = n - 2print(sum)
In a loop, you can use break to exit the loop early, or you can use continue to skip the current loop and start the next loop directly during the loop.
Conditional judgment and looping on the basis of Python