内容正文:
第5章 程序控制结构
Chap5 Program Control Structure
Nanjing University
1
程序控制结构
2
顺序
选择
循环
sequence
structure
selection structure
repetition structure
Python
Nanjing University
顺序结构
5.1
3
Python
Nanjing University
顺序结构
4
A
B
# Filename: seq.py
mystring = 'Hello, World!' print(mystring)
File
一个入口
一个出口
Python
Nanjing University
5.1.1 赋值语句
5
Python
Nanjing University
赋值语句
6
普通
赋值
r = 2
增量
赋值
m /= 5
链式
赋值
b = a = a + 1
多重
赋值
p , r = 3 , 5
Nanjing University
多重赋值
多重赋值的基本形式
7
变量1, 变量2, …, 变量n = 表达式1, 表达式2, …, 表达式n
>>> name, age = 'Niuyun', 18
>>> name
'Niuyun'
>>> age
18
Source
Python
Nanjing University
多重赋值的本质
8
>>> name, age = 'Niuyun', 18
>>> temp = 'Niuyun', 18
>>> temp
('Niuyun', 18)
>>> name, age = temp
>>> name
'Niuyun'
>>> age
18
Source
元组打包
Tuple packing
序列解包
Sequence unpacking
Python
Nanjing University
8
多重赋值
9
>>> x = 3
>>> y = 5
>>> x, y = y, x
>>> x
5
>>> y
3
Source
语法糖
syntactic sugar
Python
Nanjing University
5.1.2 基本输入和输出语句
10
Python
Nanjing University
输入/输出
11
输入
输出
input()
print()
Python
Nanjing University
输入函数input()
12
输入语句的一般形式:
x = input(['输入提示'])
返回值类型是str
Python
Nanjing University
输入input()函数
13
>>> x = input('Enter an integer between 0 and 10: ')
Enter an integer between 0 and 10: 3
>>> x
'3'
>>> x = int(input('Enter an integer between 0 and 10: '))
Enter an integer between 0 and 10: 3
>>> x
3
>>> y = float(input('Enter the price of the apples: '))
Enter the price of the apples: 4.5
>>> y
4.5
Source
返回值类型:str
int()函数
float()函数
Python
Nanjing University
输入input()函数
14
>>> z = eval(input('Enter a number: '))
Enter a number: 3
>>> z * 3
9
>>> z = eval(input('Enter the price of every apple: '))
Enter the price of every apple: 3.5
>>> z * 3
10.5
>>> z = eval(input('Enter the price of the apples: '))
Enter the price of the apples: 3 * 3.5
>>> z
10.5
Source
返回值类型:str
eval()函数
将输入的的字符串当成有效的Python表达式来求值
Python
Nanjing University
输出函数print()
15
输出语句的一般形式:
print(对象1, 对象2, …, 对象n, sep = ' ', end = '
' )
输出到标准输出设备
sep表示输出对象之间的分隔符,默认为空格
参数end的默认值为'
',表示print()函数输出完成