《趣学Python编程》笔记---第一部分:学习编程(2)

来源:互联网 发布:php 上传工具 编辑:程序博客网 时间:2024/05/22 00:19

第九章 Python的内建函数

9.1.1 abs函数 –绝对值函数

print(abs(-10))10

9.1.2 bool函数
根据参数的值返回真或者假,
当数字使用BOOL函数,0返回假,其余都为真
当对其它类型,比如没有值的字符串(none或者空字符),空的列表/元祖/字典,返回假

>>> print(bool(0))False>>> print(bool(1))True>>> print(bool(None))False>>> print(bool())False

下面这段代码,先用input来使得别人在键盘上输入的东西保存在变量year中,假此时直接按下回车,rstrip函数用来删除字符串后的空白和回车,此时bool返回值为False,然后根据if not 判断,执行

year=input('year of birth')if not bool(year.rstrip()):    print("you must enter your birth")

9.1.3 dir函数 —类似一个查找和解释说明函数
9.1.4 eval函数—估值函数
作用:把一个字符串作为参数并返回它作为一个Python表达式的结果

your_calculation=input("enter a calculation:\n")eval(your_calculation)结果:输入5*24 输出624

9.1.5 exec函数
与eval函数差不多,但可以运行更复杂的陈旭。两者不同在于eval返回一个值,儿exec则不会。—–构建读程序的程序

my_small_program='''print('hanm')print('sandwich')'''exec(my_small_program)

9.1.6/7/8 float函数/int函数/len函数
len函数返回一个字符串的长度,或者列表与元祖中元素的个数


9.1.9 max和min函数
9.1.10 range函数
range(X,Y,Z)
X= 开始 Y=结束 Z=步长(间隔)

9.1.11 sum函数
把列表中的元素加在一起

9.2用Python读写文件
读写

test_file=open("位置")text=test_file.read()print(text)test_file=open("位置",'w')test_file.write("内容")test_file.close()

第十章 常用的Python模块

10.1 使用Copy模块来复制
copy函数是一个浅拷贝,并没有拷贝所有对象
而deepcopy则会拷贝所有对象,之后原内容改变不会改变拷贝后的内容
相当于
copy—拷贝书本目录,书中内容不拷贝
deep—拷贝全书


10.2 keyword模块记录了所有关键字
函数iskeyword 判断是否为python关键字
函数kwlist包含所有关键字

>>> import keyword>>> print(keyword.iskeyword('if'))True>>> print(keyword.kwlist )['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']

10.3 用random模块获得随机数

10.3.1 用randint来随机挑选一个数字

一个无聊的猜数字游戏import randomnum=random.randint(1,100)while True:    print("guess a number between 1 and 100")    guess=input()    i=int(guess)    if i==num:        print("you guessed right")        break    elif i<num:        print("try highter")    elif i>num:        print("try lower")

10.3.2 用choice用列表中随机选取一个元素
10.3.3 用shuffle来给列表洗牌


10.4 用sys模块来控制shell程序
1.stdin.readline() 用来读取一行输入,直到回车结束,可以在括号内添加数字以控制输入的多少

import sysv=sys.stdin.readline()he who laughs>>> print(v)he who laughs>>> v=sys.stdin.readline(3)hfkasjdhfkjash>>> print(v)hfk

2.stdout对象用来输出(类似于打印),并返回输出的字符个数

sys.stdout.write("what does a fish say")what does a fish say20

10.5 用time模块来得到时间

>>> import time>>> print(time.time())1489400722.1908722>>> 

这里显示的是从1970.1.1 00:00:00 以来的秒数
一般这个time.time() 用来算时间间隔是好的

10.5.1 用asctime来转换日期(转换成可读形式)

>>> print(time.asctime())Mon Mar 13 18:27:58 2017

10.5.2 用localtime来得到日期和时间

>>> print(time.localtime())time.struct_time(tm_year=2017, tm_mon=3, tm_mday=13, tm_hour=18, tm_min=35, tm_sec=37, tm_wday=0, tm_yday=72, tm_isdst=0)指定位置,打印>>> t=time.localtime()>>> year=t[0]>>> print(year)2017

10.5.3 用sleep 来休息一下
以下代码为慢速打印

>>> for x in range(1,20):    print(x)    time.sleep(1)

10.6 用pickle模块来保存信息

>>> import pickle>>> game_data={'money':158.50}>>> save_file=open("save.dat",'wb')>>> pickle.dump(game_data,save_file)>>> save_file.close()>>> load_file=open("save.dat",'rb')>>> loaded_game=pickle.load(load_file)>>> print(loaded_game){'money': 158.5}>>> 
0 0
原创粉丝点击