print(item1,item2,...,itemk)
radius = 20 # radius is now 20
area =radius * radius *3.14159
print("This area for circle of radius " ,radius ,"is",area)
radius = input("Enter a value for radius: ")
用来转化数据类型(将用户输入的字符串类型转换为数值类型)
\ 继续符号
sum= 1 + 2 + 3 + 4 + \
5 + 6
radius number average input eval print 统称为标识符
variable = expression
var1, var2, ..., varn = exp1, exp2, ..., expn
例子 :交换x 和 y 之间的值
x = 1
y = 2
temp = x
x = y
y = temp
同时赋值案例 :
x , y = y , x # Swap x with y
radius = 20 # raidus is now 20
# Compute area
PI = 3.14159
area = radius * radius * PI
...
python 有两种数据类型 (整数和浮点数)
+ , - , * , / , // , % , **
名称 | 含义 | 举例 | 结果 |
---|---|---|---|
+ | Addtion | 34 + 1 | 355 |
- | Subtraction | 34.0 - 0.1 | 39.9 |
* | Multiplication | 300 * 30 | 9000 |
/ | Float Division | 1 / 2 | 0.5 |
// | Integer | 1 // 2 | 0 |
** | Exponentiation | 4 ** 0.5 | 2 |
% | Remainder | 20 % 3 | 2 |
102.456 等同于 1.02456E+2
0.0123456 等同于 1.02456E-2
字母E大小写随意
>>> 245.0 ** 1000
Traceback (most recent call last):
File "" , line 1, in <module>
245.0 ** 1000
OverflowError: (34, 'Result too large')
>>>
python 表达式计算方式和算术表达式一样
多个运算符时会有运算符优先级计算顺序
一般写法 count = count + 1
增强型赋值 count += 1
其他运算符类似
int(value) 函数可以将一个浮点类型的数字变为int类型
>>> value = 5.6
>>> int(value)
5
>>>>
round(value) 函数 四舍五入
>>> value = 5.6
>>> round(value)
6
>>>
int(value)和round(value) 不会改变value的值。
共同点:
都可以用于将整数字符串转换为整数
不同点:
int(value) 不能用于非整型字符串
例如 int(3.4)
eval(value) 转换时不能先有导零
time() 函数来获取当前的系统时间
1. time.time() 获取当前系统时间(自1970年1月1日零时起)
2. 用 int(time) 获取总秒数
3. time % 60 来求现在的秒数
4. time // 60 来求总分钟数
5. 总分钟数 mtime % 60 来求当前分钟数
6. 总分钟数 mtime // 60 来求总小时数
7. 总小时 htime % 24 来求现在的小时数