初识python

数字和字符串

数字类型

整形

  • 整数, 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)
    原文作者:SetCreed
    原文地址: https://www.cnblogs.com/setcreed/p/11405723.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