1.easygui安装:
下载后用cmd切换到解压文件的目录下,再输入C:\Python36\python3.exe setup.py install,完成安装
2.简单使用:
>>> import easygui
>>> easygui.msgbox('火星人,你好')
'OK'
>>> from easygui import*
>>> msgbox('火星人,你好')
'OK'
>>> import easygui as g
>>> g.msg
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
g.msg
AttributeError: module 'easygui' has no attribute 'msg'
>>> g.msgbox('火星人 你好')
'OK'
>>>
3.迭代器:提供迭代方法的容器叫迭代器
iter()和next()
>>> string = 'fishc'
>>> it = iter(string)
>>> next(it)
'f'
>>> next(it)
'i'
>>> next(it)
's'
>>> string = 'fishc'
>>> it = iter(string)
>>> while True:
try:
each = next(it)
except StopIteration:
break
print(each)
f
i
s
h
c
>>> for each in string:
print(each)
f
i
s
h
c
>>>
4.生成器:为了使代码更加简洁yield,协同程序的实现:函数可以暂停或者挂起,并在需要的时候从程序离开的位置继续或者重新开始
yiled =return 返回这个数据后,就停留这个位置,下次从这个位置继续
>>> def my():
print('生成器执行!')
yield 1
yield 2
>>> myg = my()
>>> next(my)
Traceback (most recent call last):
File "<pyshell#30>", line 1, in <module>
next(my)
TypeError: 'function' object is not an iterator
>>> next(myg)
生成器执行!
1
>>> next(myg)
2
>>> next(my)
Traceback (most recent call last):
File "<pyshell#33>", line 1, in <module>
next(my)
TypeError: 'function' object is not an iterator
>>>
5.模块
容器对数据 函数对语句 类对方法和属性的封装
模块就是程序
可以写很多个.py文件,可以用import引入,然后就可以调用多个文件中的函数
导入模块的两种方法:
1.import 模块名
2.from 模块名 import 函数名
3.import 模块名 as 任意名
import Temperature
print('32摄氏度 = %.2f华氏度'%Temperature.c2f(32))
print('99华氏度 = %.2f摄氏度'%Temperature.f2c(99))
from Temperature import c2f,f2c
print('32摄氏度 = %.2f华氏度'%c2f(32))
print('99华氏度 = %.2f摄氏度'%f2c(99))
================ RESTART: C:/Python36/code/texttemperature.py ================
32摄氏度 = 89.60华氏度
99华氏度 = 37.22摄氏度
>>>
import Temperature as tc
print('32摄氏度 = %.2f华氏度'%tc.c2f(32))
print('99华氏度 = %.2f摄氏度'%tc.f2c(99))
if __name__ == '__main__':
test()
如何将测试程序和试验程序区分开?
def c2f(cal):
fah = cal * 1.8 + 32
return fah
def f2c(fah):
cal = (fah - 32) / 1.8
return cal
def test():
print("测试,0摄氏度= %.2f华氏度" % c2f(0));
if __name__ == '__main__':
test()
直接运行这个:
测试,0摄氏度= 32.00华氏度
>>>
运行调用他的程序:
import Temperature as tc
print('32摄氏度 = %.2f华氏度'%tc.c2f(32))
print('99华氏度 = %.2f摄氏度'%tc.f2c(99))
32摄氏度 = 89.60华氏度
99华氏度 = 37.22摄氏度
>>>