数字类型及操作:
整数(int)
显示数据类型:
print(type(10))
四种进制表达式:
十进制:略
二进制:0B/0b 开头
八进制:0o/0O开头
十六进制:0x/0X开头
对应转换如下下
a = 28
print(bin(a)) #十转2
print(oct(a)) #十转8
print(hex(a)) #十转16
求100以内偶数和
um=0 for i in range(100): if i%2==0: sum=sum+i print(sum)
浮点数(float)
浮点数运算存在不确定尾数,因计算机转化有误差
from decimal import Decimal a = Decimal('1.2') b = Decimal('1') print(a-b)
进度条
count = 10 for i in range(count+1): a = '&'*i b = '*'*(count-i) c = i/count*100 print(f"[{a}->{b}]{c}%")
进度条优化版
print('开始执行'.center(100,'-')) import time count = 100 start = time.time() for i in range(count+1): a = i*'#' b = '%'*(count-i) c = i/count*100 res = time.time() - start print(f" [{a}->{b}]{c:.2f}%{res:.2f}s",end="") time.sleep(0.1) print(' '+'结束执行'.center(100,'-'))
错误点:
1.i循环需要加1,不然无法达到100%
2.time.time(),和perf_counter(),都可以使用
3.格式化字符串
dayup = 1 for i in range(365): if i % 7 in(6,0): dayup = dayup*(1-0.01) else: dayup = dayup*(1+0.01) print(dayup)