变量
变量名包括字母、数字和下划线。变量不能以数字开头。1
2
3>>>x = 3
x * 2
输出:6
获取用户的输入
使用input函数1
2
3>>>input("The meaning of life: ")
the meaning of life: 42
输出:'42'
几个标准内建函数
1.乘方函数:pow1
2
3
4
5>>>from math import pow
>>>2**3
输出:8
>>>pow(2, 3)
输出:8
2.绝对值函数:abs1
2
3>>>from math import abs
>>>abs(-19)
19
3.四舍五入函数:round1
2
3>>>from math import round
>>>round(1.0/2.0)
1.0
4.向下取整函数:floor1
2
3>>>from math import floor
>>>floor(23.9)
23
模块
导入模块的时候使用import语句1
2
3>>>import math
>>>math.floor(23.9)
23
在确定不会导入多个同名函数(从不同的模块)的情况下,你可能不希望每次调用函数的时候都写上模块的名称。那么此时可以使用import的另外一种形式:1
2
3>>>from math import sqrt
>>>sqrt(9)
3.0