5. Control Statements in Python

1. Select statement (if)

The basic form of a select statement:

if <条件>:
    语句
elif <条件>:
    语句
else:
    语句

Example of an if statement:

age = input('Please Input Your Year: ')
age = int(age)
if age < 18:
    print('未成年')
elif age >= 18 and age <= 25:
    print('青年')
else:
    print('老年人了!!!!')

Running result:
Please Input Your Year: 26
seniors! ! ! !

2. Loop statements (for, while)

  • for loop statement
  • while loop statement

(1), for loop statement

Basic form of for loop:

for <循环变量> in <遍历对象>:
    语句
else:
    语句
  • The statement in else is only executed when the loop exits normally;
  • The break statement ends the loop, and the continue statement ends the loop;

①For loop example:

for i in [1, 2, 3, 4, 5, 6, 7, 8]:
    if i == 2:
        continue
    if i == 4:
        break
    print(i)
else:
    print('程序结束')

Running result:
1
3

②Dictionary traversal example:

map = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
#遍历键和值
for key, value in map.items():
    print(key, ': ', value, sep='', end='')
    print(' ', sep='', end='')
print()
#遍历键
for key in map.keys():
    print(key, end='')
    print(' ', sep='', end='')
print()
#遍历值
for value in map.values():
    print(value, end='')
    print(' ', sep='', end='')
print()

Running result:
a: 1 b: 2 c: 3 d: 4
abcd
1 2 3 4

The range() function produces a list of integers;

  • The prototype of the range() function is as follows: range([start,]stop[,step])
  • start : the starting number, the default value is 0;
  • stop : the number of stops, an integer up to stop-1 is generated;
  • step : optional parameter, step size;

Example of for loop and range function:

sum = 0
sum = 0
for i in range(0, 101):
    sum += i
print('total is :', sum)

Running result:
total is : 5050

Built-in iteration functions:

  • enumerate(seq) number iteration
  • sorted(seq) sort iteration
  • reversed(seq) reverse the iteration
  • zip(seq1, seq2, …) iterates in parallel

Example of built-in iteration function:

lst1 = [2, 5, 1, 4, 8, 6]
lst2 = [5, 3, 2, 2, 4, 8]

# enumerate 函数
for index, value in enumerate(lst1):
    print('index is %d, value is %d' % (index, value))
# sorted 函数
print(sorted(lst1))
#reversed 函数
for i in reversed(lst2):
    print(i, end=' ')
print()
# zip函数
for i, j in zip(lst1, lst2):
    print('i is %d, j is %d' % (i, j))

运行结果:
index is 0, value is 2
index is 1, value is 5
index is 2, value is 1
index is 3, value is 4
index is 4, value is 8
index is 5, value is 6
[1, 2, 4, 5, 6, 8]
8 4 2 2 3 5
i is 2, j is 5
i is 5, j is 3
i is 1, j is 2
i is 4, j is 2
i is 8, j is 4
i is 6, j is 8

(2), while loop

Basic form of while loop:

while <条件>:
    <语句1>
else:
    <语句2>     #如果循环未被break退出,则执行

Example of while loop:

# 使用while循环计算前100个数的和
i = 0
sum = 0
while i <= 100:
    sum += i
    i += 1
else:
    print('Overred')

print('totla is:', sum)

Running result:
Overred
totla is: 5050

This i + = 1 Same i = i + 1

3. Overturn or connotation

Basic form:

[ <i 相关表达式> for i in aiterator ]
[ <i 相关表达式> for i in aiterator if <条件> ]

Push to example:

# 列表推倒,获取0-10的平方的列表
lst = [i * i for i in range(0, 11)]
print(lst)

# 字典推倒,获取字典
keys = ['name', 'age', 'weight']
values = ['Douzhq', 26, 75]
map = {key: value for key, value in zip(keys, values)}
print(map)

# 带条件推倒,获取0-10的平方的偶数列表
lst = [i * i for i in range(0, 11) if i * i % 2 == 0]
print(lst)

Running result:
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
{'name': 'Douzhq', 'age': 26, 'weight': 75}
[0, 4, 16, 36, 64, 100]

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325643351&siteId=291194637