第三周 基本数据类型

2020-01-23  本文已影响0人  Yuanshuo

数字类型及操作

整数类型

a = pow(2, 3)
print(a)

二进制0b0B开头
八进制0o0O开头
十六进制0x0X开头

浮点数类型

round(x, d)  #四舍五入函数,d截取小数位数

浮点数的科学计数法表示
<a>e<b> ----> a*10^b

复数类型

z = 7.77e + 77j
a = z.real #实部
b = z.imag #虚部

数值类型及操作

操作符及使用 描述
x + y x, y之和
x - y x, y之差
x * y x, y之积
x / y x, y之商 10/3=3.3333
x // y x, y之整数商 10//3=3
x % y 模运算,返回除法余数 5%2=1
x ** y 幂运算,x^y
x += y x = x + y
x -=y x = x - y
x *=y x = x * y
x /=y x = x / y
x %=y x = x % y
x **=y x = x ** y
x //=y x = x // y

数值运算函数

函数及使用 描述
int(x) 直接舍弃小数部分,将x变为整数
float(x) 增加小数部分,将x变为浮点数
complex(x) 增加虚部部分,将x变为复数

实例

# example 1 天天向上
# 读取键入变量
a = input("print your variable:")
# 每天进步一点点
dayup = pow(1+a, 365)
# 每天退步一点点
daydown = pow(1-a, 365)
# 输出打印
print("dayup : {:.2f}, daydown : {:.2f}".format(dayup, daydown))
def dayUp(a):
    dayup = 1
    for i in range(365):
        if i % 7 in [0,6]:
            dayup = dayup * (1 - 0.01)
        else:
            dayup = dayup * (1 + a)
    return dayup

a = 0.01

while dayUp(a) < 37.78:
    a += 0.001
print("工作日努力参数:{:.3f}".format(a))

字符串类型及操作

字符串操作符

操作符及使用 描述
x + y 连接字符串x, y
n * x 或 x * n 复制n次字符串x
x in s 如果x是s的子串,返回True;否则False
eval() #去掉最外层引号
字符串处理函数 描述
len(x) 返回字符串x的长度
str() 返回任意类型x的字符串形式
hex(x) 返回整数x的十六进制的字符串类型
oct(x) 返回整数x的八进制的字符串类型
字符串处理方法 描述
str.lower() 返回小写字符
str.upper() 返回大写字符
str.split() 对字符串切片
str.count(sub) 返回字串sub在str中出现的次数
str.replace(a, b) 将a替换为b
str.center(width, fillchar) 在width内str居中
str.strip(chars) 从str中删去其左右两侧chars中所列出的字符
str.join(iter) 用str分割字符串iter

time库的使用

函数 描述
time() time.time()获取当前时间戳
ctime() time.ctime()获取当前时间并以易读方式表示
gmtime() time.gmtime()获取当前时间,可调取的时间格式
import time

Time = time.time()
CTime = time.ctime()
GMtime = time.gmtime()
print('time.time():', Time)
print('time.ctime():', CTime)
print('time.gmtime()', GMtime)
time.time(): 1579741343.4657865
time.ctime(): Thu Jan 23 09:02:23 2020
time.gmtime() time.struct_time(tm_year=2020, tm_mon=1, tm_mday=23, tm_hour=1, tm_min=2, tm_sec=23, tm_wday=3, tm_yday=23, tm_isdst=0)
import time

start = time.perf_counter()
wait = time.sleep(7)
end = time.perf_counter()
timekeeping = end - start
print('Timekeeping:', timekeeping)
Timekeeping: 7.000483000000031
import time

scale = 50
print('Start'.center(scale, '-'))
start = time.perf_counter()
for i in range(scale+1):
    a = '*' * i
    b = '.' * (scale-i)
    c = (i/scale) * 100
    timekeeping = time.perf_counter() - start
    print('{:^3.0f}%[{}->{}]{:.2f}s'.format(c,a,b,timekeeping))
    time.sleep(0.1)
print('\n' + 'End'.center(scale, '-'))
----------------------Start-----------------------
 0 %[->..................................................]0.00s
 2 %[*->.................................................]0.10s
 4 %[**->................................................]0.20s
 6 %[***->...............................................]0.30s
 8 %[****->..............................................]0.40s
10 %[*****->.............................................]0.50s
12 %[******->............................................]0.60s
14 %[*******->...........................................]0.70s
16 %[********->..........................................]0.80s
18 %[*********->.........................................]0.90s
20 %[**********->........................................]1.01s
22 %[***********->.......................................]1.11s
24 %[************->......................................]1.21s
26 %[*************->.....................................]1.31s
28 %[**************->....................................]1.41s
30 %[***************->...................................]1.51s
32 %[****************->..................................]1.61s
34 %[*****************->.................................]1.71s
36 %[******************->................................]1.81s
38 %[*******************->...............................]1.91s
40 %[********************->..............................]2.01s
42 %[*********************->.............................]2.11s
44 %[**********************->............................]2.21s
46 %[***********************->...........................]2.31s
48 %[************************->..........................]2.41s
50 %[*************************->.........................]2.51s
52 %[**************************->........................]2.62s
54 %[***************************->.......................]2.72s
56 %[****************************->......................]2.82s
58 %[*****************************->.....................]2.92s
60 %[******************************->....................]3.02s
62 %[*******************************->...................]3.12s
64 %[********************************->..................]3.22s
66 %[*********************************->.................]3.32s
68 %[**********************************->................]3.42s
70 %[***********************************->...............]3.52s
72 %[************************************->..............]3.62s
74 %[*************************************->.............]3.72s
76 %[**************************************->............]3.82s
78 %[***************************************->...........]3.92s
80 %[****************************************->..........]4.02s
82 %[*****************************************->.........]4.13s
84 %[******************************************->........]4.23s
86 %[*******************************************->.......]4.33s
88 %[********************************************->......]4.43s
90 %[*********************************************->.....]4.53s
92 %[**********************************************->....]4.63s
94 %[***********************************************->...]4.73s
96 %[************************************************->..]4.83s
98 %[*************************************************->.]4.93s
100%[**************************************************->]5.03s

-----------------------End------------------------
# 单行刷新
import time

scale = 50
print('Start'.center(scale, '-'))
start = time.perf_counter()
for i in range(scale+1):
    a = '*' * i
    b = '.' * (scale-i)
    c = (i/scale) * 100
    timekeeping = time.perf_counter() - start
    print('\r{:^3.0f}%[{}->{}]{:.2f}s'.format(c,a,b,timekeeping), end='')
    time.sleep(0.1)
print('\n' + 'End'.center(scale, '-'))
----------------------Start-----------------------
100%[**************************************************->]5.03s
-----------------------End------------------------
上一篇下一篇

猜你喜欢

热点阅读