数字,字符串,time模块,文本进度条
数字和字符串
数字类型
整形
- 整数, 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)
数字,字符串,time模块,文本进度条的更多相关文章
- python预课02 time模块,文本进度条示例,数字类型操作,字符串操作
time模块 概述:time库是Python中处理时间的标准库,包含以下三类函数 时间获取: time(), ctime(), gmtime() 时间格式化: strftime(), strptime ...
- 自主学习python文本进度条及π的计算
经过自己一段时间的学习,已经略有收获了!在整个过程的进行中,在我逐渐通过看书,看案例,做题积累了一些编程python的经验以后,我发现我渐渐爱上了python,爱上了编程! 接下来,当然是又一些有趣的 ...
- #Python绘制 文本进度条,带刷新、时间暂缓的
#Python绘制 文本进度条,带刷新.时间暂缓的 #文本进度条 import time as T st=T.perf_counter() print('-'*6,'执行开始','-'*6) maxx ...
- Python入门习题4.文本进度条
例4.1.设置一组文本进度条,使之运行效果如下: --------执行开始--------% 0 [->**********]%10 [*->*********]%20 [**->* ...
- 【Python】文本进度条
1.0代码: import time#引入time库 scale=10#文本进度条宽度 print("------执行开始------") for i in range(scale ...
- python实例文本进度条
简单的文本进度条代码 解析 引入time库 打印一行作为开始 最后也打印一个结束的标签 定义变量等于10,文本进度条大概的宽度是10 使用for循环来模拟进度,for i in range()能够不断 ...
- python_way day6 反射,正则 模块(进度条,hash)
python_way day6 反射 正则 模块 sys,os,hashlib 一.模块: 1.sys & os: 我们在写项目的时候,经常遇到模块互相调用的情况,但是在不同的模块下我们通过什 ...
- sys模块和os模块,利用sys模块生成进度条
sys模块import sysprint(sys.argv)#sys.exit(0) #退出程序,正常退出exit(0)print(sys.version) #获取 ...
- [ python ] 使用sys模块实现进度条
在写网络IO传输的时候, 有时候需要进度条来显示当前传输进度,使用 sys 模块就可以实现: sys.stdout.write() 这个函数在在控制台输出字符串不会带任何结尾,这就意味着这个输出还没有 ...
随机推荐
- java面试遇到的坑[第三期]
亲爱的同学们,本人因为连续几周遭遇一定的工作压力几乎被压榨的只剩一个空壳,还好经常锻炼有一副好身体(皮囊),算是挺过来了.为了大家年前能早早入坑马不停蹄回到阵地给大家带来第二期的面试坑题,有些题是大家 ...
- grep: /usr/include/php/main/php.h: No such file or directory
异常 grep: /usr/include/php/main/php.h: No such file or directory grep: /usr/include/php/Zend/zend_mod ...
- Python自动化运维之高级函数
本帖最后由 陈泽 于 2018-6-20 17:31 编辑 一.协程 1.1协程的概念 协程,又称微线程,纤程.英文名Coroutine.一句话说明什么是线程:协程是一种用户态的轻量级线程.(其实并没 ...
- html学习之二(常用标签练习)
<!DOCTYPE html><head> <meta charset="utf-8"> <title>锚点链接</title ...
- java中的字符串二
public class TestString { public static void main(String[] args) { // TODO Auto-generated method stu ...
- 改名移动批量文件 shutil.copy
import glob import os import shutil filePath = 'F:/project/Breast/InBreast/INBreast/imgout/' newFile ...
- mybatis中<include>标签的作用
MyBatis中sql标签定义SQL片段,include标签引用,可以复用SQL片段 sql标签中id属性对应include标签中的refid属性.通过include标签将sql片段和原sql片段进行 ...
- Fink| API| Time与Window
1. Flink 批处理Api 1.1 Source Flink+kafka是如何实现exactly-once语义的 Flink通过checkpoint来保存数据是否处理完成的状态: 有JobMana ...
- fiddler 抓取winform wcf包
修改客户端配置 <system.net> <defaultProxy> <proxy bypassonlocal="false" usesystemd ...
- mongodb创建管理员用户
db.createUser({user: "admin",pwd: "xxx",roles: ["root"]});