1.运算
(1)向下取整
5//22
-5//2-3
# 整除:含浮点操作数时结果为浮点数
5//2.02.0
整理注: 这里仍是整除
//,不是普通除法/;结果为浮点数是因为操作数中包含浮点数。
(2)只取整数
int(5/2)2
int(-5/2)-2
(3)round函数
精度问题:Values are rounded to the closest multiple of 10 to the power minus ndigits; if two multiples are equally close, rounding is done toward the even choice.
round(2.5)2
round(3.5)4
round(2.65, 1)2.6
round(2.75, 1)2.8
整理注: “距离相等时取偶数”针对实际存储的数值。十进制小数通常不能用二进制浮点数精确表示,因此不要仅按十进制写法推断舍入结果。
(4)Math包
floor()函数:下舍
import math
print(math.floor(45.17))
print(math.floor(-45.17))
print(math.floor(100.12))
print(math.floor(100.72))45
-46
100
100
ceil()函数:上入
print(math.ceil(45.17))
print(math.ceil(-45.17))
print(math.ceil(100.12))
print(math.ceil(100.72))46
-45
101
101
(5)字符串拼接
'1' + '2''12'
'1' * 3'111'
(6)浮点运算
x = 1
3.9 * x * (1 - x)0.0
2.输入
age = input('age')
final = float(age) + 1
print(final)age 11.2
12.2
整理注:
input()是原笔记中的交互示例。本页只展示代码和原有输入/输出记录,不收集输入,也不运行代码。
3.if-else
a = None # ''空字符串 or 0 or [] or False
if a:
print('Hello')
else:
print('World')World
a = ' ' # 非空字符串
if a:
print('Hello')
else:
print('World')Hello