time库:

python TTFont用法_系统时间


python TTFont用法_系统时间_02

import time
print(time.ctime())    #获取当前时间\系统时间
#时间格式化
'''
strftime(tpl,ts),tpl是格式化模板字符串,用来定义输出效果
ts是计算机内部时间类型变量
'''
t=time.gmtime()  #GMT时间,比北京时间慢8小时
print(time.strftime("%Y-%m-%d %H:%M:%S",t))  #年月日,时分秒,将t省略则默认采用系统时间(北京时间)

python TTFont用法_系统时间_03

import time
start=time.perf_counter()     #用来计时的
end=time.perf_counter()
print(end-start)   #end-start是程序运行的时间
import time
for i in range(101):
#  \r是在每次输出前将光标移至当前行行首
    print("{:3}%".format(i),end=" jk")# 3是i的输出宽度,end参数使之不换行
    time.sleep(0.01)

python TTFont用法_随机数_04

import time
for i in range(101):
#  \r是在每次输出前将光标移至当前行行首
    print("\r{:3}%".format(i),end=" jk")# 3是i的输出宽度,end参数使之不换行
    time.sleep(0.01)

python TTFont用法_系统时间_05

# 文本进度条实例
import time
scale=50
print("执行开始".center(scale//2,'-'))
start=time.perf_counter()
for i in range(scale+1):
    a='*'*i
    b='.'*(scale-i)
    c=(i/scale)*100
    dur=time.perf_counter()-start
    print("\r{:^3.0f}%[{}->{}]{:.2f}s".format(c,a,b,dur),end='')
    time.sleep(0.1)
print("\n"+"执行结束".center(scale//2,'-'))

python TTFont用法_python_06


逻辑符号:and(与) or(或) not(非)

异常处理:

python TTFont用法_python_07


python TTFont用法_python_08


python TTFont用法_python TTFont用法_09


python TTFont用法_python_10


python TTFont用法_系统时间_11


python TTFont用法_字符串_12


print 默认输出是换行的,如果要实现不换行需要在变量末尾加上 end=""。

#CalBMI.py
height,weight=eval(input("请输入身高和体重: "))#为了获取两个输入,要采用逗号隔开的形式
bmi=weight/pow(height,2)
print("BMI数值为:{:.2f}".format(bmi))
who,nat='',''
if bmi<18.5:
    who,nat='偏瘦','偏瘦'
elif 18.5<=bmi<24:
    who,nat='正常','正常'
elif 24<=bmi<25:
    who,nat='正常','偏胖'
elif 25<=bmi<28:
    who,nat='偏胖','偏胖'
elif 28<=bmi<30:
    who,nat='偏胖','肥胖'
else:
    who,nat='肥胖','肥胖'
print("BMI指标为:国际'{0}',国内'{1}'".format(who,nat))   #0和1都是索引对应who和nat

python TTFont用法_随机数_13

循环结构:

python TTFont用法_字符串_14

#字符串遍历循环
#s是字符串,遍历字符串每个字符,产生循环
#c代表字符串中的每个字符,从字符串中按顺序取出每个字符
s='Python123'
for c in s:
    print(c,end='')
#列表遍历循环
# 将列表中的每一个元素拿出来
print("\n")
for item in [123,'py',456]:
    print(item,end=',')

python TTFont用法_python TTFont用法_15

random库:

#随机数由随机数种子来产生
#不需要再现随机过程的话,不调用seed函数也行
import random
random.seed(10)#随机数的产生与种子有关,种子10产生的第一个随机小数一定是0.57
print(random.random())  #生成一个[0,1)之间的随机小数。
#扩展随机数函数
print(random.randint(10,100))#生成一个[10,100]之间的整数
print(random.randrange(10,100,10))#生成一个[m,n)之间以10为步长的随机整数
print(random.uniform(10,100))#生成一个[10,100]之间的随机小数。

python TTFont用法_python_16


python TTFont用法_python_17