数字和字符串

数字类型

整形

  • 整数, 1/2/3/12/2019
  • 整形用来描述什么, 身高/年龄/体重
age = 18
height = 180

浮点型

浮点数,小数

salary = 10
print(salary)

复数

z = 1 + 2j
print(z.real,z.imag)
## 1.0 2.0

数字类型方法

print(pow(2,3))  # 幂运算
print(1.2+2.3) # 3.5
print(0.1+0.2) # 0.30000000000000004
print(round(0.1+0.44,1)) # 0.5 四舍五入
print(abs(-1)) # 绝对值
print(divmod(16,3)) # 运算结果(商数, 余数

浮点数计算会有误差,小数精准

这就是机器进行二进制计算引入的误差,为了消除这样的误差,进行更加精确的浮点计算,就要是用到decimal模块。

from decimal import *
a = Decimal('0.1') # Decimal函数传入的浮点数必须加引号,构成字符串形式,传入整数就不用了
b = Decimal('0.2')
print(type(a+b),a+b) # <class 'decimal.Decimal'> 0.3 print(Decimal(0.1)) # 0.1000000000000000055511151231257827021181583404541015625 Decimal函数传入浮点数并不精确

小数的精准计算:

from decimal import *
getcontext().prec = 4 # 设置有效数字为4
print(Decimal('2.2')/Decimal('1.3')) # 1.692
from decimal import *
print(Decimal('3.141592653').quantize(Decimal('0.0000'))) # 设定小数位数 这里设置了4位 # 打印结果:3.1416

字符串

name = 'neo'
gender = 'male'
print(name, gender)
  • 三个单引号或三双引号可以换行
poem = '''
When I was a young man, I had liberty, but I did not see it.
I have time, but I did not know it. I have love, but I did not feel it.
Many decades would pass before I understood the meaning of all three.
'''
print(poem)
  • 引号检测机制
print("neo's name is neo")  # 如果字符串中需要有单引号,要用双引号包裹整个字符串
print('''neo's name is "neo"''')
  • 转义
print('neo\'s name is "neo"')   # neo's name is "neo"
print('\tneo') # \t 4个空格,缩进
  • 换行 \n
print('When I was a young man, I had liberty, but I did not see it.\nI have time, but I did not know it. I have love, but I did not feel it.\nMany decades would pass before I understood the meaning of all three.')
# 打印结果:
When I was a young man, I had liberty, but I did not see it.
I have time, but I did not know it. I have love, but I did not feel it.
Many decades would pass before I understood the meaning of all three.
  • r 取消转义
print(r'\ta\na')
# 打印结果:\ta\na
  • \r \r 默认表示将输出的内容返回到第一个指针,这样的话,后面的内容会覆盖前面的内容

字符串运算

print('neo' + '123')   # neo123
print('neo'* 4) # neoneoneoneo

字符串常用内置方法

s = 'hello world'
res = s.split('o') # 切割
print(res)
# 打印结果:['hell', ' w', 'rld'] print(s.startswith('h')) # 以指定字符串开头,就打印True
print(s.endswith('d'))
print(s.center(20,'*')) # 填充 ****hello world*****
  • f-string格式化
s1 = 'neo'
s2 = '25'
s3 = 'height'
s4 = 180
print(f'{s1} {s2} {s3} {s4}') # {} 占位,且数字自动转化为字符串
print('{} {} {} {}'.format(s1,s2,s3,s4))
  • 字符居中/居左/居右
s = 'neo121'
print(f'{s:*^10}') # **neo121**
print(f'{s:*<10}') # neo121****
print(f'{s:*>10}') # ****neo121

time模块

import time

print(time.time())  # 从1970.01.01.00:00开始计算时间
import time

print('-------')
time.sleep(3) # 睡眠
print('-------')
# cpu级别的时间计算,一般用于程序耗时时间计算
import time start = time.perf_counter()
for i in range(10):
print(i)
time.sleep(0.01)
print(time.perf_counter() - start) # 打印结果:
0
1
2
3
4
5
6
7
8
9
0.10681829999999998

文本进度条

