函数的使用示例
- abs(x)
>>> abs(10)
10
>>> abs(0)
0
>>> abs(-10)
10
>>> abs(1+2j)
2.23606797749979
>>> pow(5,0.5)
2.23606797749979
- divmod(x,y)
>>> divmod(10,3)
(3, 1)
>>> divmod(25,2)
(12, 1)
>>>
- pow(x,y)
>>> pow(2,5)
32
>>> pow(25,0.5)
5.0
>>> pow(100,0)
1
- round(x,d)
>>> round(10/3,3)
3.333
>>> round(2/3,3)
0.667
>>>
- z.real和z.imag(x)
>>> z=1+2j
>>> z.real
1.0
>>> z.imag
2.0
>>>
- min/max(x1,x2….)
>>> max(1,2,5,545,54631,5646531,56)
5646531
>>> min(1,2,5,3,5,2,-545,-1)
-545
>>>
- int/float/complex()
int和float无法将complex转换为int/float
>>> int(232.214551354)
232
>>> float(1+8j)
Traceback (most recent call last):
File "<pyshell#20>", line 1, in <module>
float(1+8j)
TypeError: can't convert complex to float
>>> int(1+2j)
Traceback (most recent call last):
File "<pyshell#21>", line 1, in <module>
int(1+2j)
TypeError: can't convert complex to int
>>> float(12.65)
12.65
>>> float(12)
12.0
>>> complex(12)
(12+0j)
>>> complex(12.65)
(12.65+0j)
>>>
- eval()
>>> s='123'
>>> eval(s)
123
>>>
实例一:温度转换
#TempConvert.py //注释行
TempStr = input("请输入带有符号的温度值: ")
if TempStr[-1] in ['F', 'f']:
C = (eval(TempStr[0:-1]) - 32)/1.8
print("转换后的温度是{:.2f}C".format(C))
elif TempStr[-1] in ['C', 'c']:
F = 1.8*eval(TempStr[0:-1]) + 32
print("转换后的温度是{:.2f}F".format(F))
else:
print("输入格式错误")
实例二:蟒蛇
#PythonDraw.py 注释
import turtle
turtle.setup(650, 350, 200, 200) 设置画布的长宽以及在屏幕上的位置
turtle.penup() 提起画笔,准备绘画
turtle.fd(-250) 向前移动-250像素长度,即后退250
turtle.pendown() 画笔落下
turtle.pensize(25) 画笔宽度的设置
turtle.pencolor("purple") 画笔颜色的设置
turtle.seth(-40) 画笔角度的设置为绝对-40°
for i in range(4):
turtle.circle(40, 80) 画笔左边画80°的圆,半径为40
turtle.circle(-40, 80) 画笔左边画80°的圆,半径为40
turtle.circle(40, 80/2) 画笔左边画40°的圆,半径为40
turtle.fd(40)
turtle.circle(16, 180)
turtle.fd(40 * 2/3)
turtle.done()
实例三:玫瑰花(请读者自行参照上一章内容分析)
import turtle
# 设置初始位置
turtle.penup()
turtle.left(90)
turtle.fd(200)
turtle.pendown()
turtle.right(90)
# 花蕊
turtle.fillcolor("red")
turtle.begin_fill()
turtle.circle(10,180)
turtle.circle(25,110)
turtle.left(50)
turtle.circle(60,45)
turtle.circle(20,170)
turtle.right(24)
turtle.fd(30)
turtle.left(10)
turtle.circle(30,110)
turtle.fd(20)
turtle.left(40)
turtle.circle(90,70)
turtle.circle(30,150)
turtle.right(30)
turtle.fd(15)
turtle.circle(80,90)
turtle.left(15)
turtle.fd(45)
turtle.right(165)
turtle.fd(20)
turtle.left(155)
turtle.circle(150,80)
turtle.left(50)
turtle.circle(150,90)
turtle.end_fill()
# 花瓣1
turtle.left(150)
turtle.circle(-90,70)
turtle.left(20)
turtle.circle(75,105)
turtle.setheading(60)
turtle.circle(80,98)
turtle.circle(-90,40)
# 花瓣2
turtle.left(180)
turtle.circle(90,40)
turtle.circle(-80,98)
turtle.setheading(-83)
# 叶子1
turtle.fd(30)
turtle.left(90)
turtle.fd(25)
turtle.left(45)
turtle.fillcolor("green")
turtle.begin_fill()
turtle.circle(-80,90)
turtle.right(90)
turtle.circle(-80,90)
turtle.end_fill()
turtle.right(135)
turtle.fd(60)
turtle.left(180)
turtle.fd(85)
turtle.left(90)
turtle.fd(80)
# 叶子2
turtle.right(90)
turtle.right(45)
turtle.fillcolor("green")
turtle.begin_fill()
turtle.circle(80,90)
turtle.left(90)
turtle.circle(80,90)
turtle.end_fill()
turtle.left(135)
turtle.fd(60)
turtle.left(180)
turtle.fd(60)
turtle.right(90)
turtle.circle(200,60)
turtle.seth(60)
turtle.fd(100)
turtle.seth(-60)
turtle.fd(100)
turtle.seth(60)
turtle.fd(100)
turtle.seth(-60)
turtle.fd(100)
turtle.seth(60)
turtle.fd(50)
turtle.seth(0)
turtle.fd(50)
turtle.left(120)
turtle.fd(50)
turtle.seth(-120)
turtle.fd(50)
turtle.seth(0)
turtle.fd(50)
turtle.seth(-60)
turtle.fd(50)
turtle.done()
turtle.stamp()
字符串函数使用简例:
>>> 'HELLO,WRITER'.lower()
'hello,writer'
>>> 'hello,world'.upper()
'HELLO,WORLD'
>>> 'hello'.isupper()
False
>>> 'hello,world'.islower()
True
>>>
>>> 'hello,i love you!'.isalpha()
False
>>> 'i love you'.isalpha()
False
>>> 'hello'.isalpha()
True
>>> ' \n '.isspace()
True
>>> 'hello world'.startswith('hello')
True
>>> 'hello,world'.endswith('world')
True
>>> 'hello,world'.endswith('ld')
True
>>> ','.join(['cats','rats','bats'])
'cats,rats,bats'
>>> '-'.join(['h','e','l','l','o'])
'h-e-l-l-o'
>>> spam='''hello world
ni hao
nice to meet you
'''
>>> spam.split('\n')
['hello world', 'ni hao', 'nice to meet you', '']
>>> 'eeeeeeeeeeeeeeeeeeeeee'.count('e')
22
>>> 'hello world'.rjust(20,'=')
'=========hello world'
>>> 'hello world'.ljust(20,'=')
'hello world========='
>>> 'hello world'.center(20,'=')
'====hello world====='
>>> ' hello world '.lstrip(' ')
'hello world '
>>> 'hello world'.strip(' ')
'hello world'
time库
- time.time()
>>> import time
>>> time.time()
1534498794.9012585
>>>
- time.ctime()
>>> time.ctime()
'Fri Aug 17 17:41:21 2018'
- time.gmtime()
>>> time.gmtime()
time.struct_time(tm_year=2018, tm_mon=8, tm_mday=17, tm_hour=9, tm_min=41, tm_sec=28, tm_wday=4, tm_yday=229, tm_isdst=0)
- time.sleep()
>>> for i in range(5): 以渐变的形式打印出来
print(i)
time.sleep(i)
0
1
2
3
4
- datetime.datetime.now()
>>> import datetime
>>> datetime.datetime.now()
datetime.datetime(2018, 8, 17, 17, 45, 43, 508754)
- datetime.datetime()
>>> datetime.datetime(2018,7,7,7,7,7)
datetime.datetime(2018, 7, 7, 7, 7, 7)
- datetime.datetime.fromtimestamp()
>>> datetime.datetime.fromtimestamp(time.time())
datetime.datetime(2018, 8, 17, 17, 53, 35, 903952)
>>> datetime.datetime.fromtimestamp(10000000)
datetime.datetime(1970, 4, 27, 1, 46, 40)
timedelta数据类型
>>> delta=datetime.timedelta(days=11,hours=12,minutes=15,seconds=11)
>>> delta.days
11
>>> delta.seconds
44111
>>> delta.total_seconds()
994511.0
>>> str(delta)
'11 days, 12:15:11'
>>>
- strftime()
>>> q=datetime.datetime(2015,10,10,10,10,10)
>>> q.strftime('%Y/%m/%d %H:%M:%S')
'2015/10/10 10:10:10'
>>> q.strftime('%I:%M %p')
'10:10 AM'
>>> q.strftime('%B OF %y')
'October OF 15'
>>>
- strptime()
>>> datetime.datetime.strptime('2015/11/11 16:29:21','%Y/%m/%d %H:%M:%S')
datetime.datetime(2015, 11, 11, 16, 29, 21)
实例四:文本进度条
#TextProBarV3.py
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,'-'))
- 列表内容
实例五:天天向上
day=input()
day=eval(day)
q=w=1
for i in range(365):
q=q*(1+day*0.001)
for j in range(365):
w=w*(1-day*0.001)
s=q//w
print('{:.2f}'.format(q),'{:.2f}'.format(w),'{:.0f}'.format(s),sep=',')
实例六:凯撒密码
temp=input()
a=len(temp)
b=''
for i in range(a):
c=ord(temp[i])
if c==32: 空格值为32
b+=' '
else :a
d=(c-97+3)%26
r=chr(d+97) 97为a的unicode值
b+=r
print(b)
- ord()函数取得每一个字符串的unicode值
- 将每一个字母的unicode—97+3并取余数得到原有字符的+3后的字符
- 最后将Unicode值+97转换为字符值