数字和字符串

数字类型

整形

  • 整数, 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. windows linux 通过SSH X11Forwrding 使用图形化界面

    有时候,我们需要在命令行中使用远程的GUI程序,这样我们就需要x11转发的来进行访问: Linux平台下不需要特别的配置,假如我们要远程的机器是centos机器,只要做如下配置即可: #vi /etc ...

  2. shell脚本里使用echo输出颜色

    格式: echo -e "\033[字背景颜色;字体颜色m字符串\033[0m" 转义序列要是通过彩色化提示符来增加个性化,就要用到转义序列. 转义序列就是一个让 shell 执行 ...

  3. c# WF 第3节 窗体的属性

    本节内容: 1:如何找到窗口属性 2:窗口属性 1:如何找到窗口属性 2:窗口属性

  4. Node.js Koa2开发微信小程序服务端

    1.promise.async.await const Koa = require('koa') const app = new Koa() // 应用程序对象 有很多中间件 // 发送HTTP KO ...

  5. JAVA字符串转换整数

    public class compint { /** * @param args */ public static void main(String[] args) { // TODO Auto-ge ...

  6. 【声明式事务】Spring事务特性(二)

    spring所有的事务管理策略类都继承自org.springframework.transaction.PlatformTransactionManager接口. 其中TransactionDefin ...

  7. C++ 异或运算及其应用

    前置知识: 1.一个整数自己跟自己异或,结果为0   //因为异或的法则为,相同为0,不同为1,注意这里所说的都是二进制位. 2.任意一个整数跟0异或,结果为本身. //因为1异或0得1,0异或0,得 ...

  8. C#获取CPU和内存使用率

    获取内存使用率 方式1: using System; using System.Runtime.InteropServices; namespace ConsoleApp1 { public clas ...

  9. ASP.NET开发实战——(十三)ASP.NET MVC 与数据库之EF实体类与数据库结构

    大家都知道在关系型数据库中每张表的每个字段都会有自己的属性,如:数据类型.长度.是否为空.主外键.索引以及表与表之间的关系.但对于C#编写的类来说,它的属性只有一个数据类型和类与类之间的关系,但是在M ...

  10. what is variable?

    what is variable? variable:pytorch中的变量,存储tensor,数值会不断变动 在 Torch 中的 Variable 就是一个存放会变化的值的地理位置. 里面的值会不 ...