'''
0 %[->..........]
10 %[*->.........]
20 %[**->........]
30 %[***->.......]
40 %[****->......]
50 %[*****->.....]
60 %[******->....]
70 %[*******->...]
80 %[********->..]
90 %[*********->.]
100%[**********->]
'''

简单开始

星号在递增,小点在递减,用两个循环

for i in range(10):
print('*'* i + '.' * (10 - i)) # 打印结果:
..........
*.........
**........
***.......
****......
*****.....
******....
*******...
********..
*********.
for i in range(10):
print(f'[{"*" * i} -> {"." * (10 - i)}]') # 打印结果:
[ -> ..........]
[* -> .........]
[** -> ........]
[*** -> .......]
[**** -> ......]
[***** -> .....]
[****** -> ....]
[******* -> ...]
[******** -> ..]
[********* -> .]
for i in range(10):
print(f'{i*10: ^3}% [{"*" * i} -> {"." * (10 - i)}]') # 打印结果:
0 % [ -> ..........]
10 % [* -> .........]
20 % [** -> ........]
30 % [*** -> .......]
40 % [**** -> ......]
50 % [***** -> .....]
60 % [****** -> ....]
70 % [******* -> ...]
80 % [******** -> ..]
90 % [********* -> .]

继续修改

scale = 11
for i in range(scale):
print(f'{(i/scale)*scale: ^3.1f}% [{"*" * i} -> {"." * (scale - i)}]') # 打印结果:
0.0% [ -> ...........]
1.0% [* -> ..........]
2.0% [** -> .........]
3.0% [*** -> ........]
4.0% [**** -> .......]
5.0% [***** -> ......]
6.0% [****** -> .....]
7.0% [******* -> ....]
8.0% [******** -> ...]
9.0% [********* -> ..]
10.0% [********** -> .]

单条显示

scale = 101
for i in range(scale):
print(f'\r{(i/scale)*scale: ^3.1f}% [{"*" * i} -> {"." * (scale - i)}]', end='') # 打印结果:
100.0% [**************************************************************************************************** -> .]

文本进度条最终形式

import time

start = time.perf_counter()
scale = 101
for i in range(scale):
print(f'\r{(i / scale) * scale: ^3.1f}% [{"*" * i} -> {"." * (scale - i)}] {time.perf_counter() - start:.2f}s',
end='')
time.sleep(0.1)

最新文章

  1. Android 进程常驻----native保活5.0以上方案推演过程以及代码
  2. android 蓝牙串口通讯使用简介
  3. php报错日志:PHP Deprecated:Automatically populating $HTTP_RAW_POST_DATA is deprecated
  4. PDF 补丁丁 0.4.2.891 测试版发布:合并PDF文件时设置书签文本和样式
  5. o2o家庭助手demo
  6. POJ 2711 Regular Words(DP + 高精度)
  7. Bash 字符串处理命令
  8. samba搭建
  9. &lt;Win32_14&gt;__win32控件(2)__教你自学掌握所有控件的玩法
  10. SQL 常用语法一
  11. 深入浅出低功耗蓝牙(BLE)协议栈
  12. [Hyperledger] Fabric系统中 peer模块的 gossip服务详解
  13. Apache kylin 入门
  14. centos/ubuntu 双击运行 .sh(shell)文件
  15. Matplotlib学习---用matplotlib画散点图,气泡图(scatter plot, bubble chart)
  16. JavaScript 匿名函数
  17. fiddler学习总结--fiddler抓包篡改数据请求
  18. Keepalive+nginx实现高可用负载均衡方案
  19. Android--Android Studio 打开ADM报错
  20. [转]bigbluebutton中文社区 / 开放API / bbb API

热门文章

  1. nginx 安装 lua_nginx_module 模块(nginx——lua 学习笔记1)
  2. 使用ML.NET进行自定义机器学习
  3. webstorm的git操作使用
  4. 【bzoj4154】(dfs序+kd-tree)
  5. day47_9_6(前端之js)
  6. (day55)七、查询优化、MTV和MCV、choices、AJAX、序列化
  7. 剑指Offer-26.二叉搜索树与双向链表(C++/Java)
  8. stm32配置led
  9. [PKUSC2018]最大前缀和(状压DP)
  10. JVM调优YoungGC