数字和字符串

数字类型

整形

  • 整数, 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模块,文本进度条的更多相关文章

  1. python预课02 time模块,文本进度条示例,数字类型操作,字符串操作

    time模块 概述:time库是Python中处理时间的标准库,包含以下三类函数 时间获取: time(), ctime(), gmtime() 时间格式化: strftime(), strptime ...

  2. 自主学习python文本进度条及π的计算

    经过自己一段时间的学习,已经略有收获了!在整个过程的进行中,在我逐渐通过看书,看案例,做题积累了一些编程python的经验以后,我发现我渐渐爱上了python,爱上了编程! 接下来,当然是又一些有趣的 ...

  3. #Python绘制 文本进度条,带刷新、时间暂缓的

    #Python绘制 文本进度条,带刷新.时间暂缓的 #文本进度条 import time as T st=T.perf_counter() print('-'*6,'执行开始','-'*6) maxx ...

  4. Python入门习题4.文本进度条

    例4.1.设置一组文本进度条,使之运行效果如下: --------执行开始--------% 0 [->**********]%10 [*->*********]%20 [**->* ...

  5. 【Python】文本进度条

    1.0代码: import time#引入time库 scale=10#文本进度条宽度 print("------执行开始------") for i in range(scale ...

  6. python实例文本进度条

    简单的文本进度条代码 解析 引入time库 打印一行作为开始 最后也打印一个结束的标签 定义变量等于10,文本进度条大概的宽度是10 使用for循环来模拟进度,for i in range()能够不断 ...

  7. python_way day6 反射,正则 模块(进度条,hash)

    python_way day6 反射 正则 模块 sys,os,hashlib 一.模块: 1.sys & os: 我们在写项目的时候,经常遇到模块互相调用的情况,但是在不同的模块下我们通过什 ...

  8. sys模块和os模块,利用sys模块生成进度条

    sys模块import sysprint(sys.argv)#sys.exit(0)             #退出程序,正常退出exit(0)print(sys.version)       #获取 ...

  9. [ python ] 使用sys模块实现进度条

    在写网络IO传输的时候, 有时候需要进度条来显示当前传输进度,使用 sys 模块就可以实现: sys.stdout.write() 这个函数在在控制台输出字符串不会带任何结尾,这就意味着这个输出还没有 ...

随机推荐

  1. Python函数作用域和匿名函数

    匿名函数的定义 全局变量和局部变量的概念 global(全局变量)和 nonlocal(局部变量) 闭包.递归.回调 匿名函数 匿名函数  lambda 语法规则:lambda   参数 : 表达式 ...

  2. Eclipse修改JSP文件的默认编码

    Eclipse新建JSP文件,可以看到默认使用的是ISO-8859-1编码,如下图,而这种编码是无法保存中文的,不符合我们的需求 那么应该怎样修改呢?找到菜单Window-Preferences,找到 ...

  3. gn-build

    I'm not completely sure from the error you describe but it sounds like you don't have a .gn file in ...

  4. 经典损失函数:交叉熵(附tensorflow)

    每次都是看了就忘,看了就忘,从今天开始,细节开始,推一遍交叉熵. 我的第一篇CSDN,献给你们(有错欢迎指出啊). 一.什么是交叉熵 交叉熵是一个信息论中的概念,它原来是用来估算平均编码长度的.给定两 ...

  5. jQuery中的CSS(四)

    1. css(name|pro|[,val|fn]), 访问匹配元素的样式属性 jQuery 1.8中,当你使用CSS属性在css()或animate()中,我们将根据浏览器自动加上前缀(在适当的时候 ...

  6. 设计模式-单例模式(Singleton) (创建型模式)

    //以下代码来源: 设计模式精解-GoF 23种设计模式解析附C++实现源码 //Singleton.h #pragma once #include<iostream> class Sin ...

  7. IDEA取消SVN关联 , 在重新分享项目

    IDEA取消SVN关联,在重新分享项目     安装插件 1.打开Intellij中工具栏File的setting(ctrl+alt+s),选择plugins,在右边搜索框输入“SVN”,搜索.选择“ ...

  8. WinCC中通过脚本禁用或启用Windows快捷键

    有些项目要求WinCC全屏运行,并禁止通过操作系统快捷键切换到桌面,这时只需要在WinCC的计算机属性中勾选“禁用用于进行操作系统访问的快捷键”.此后当WinCC运行时,按Win键或Ctrl+Alt+ ...

  9. Introduction to Semidefinite Programming (SDP)

    https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-251j-introduction-to-mathe ...

  10. Laravel5 --- QQ邮箱发送邮件

    1. 在此之前先确认QQ邮箱是否开启了POP3/SMTP服务,如果未开启则须开启 QQ邮箱->设置->账户->POP3/IMAP/SMTP/Exchange/CardDAV/CalD ...