自行CSDN或B站查询,安装好python以及pycharm
message="Hello python world!"
print(message)
message="hollo world"
print(message)
运行这个程序,将看到这两行输出:
Hello python world!
hollo world
在程序中可随时改变量的值,而Python将始终记录变量的最新值。
在python中使用变量时,需要遵守一些规则和指南。违反这些规则将引发错误,而指南旨在让你的代码更容易阅读和理解。
字符串就是一系列字符。在python中,用引号括起来的都是字符串,其中的引号可以是单引号,也可以是双引号,如下所示:
'this is a string'
"this is also a string"
name ="zhouPH"
print(name.title()) #首字母大写
print(name.upper()) #字符串全部大写
print(name.lower()) #字符串全部小写
这些代码输出如下:
Zhouph
ZHOUPH
zhouph
python使用加号(+)来合并字符串。
first_name = "ph"
last_name ="zhou"
fullname = first_name+" "+last_name
print(fullname)
这些代码输出如下:
ph zhou
print("languages:\n\tpython\n\tc\n\tjavaScript")
这些代码输出如下:
languages:
python
c
javaScript
language=' python '
print(language.rstrip()) #删除末尾空白
print(language.lstrip()) #删除开头空白
print(language.strip()) #删除两端空白
这些代码输出如下:
python
python
python
字符串中含撇号,不可用单引号
age = 23
messge = "happy " + str(age) + "rd Birthday"
print(messge)
这些代码输出如下:
happy 23rd Birthday
在python中,注释用#标识。#后面的内容都会被python解释器忽略;
列表是有序集合,因此要访问列表的任何元素,只需将该元素的位置或者索引告诉python即可;
phone = ['iphone','oppo','huawei','vivo']
print(phone[0])
当你请求获取列表元素时,python只返回该元素,而不包含[]和引号:
iphone
在python中,第一个列表的索引为0,而不是1;大多数编程语言都是如此,这与列表操作的底层实现相关;
phone = ['iphone','oppo','huawei','vivo']
print(phone[1])
print(phone[3])
print(phone[-2])
这些代码返回列表的的第2个元素和第4个元素以及倒数第2个元素:
oppo
vivo
huawei
你创建的大多数列都将是动态的,这意味这列表创建后,将随着程序的运行增删元素;
phone = ['iphone','oppo','huawei','vivo']
phone[0]= 'ibanana'
print(phone)
这些代码输出如下:
[‘ibanana’, ‘oppo’, ‘huawei’, ‘vivo’]
你可以修改任何列表元素的值,而不仅仅是第一个元素的值;
1、在列表末尾添加元素
phone = ['iphone','oppo','huawei','vivo']
phone.append('ibanana')
print(phone)
这些代码输出如下:
[‘iphone’, ‘oppo’, ‘huawei’, ‘vivo’, ‘ibanana’]
2、在列表中插入元素
使用insert()可在列表的任何位置添加新元素;为此你需要制定新元素的索引和值;
phone = ['iphone','oppo','huawei','vivo']
phone.insert(0,'ibanana')
print(phone)
这些代码输出如下:
[‘ibanana’, ‘iphone’, ‘oppo’, ‘huawei’, ‘vivo’]
3、从列表中删除元素
1.使用del语句删除元素
phone = ['iphone','oppo','huawei','vivo']
del phone[0]
print(phone)
这些代码输出如下:
[‘oppo’, ‘huawei’, ‘vivo’]
2、使用方法pop删除元素
phone = ['iphone','oppo','huawei','vivo']
xx_phone = phone.pop()
print(phone) #远列表已删除最后一个值
print(xx_phone) #被删除的值保留在变量xx_phone中
这些代码输出如下:
[‘iphone’, ‘oppo’, ‘huawei’]
vivo
[‘iphone’, ‘oppo’, ‘huawei’]
3、弹出列表中任何位置的元素
phone = ['iphone','oppo','huawei','vivo']
first_phone = phone.pop()
print('the first phone I owned was a '+first_phone.title()+'.')
当你使用pop()时,被弹出的元素就不再在列表中了,这些代码输出如下:
the first phone I owned was a Vivo.
4、根据值删除元素
phone = ['iphone','oppo','huawei','vivo']
phone.remove('huawei') #确定’huawei‘出现在列表什么位置,并将该元素删除;
print(phone)
这些代码输出如下:
[‘iphone’, ‘oppo’, ‘vivo’]
创建的列表中,元素的排序常常是无法预测的;python提供了很多组织列表的方式,可以根据具体情况使用;
cars=['bwm','audo','toyata','Cadillac']
cars.sort()
print(cars)
cars.sort(reverse=True)
print(cars)
这些代码输出如下:
[‘Cadillac’, ‘audo’, ‘bwm’, ‘toyata’]
[‘toyata’, ‘bwm’, ‘audo’, ‘Cadillac’
cars=['bwm','audo','toyata','Cadillac']
print('here is the original list: ')
print(cars)
print("\nhere is the sorted list: ")
print(sorted(cars))
print("\nhere is the original list again")
print(cars)
这些代码输出如下:
here is the original list:
[‘bwm’, ‘audo’, ‘toyata’, ‘Cadillac’]here is the sorted list:
[‘Cadillac’, ‘audo’, ‘bwm’, ‘toyata’]here is the original list again
[‘bwm’, ‘audo’, ‘toyata’, ‘Cadillac’]
方法revers()永久性地修改元素地排列顺序,但可以恢复到原来地排序顺序,再次调用reverse()即可;
cars=['bwm','audo','toyata','Cadillac']
print(cars)
cars.reverse()
print(cars)
这些代码输出如下:
[‘bwm’, ‘audo’, ‘toyata’, ‘Cadillac’]
[‘Cadillac’, ‘toyata’, ‘audo’, ‘bwm’]
使用函数len()可快速获取列表地长度;
cars=['bwm','audo','toyata','Cadillac']
print(len(cars))
这些代码输出如下:
4
注意:python计算列表元素数时从1开始,因此确认列表长度时,你应该不会遇到差一错误;
3.4 使用列表时避免索引错误
cars=['bwm','audo','toyata','Cadillac']
print(cars[3])
**注意:列表索引差一的特征,**这些代码输出如下:
Cadillac
用for循环来打印菜单:
meau = ['饺子','小笼包','汤包','油条','烧饼','馄饨']
for food in meau:
print(food)
这些代码输出如下:
饺子
小笼包
汤包
油条
烧饼
馄饨
for food in meau:
这行代码让python获取列表meau中地第一个值(‘饺子’),并将其存储到变量food中;
接下来,python读取下一行代码:
print(food)
它会让python打印的值——依然是’饺子’;鉴于该列表还包含其他值,python返回到循环的第一行:
for food in meau:
python获取列表中下一个名字——‘小笼包’,并将它存储到food中,在执行下面这行代码:
print(food)
python再次打印变量food的值;接下来执行这个循环;
对列表最后一个值 '馄饨’进行处理,至此,列表再无其他值,因此python接着执行下一行代码,在示例中,for循环没有其他代码,因此程序就此结束。
meau = ['饺子', '小笼包', '汤包', '油条', '烧饼', '馄饨']
for food in meau:
print(food + ', 真是太美味了!!!')
这些代码输出如下:
饺子, 真是太美味了!!!
小笼包, 真是太美味了!!!
汤包, 真是太美味了!!!
油条, 真是太美味了!!!
烧饼, 真是太美味了!!!
馄饨, 真是太美味了!!!
for 循环中,想包含多少行代码都可以。实际上,你会发现使用for循环对每个元素执行众多不同的操作很有用。
meau = ['饺子', '小笼包', '汤包', '油条', '烧饼', '馄饨']
for food in meau:
print(food + ', 真是太美味了!!!')
print('哈哈哈哈哈\n')
print('吃了好了东西,撑死我了')
这些代码输出如下:
饺子, 真是太美味了!!!
哈哈哈哈哈小笼包, 真是太美味了!!!
哈哈哈哈哈汤包, 真是太美味了!!!
哈哈哈哈哈油条, 真是太美味了!!!
哈哈哈哈哈烧饼, 真是太美味了!!!
哈哈哈哈哈馄饨, 真是太美味了!!!
哈哈哈哈哈
- 吃了好了东西,撑死我了
在for循环后面,没有缩进的代码都只执行一次,而不会重复执行。
缩进错误:
忘记缩进;
忘记缩进额外的代码行;
不必要的缩进;
循环后不必要的缩进;
遗漏冒号
列表非常适合用于存储数字集合,而python提供了很多工具,可帮助你高效地处理数字列表;
python函数range()让你能轻松地生成一系列的数字;
for value in range(1,5):
print(value)
这些代码输出如下:
1
2
3
4
示例中,range()只是打印数字1~4,编程语言中常见的差一行为的结果;
要创建数字列表,可使用函数list()将range()的结果直接转换为列表;
如果将range()作为list()的参数,输出将为一个数字列表;
numbers = list(range(1,5))
print(numbers)
这些代码输出如下:
[1, 2, 3, 4]
使用range()时,还可以指定步长;
下面代码打印1~10以内的偶数:
even_number = list(range(2,11,2))
print(even_number)
这些代码输出如下:
[2, 4, 6, 8, 10]
打印1~10值的平方,将结果存储到square,再将平方值附加到列表squares末尾
squares = []
for value in range(1,11):
square = value**2
squares.append(square)
print(squares)
这些代码输出如下:
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
digits = list(range(1,11))
print(max(digits))
print(min(digits))
print(sum(digits))
这些代码输出如下:
10
1
55
squares =[value**2 for value in range(1,11)]
print(squares)
你还可以处理列表的部分元素——python称之为切片;
player =['姚明','易建联','米兰中锋','王治郅','科比']
print(player[0:3])
print(player[:4])
print(player[-3:])
print(player[:-1])
这些代码输出如下:
[‘姚明’, ‘易建联’, ‘米兰中锋’]
[‘姚明’, ‘易建联’, ‘米兰中锋’, ‘王治郅’]
[‘米兰中锋’, ‘王治郅’, ‘科比’]
[‘姚明’, ‘易建联’, ‘米兰中锋’, ‘王治郅’]
players =['姚明','易建联','米兰中锋','王治郅','科比']
for player in players[:3]:
print(player)
这些代码输出如下:
姚明
易建联
米兰中锋
my_foods = ['披萨','蛋糕','麦当劳','蛋挞']
friend_foods = my_foods[:]
print(my_foods)
print(friend_foods)
这些代码输出如下:
[‘披萨’, ‘蛋糕’, ‘麦当劳’, ‘蛋挞’]
[‘披萨’, ‘蛋糕’, ‘麦当劳’, ‘蛋挞’]
元组看起来犹如列表,但用圆括号而不是方括号来标识。定义元组后,就可以使用索引来访问其他元素,就像访问列表元素一样;
dimensions = (200 , 50)
print(dimensions)
print(dimensions[0])
dimensions[0] = 100 #TypeError: 'tuple' object does not support item assignment
print(dimensions)
这些代码输出如下:
(200, 50)
200
Traceback (most recent call last):
File “C:\Users\DELL\PycharmProjects\pythonProject\dome4.py”, line 58, in
dimensions[0] = 100
TypeError: ‘tuple’ object does not support item assignment
dimensions\[0\] = 100 试图修改第一个元素的值,导致python返回类型错误消息;
试图修改元组的操作是被禁止的,因此python支持不能给元组的元素赋值;
像列表一样,也可以使用for循环来遍历元组中的所有值:
dimensions = (200 , 50)
for dimension in dimensions:
print(dimension)
这些代码输出如下:
200
50
虽然不能修改元组的元素,但可以给存储元组的变量赋值;
dimensions = (200 , 50)
for dimension in dimensions:
print(dimension)
dimensions = (400 , 80)
for dimension in dimensions:
print(dimension)
这些代码输出如下:
200
50
400
80
cars = ['bwm','dudi','subaru','toyota']
for car in cars:
if car =='bwm':
print(car.upper())
else:
print(car.title())
这些代码输出如下:
BWM
Dudi
Subaru
Toyota
car ='bwm'
print(car == 'bwm')
print(car == 'audi')
这些代码输出如下:
True
False
在python中检查是否相等时区分大小写,例如,两个大小写不同的值会被视为不相等:
car='Audi'
print(car == 'audi')
这些代码输出如下:
False
如果大小写很重要,这种行为有其优点;
但如果大小写无关紧要,而只想检查变量的值,可将变量的值转换为小写,在进行比较;
car='Audi'
print(car.lower() == 'audi')
这些代码输出如下:
True
5.2.3 检查是否不相等
要判断两个值是否不等,可使用(!=),其中!表示**不,**在很多编程语言中都如此;
x1 = 1
if x1 != 2:
print(str(x1)+"这个数字不是2")
这些代码输出如下:
1这个数字不是2
你编写的大多数条件表达式都检查两个字是否相等,但有时候检查两个值是否不等效率更高。
检查数值非常简单,例如,下面的代码检查一个人是否18岁:
age = 18
print(age == 18)
这些代码输出如下:
True
下面代码在提供答应不正确时答应一条消息:
answer = 17
if answer != 42:
print("that is not the correct answer.please try again")
这些代码输出如下:
that is not the correct answer.please try again
条件语句可包含各种数学比较:
age = 26
print(age < 21)
print(age <= 21)
print(age >= 21)
print(age > 21)
print(age != 21)
这些代码输出如下:
False
False
True
True
True
1、 使用and检查多个条件
age_0 = 22
age_1 = 33
print(age_0 >= 21 and age_1 <=22)
print(age_0 >= 21 and age_1 <=44)
这些代码输出如下:
False
True
为改善可读性,建议改成:
age_0 = 22
age_1 = 33
print((age_0 >= 21) and (age_1 <=22))
print((age_0 >= 21) and (age_1 <=44))
2、 使用or检查多个条件
age_0 = 22
age_1 = 18
print((age_0>21)or(age_1>=21))
print((age_0>22)or(age_1>=21))
这些代码输出如下:
True
False
cars = ['bwm','dudi','subaru','toyota']
print('bwm' in cars)
这些代码输出如下:
True
cars = ['bwm','dudi','subaru','toyota']
car ='凯迪拉克'
if car not in cars:
print(car.title()+',这车不在车库里面')
这些代码输出如下:
凯迪拉克,这车不在车库里面
布尔表达式的结果要么为True,要么为False
age =19
if age >= 18:
print("you are old enough to vote!")
这些代码输出如下:
you are old enough to vote!
age =17
if age >= 18:
print("you are old enough to vote!")
else:
print("sorry,you are too young to vote")
这些代码输出如下:
sorry,you are too young to vote
age =12
if age <4:
print("your adminssion cast is $0")
elif age <18:
print("your adminssion cost is $5")
else:
print("your adminssion cost is $10")
这些代码输出如下:
your adminssion cost is $5
age =77
if age <4:
print("your adminssion cast is $0")
elif age <18:
print("your adminssion cost is $5")
elif age <65:
print("your adminssion cost is $10")
else:
print("your adminssion cost is $0")
这些代码输出如下:
your adminssion cost is $0
age =77
if age <4:
print("your adminssion cast is $0")
elif age <18:
print("your adminssion cost is $5")
elif age <65:
print("your adminssion cost is $10")
elif age >65:
print("your adminssion cost is $0")
这些代码输出如下:
your adminssion cost is $0
requested_toppings =['mushrooms','extra cheese']
if 'mushrooms' in requested_toppings:
print("adding mushrooms")
if 'pepperoni' in requested_toppings:
print("adding pepperoni")
if 'extra cheese' in requested_toppings:
print("adding extra cheese")
print("\nFinished making your pizza!")
这些代码输出如下:
sorry,you are too young to vote
your adminssion cost is $0
your adminssion cost is $0
adding mushrooms
adding extra cheeseFinished making your pizza!
requested_toppings =['mushrooms','extra cheese','pepperoni']
for requested_topping in requested_toppings:
print("adding "+requested_topping+".")
print("\nFinished making your pizza!")
这些代码输出如下:
Finished making your pizza!
adding mushrooms.
adding extra cheese.
adding pepperoni.Finished making your pizza!
requested_toppings = []
if requested_toppings:
for requested_topping in requested_toppings:
print("adding " + requested_topping + ".")
print("\nFinished making your pizza!")
else:
print("are you sure want a plain pazzia")
这些代码输出如下:
are you sure want a plain pazzia
available_toppings = ['mushrooms','olives','green peppers','pepperoni','pineapple','extra cheese']
requested_toppings =['mushrooms','extra cheese','pepperoni']
for available_topping in requested_toppings:
if requested_topping in available_toppings:
print("adding "+requested_topping+".")
else:
print("sorry we don't have "+ requested_topping+".")
这些代码输出如下:
adding pepperoni.
adding pepperoni.
adding pepperoni.
PEP8提供的唯一建议:在诸如==、>=和<=比较运算符两边各添加一个空格,例如 if age < 4比if age<4 好
alien_0={'color':'green','point':5}
print(alien_0['color'])
print(alien_0['point'])
这些代码输出如下:
green
5
要获取与键相关联的值,可依次指定字典名和放在方括号内的键,如下所示:
alien_0={'color':'green'}
print(alien_0['color'])
这些代码输出如下:
green
字典中可包含任意数量的键-值对
alien_0={'color':'green','point':5}
new_points = alien_0['point']
print("you just earned " + str(new_points)+"")
这些代码输出如下:
you just earned 5
alien_0={'color':'green','point':5}
alien_0['x_position'] = 0
alien_0['y_position'] = 25
print(alien_0)
这些代码输出如下:
{‘color’: ‘green’, ‘point’: 5, ‘x_position’: 0, ‘y_position’: 25}
注意:
alien_0 = {}
alien_0['color'] = 'green'
alien_0['point'] = '5'
print(alien_0)
这些代码输出如下:
{‘color’: ‘green’, ‘point’: ‘5’}
使用字典俩存储用户提供的数据或在编写能自动生产大量键-值对的代码时,通常都需要先定义一个空字典;
alien_0 = {'color':'green'}
print("the alien is now "+alien_0['color'] + ".")
alien_0['color'] = 'yellow'
print("the alien is now "+alien_0['color'] + ".")
这些代码输出如下:
the alien is now green.
the alien is now yellow.
alien_0={'color':'green','point':5}
print(alien_0)
del alien_0['point']
print(alien_0)
这些代码输出如下:
{‘color’: ‘green’, ‘point’: 5}
{‘color’: ‘green’}
删除的键-值对永久消失了
favorite_languages ={
'jen':'python',
'sarah':'c',
'edward':'ruby',
'pjil':'python'
}
print("sarah's favorite language is "+favorite_languages['sarah'].title()+".")
这些代码输出如下:
sarah’s favorite language is C.
for key,value in user_0.items():
print("\nkey: "+key)
print("key: "+value)
这些代码输出如下:
key: username
key: bananakey: first
key: enricokey: last
key: fermi
favorite_languages ={
'jen':'python',
'sarah':'c',
'edward':'ruby',
'pjil':'python'
}
for name in favorite_languages.keys():
print(name.title())
这些代码输出如下:
Jen
Sarah
Edward
Pjil
friends = ['phil','sarah']
for name in favorite_languages.keys():
print("\n"+name.title())
if name in friends:
print(" hi "+name.title()+", I see your favorite language is "+favorite_languages[name].title()+"!")
这些代码输出如下:
Jen
Sarah
hi Sarah, I see your favorite language is C!Edward
Pjil
favorite_languages ={
'jen':'python',
'sarah':'c',
'edward':'ruby',
'pjil':'python'
}
for name in sorted(favorite_languages.keys()):
print(name.title()+" ,thank you fir taking the poll.")
这些代码输出如下:
Edward ,thank you fir taking the poll.
Jen ,thank you fir taking the poll.
Pjil ,thank you fir taking the poll.
Sarah ,thank you fir taking the poll.
如果你感兴趣的主要是字典包含的值,可使用values(),它返回一个值列表,而不包含任何键;
favorite_languages ={
'jen':'python',
'sarah':'c',
'edward':'ruby',
'pjil':'python'
}
print("the following langguages have been mentioned:")
for language in favorite_languages.values():
print(language.title())
这些代码输出如下:
the following langguages have been mentioned:
Python
C
Ruby
Python
favorite_languages ={
'jen':'python',
'sarah':'c',
'edward':'ruby',
'pjil':'python'
}
print("the following langguages have been mentioned:")
for language in set(favorite_languages.values()):
print(language.title())
这些代码输出如下:
the following langguages have been mentioned:
Ruby
Python
C
结果是一个不重复的列表,其中列出了被调查者提及的所有语言。
有时候,需要将一系列字典存储在列表中,或将列表作为值存储在字典中,这称为嵌套;
alien_0 = {'color':'green','points':5}
alien_1 = {'color':'yellow','points':10}
alien_2 = {'color':'red','points':15}
aliens =[alien_0,alien_1,alien_2]
for alien in aliens:
print(alien)
这些代码输出如下:
{‘color’: ‘green’, ‘points’: 5}
{‘color’: ‘yellow’, ‘points’: 10}
{‘color’: ‘red’, ‘points’: 15}
用range()生成30个外星人:
aliens =[]
for alien_number in range(30):
new_alien = {'color':'green','points':5,'speed':'slow'}
aliens.append(new_alien)
#显示前五个外星人
for alien in aliens[:5]:
print(alien)
print("tatal bumber if aliens: "+ str(len(aliens)))
这些代码输出如下:
{‘color’: ‘green’, ‘points’: 5, ‘speed’: ‘slow’}
{‘color’: ‘green’, ‘points’: 5, ‘speed’: ‘slow’}
{‘color’: ‘green’, ‘points’: 5, ‘speed’: ‘slow’}
{‘color’: ‘green’, ‘points’: 5, ‘speed’: ‘slow’}
{‘color’: ‘green’, ‘points’: 5, ‘speed’: ‘slow’}
tatal bumber if aliens: 30
修改前三个外星人为黄色、速度中且值10个点:
aliens =[]
for alien_number in range(30):
new_alien = {'color':'green','points':5,'speed':'slow'}
aliens.append(new_alien)
for alien in aliens[0:3]:
if alien['color'] == 'green':
alien['color'] = 'yellow'
alien['point'] = 10
alien['speed'] = 'medium'
for alien in aliens[0:5]:
print(alien)
这些代码输出如下:
{‘color’: ‘yellow’, ‘points’: 5, ‘speed’: ‘medium’, ‘point’: 10}
{‘color’: ‘yellow’, ‘points’: 5, ‘speed’: ‘medium’, ‘point’: 10}
{‘color’: ‘yellow’, ‘points’: 5, ‘speed’: ‘medium’, ‘point’: 10}
{‘color’: ‘green’, ‘points’: 5, ‘speed’: ‘slow’}
{‘color’: ‘green’, ‘points’: 5, ‘speed’: ‘slow’}
有时候,需要将列表存储在字典中,而不是字典存储在列表中;
可在字典中嵌套字典,但这样做,代码可能很快复杂起来;
例如:如果有多个网址用户,每个人都有独特的用户名,可在字典中将用户名作为键,然后将每位用户的信息存储到一个字典中,并将改字典作为用户名相关联的值。
users = {
'aeinstein' : {
'first' : 'albert',
'last' : 'einstein',
'location' : 'princeton',
} ,
'mcurie' : {
'first' : 'marie',
'last' : 'curie',
'location' : 'paris'
}
}
for username , user_info in users.items():
print("\nUsername: "+ username)
full_name = user_info['first'] + " " + user_info['last']
location = user_info['location']
print("\tFull name: " + full_name.title())
print("\tLocation: " + location.title())
这些代码输出如下:
Username: aeinstein
Full name: Albert Einstein
Location: PrincetonUsername: mcurie
Full name: Marie Curie
Location: Paris
message = input("tell me something, and I will repeat it back to you: ")
print(message)
这些代码输出如下:
tell me something, and I will repeat it back to you: 所有的伟大都源于一次勇敢的开始
所有的伟大都源于一次勇敢的开始
函数input() 接受一个参数:即要向用户显示的提示或说明,让用户知道改如何做;
每当你使用函数input()时,都应该指定清晰而易于明白的指示,准确地指出你希望用户提供什么样地信息——任何指出用户改输入何种信息地提示都行,如下所示:
name = input("please enter your name :")
print("hello, "+name + "!")
这些代码输出如下:
please enter your name :周周周
hello, 周周周!
使用函数int()时,python将用户输入读为字符串。请看下面让用户输入其年龄地解释器会话:
age = input("how old are you? ")
print(age>21)
这些代码输出如下:
how old are you? 21
Traceback (most recent call last):
File “C:\Users\DELL\PycharmProjects\pythonProject\demo7.py”, line 8, in
print(age>21)
TypeError: ‘>’ not supported between instances of ‘str’ and ‘int’
age = input("how old are you? ")
age = int(age)
print(print(age>21))
这些代码输出如下:
how old are you? 21
False
处理数值信息时,求模运算符(%)是一个很有用的工具,它将两个数相除并返回余数;
print(4%3)
print(7%3)
print(8%3)
print(9%3)
print(10%3)
这些代码输出如下:
1
1
2
0
1
number = input('enter a number,and I will tell you if it is even or odd: ')
number = int(number)
if number%2 == 0:
print("\nthe number " +str(number) + "is even")
else:
print("\nthe number " +str(number) + "is odd ")
这些代码输出如下:
enter a number,and I will tell you if it is even or odd: 9999
the number 9999 is odd
for循环用于针对集合中的每个元素的一个代码块,而while循环不断地运行,直到指定地条件不满足为止;
number = 1
while number <= 5:
print(number)
number +=1
这些代码输出如下:
1
2
3
4
5
number = 1
while number <= 5:
print(number)
number +=1;
prompt = "\ntell me something, and I will repeat it back to you: "
prompt += "\nEnter 'quit' to end the program ."
message = ""
while message!='quit':
message = input(prompt)
print(message)
这些代码输出如下:
tell me something, and I will repeat it back to you:
Enter ‘quit’ to end the program .哈哈哈哈
哈哈哈哈tell me something, and I will repeat it back to you:
Enter ‘quit’ to end the program .哈哈哈
哈哈哈tell me something, and I will repeat it back to you:
Enter ‘quit’ to end the program .quit
quitProcess finished with exit code 0
在前一个示例中,我们让程序在满足指定条件时就执行特定地任务。但在更复杂地程序中,很多不同地事件都会导致程序停止运行;在这种情况下,该怎么办呢?
在要求很多条件地满足才继续运行地程序中,定义一个变量,用于判断整个程序是否处于活动状态;这个变量称之为标志,充当了程序地交通信号灯。
可以让程序标准为True时继续运行,并在任何事件导致标准为False时让程序停止运行;这样while语句中就只需要检查一个条件——标志地当前值是否为True,并将所有测试测试都放在其他地方,从而让程序编程更为整洁;
prompt = "\ntell me something, and I will repeat it back to you: "
prompt += "\nEnter 'quit' to end the program ."
active = True
while active:
message = input(prompt)
if message == 'quit':
active = False
else:
print(message)
这些代码输出如下:
tell me something, and I will repeat it back to you:
Enter ‘quit’ to end the program .xx
xxtell me something, and I will repeat it back to you:
Enter ‘quit’ to end the program .xxxx
xxxxtell me something, and I will repeat it back to you:
Enter ‘quit’ to end the program .quitProcess finished with exit code 0
prompt = "\nplease enter the name of a city you have visited: "
prompt += "\nEnter 'quit' to end the program ."
while True:
city = input(prompt)
if city == 'quit':
break
else:
print("I'd love to go to "+ city.title() + "!")
这些代码输出如下:
please enter the name of a city you have visited:
Enter ‘quit’ to end the program .杭州
I’d love to go to 杭州!please enter the name of a city you have visited:
Enter ‘quit’ to end the program .上海
I’d love to go to 上海!please enter the name of a city you have visited:
Enter ‘quit’ to end the program .北京
I’d love to go to 北京!please enter the name of a city you have visited:
Enter ‘quit’ to end the program .
注意:在任何Python循环中都可以使用break语句。例如,可使用break语句来退出遍历列表或字典地for循环。
例如从1数到10,值打印其中技术地循环:
number = 0
while number < 10:
number += 1
if number % 2 == 0:
continue
print(number)
这些代码输出如下:
1
3
5
7
9
每个while循环都必须有停止运行的途径,这样才不会没完没了的执行下去;
**注意:**有些编辑器内嵌了输出窗口,这可能导致难以接受无限循环,因此不得不关闭编辑器来结束无限循环。
假设有一个列表,其中包含新注册但还未验证的网站客户;
验证这些用户后,如何将他们移到另一个已验证用户列表中
#首先,创建一个待验证的用户列表
#和一个用于存储已验证用户的空列表
unconfirmed_users = ['alice','brian','candace']
confirmed_users = []
while unconfirmed_users:
current_user = unconfirmed_users.pop()
print("verifying user: " + current_user.title())
confirmed_users.append(current_user)
#显示所有已验证的用户
print("\nthe following user have been confirmed:")
for confirmed_user in confirmed_users:
print(confirmed_user.title())
这些代码输出如下:
the following user have been confirmed:
Candace
Brian
Alice
在第3章,我们使用方法remove()来删除列表中的特定值,这之所以可行,是因为要删除的值再列表中只出现了一次;如果要删除列表中所有包含特定值的元素,该怎么办?
pets = ['dog','cat','dog','goldfish','cat','rabbit','cat']
print(pets)
while 'cat' in pets:
pets.remove('cat')
print(pets)
print(pets)
这些代码输出如下:
[‘dog’, ‘cat’, ‘dog’, ‘goldfish’, ‘cat’, ‘rabbit’, ‘cat’]
[‘dog’, ‘dog’, ‘goldfish’, ‘cat’, ‘rabbit’, ‘cat’]
[‘dog’, ‘dog’, ‘goldfish’, ‘rabbit’, ‘cat’]
[‘dog’, ‘dog’, ‘goldfish’, ‘rabbit’]
[‘dog’, ‘dog’, ‘goldfish’, ‘rabbit’]
可使用while 循环提示用户输入任意数量的信息;
responses ={}
polling_active =True
while polling_active:
name = input("\nwhat is your name? ")
response = input("which moutain would you like to climb somday? ")
responses[name] = response
repeat = input("would you like to let anther person resond?(yes/no)")
if repeat =='no':
polling_active =False
print("\n--- Poll Results ---")
for name, response in responses.items():
print(name +"would like to climb "+ response +".")
这些代码输出如下:
what is your name? 爸爸
which moutain would you like to climb somday? 泰山
would you like to let anther person resond?(yes/no)yeswhat is your name? 妈妈
which moutain would you like to climb somday? 泰山
would you like to let anther person resond?(yes/no)no— Poll Results —
爸爸would like to climb 泰山.
妈妈would like to climb 泰山.
def greet_user():
print("hello 哒哒哒")
greet_user()
这些代码输出如下:
hello 哒哒哒
def greet_username(username):
print("Hello, " + username.title() + "!")
greet_username('dada')
这些代码输出如下:
Hello, Dada!
基本定义
两种情况
值传递和引用传递的基本区别是,进行值传递后,形式参数的值发生改变,实际参数的值不变;而进行应用传递后,形式参数的值发生改变,实际参数的值也一样发生改变。
由此可见值传递和引用传递的关键是可变对象和不可变对象
可变对象和不可变对象
1、调用函数多次
def describe_pet(animal_type,pet_name):
print("\nI have a " + animal_type + ".")
print("My " + animal_type + "'s name is " + pet_name.title() + ".")
describe_pet('hamster','harry')
这些代码输出如下:
I have a hamster.
My hamster’s name is Harry.
2、位置实参的顺序很重要
使用位置实参来调用函数时,如果实参的顺序不正确,结果可能出乎意料;
def describe_pet(animal_type,pet_name):
print("\nI have a " + animal_type + ".")
print("My " + animal_type + "'s name is " + pet_name.title() + ".")
describe_pet(animal_type='hamster',pet_name='harry')
describe_pet(pet_name='wangcai',animal_type='dog')
这些代码输出如下:
I have a hamster.
My hamster’s name is Harry.I have a dog.
My dog’s name is Wangcai.
注意:使用关键字实参时,无比准确地指定函数定义中的形参值。
#使用默认值时,在形参列表中必须先列出没有默认值的形参,在列出有默认值的形参,这让Python依然能够正确地解读位置实参;
def describe_pet(pet_name,animal_type = 'dog'):
print("\nI have a " + animal_type + ".")
print("My " + animal_type + "'s name is " + pet_name.title() + ".")
describe_pet(pet_name='harry')
describe_pet(pet_name='wangcai',animal_type='cat')
这些代码输出如下:
I have a dog.
My dog’s name is Harry.I have a cat.
My cat’s name is Wangcai.
注意:使用默认值时,在形参列表中必须先列出没有默认值的形参,在列出有默认值的形参,这让Python依然能够正确地解读位置实参;
musician = get_formatted_name('jimi','hendrix')
print(musician)
这些代码输出如下:
Jimi Hendrix
有时候,需要让实参变成可选的,这样使用函数的人就只需要在必要是才提供额外的信息;可以使用默认值让实参编程可选的;
def get_formatted_name(first_name,middle_name,last_name):
"""返回整洁的名字"""
full_name = first_name + ' ' + middle_name + ' ' + last_name
return full_name.title()
musician = get_formatted_name('jimi','lee','hendrix')
print(musician)
这些代码输出如下:
Jimi Lee Hendrix
def build_person(first_name,last_name):
person = {'first':first_name,'last':last_name}
return person
musician = build_person('jimi','hendrix')
print(musician)
这些代码输出如下:
{‘first’: ‘jimi’, ‘last’: ‘hendrix’}
可将函数同书本前面介绍任何python结构结合起来使用;
def get_formatted_name(first_name,last_name):
"""返回整洁的名字"""
full_name = first_name + ' ' + last_name
return full_name.title()
while True:
print("\nPlease tell me your name: ")
f_name = input("First name:")
l_name = input("Last name:")
formatted_name = get_formatted_name(f_name,l_name)
print("\nhello, " + formatted_name + "!")
这些代码输出如下:
Please tell me your name:
First name:zhou
Last name:zhouzhouhello, Zhou Zhouzhou!
Please tell me your name:
First name:
def get_formatted_name(first_name,last_name):
"""返回整洁的名字"""
full_name = first_name + ' ' + last_name
return full_name.title()
while True:
print("\nPlease tell me your name: ")
f_name = input("First name:")
if f_name == 'q':
break
l_name = input("Last name:")
if l_name == 'q':
break
formatted_name = get_formatted_name(f_name,l_name)
print("\nhello, " + formatted_name + "!")
这些代码输出如下:
Please tell me your name:
First name:zhou
Last name:zhouzhouhello, Zhou Zhouzhou!
Please tell me your name:
First name:q
def greet_users(names):
"""向列表中的每位用户都发出简单的问候"""
for name in names:
msg = "Hello, " + name.title() + "!"
print(msg)
usernames = ['hannah','ty','margot']
greet_users(usernames)
这些代码输出如下:
Hello, Hannah!
Hello, Ty!
Hello, Margot!
unprinted_designs = ['iphone case','robot pendant','dodecahedron']
completed_models = []
while unprinted_designs:
cuurrent_design = unprinted_designs.pop()
print("priting model: " + cuurrent_design)
completed_models.append(cuurrent_design)
print("\nThe following moedls have been printed:")
for completed_model in completed_models:
print(completed_model)
这些代码输出如下:
priting model: dodecahedron
priting model: robot pendant
priting model: iphone caseThe following moedls have been printed:
dodecahedron
robot pendant
iphone case
def print_models(unprinted_designs,comleted_moedls):
"""
模拟打印每个设计,直到没有未打印的设计为止
打印每个设计后,都将其移到列表completed_models中
"""
while unprinted_designs:
cuurrent_design = unprinted_designs.pop()
print("Printing model: " + cuurrent_design)
completed_models.append(cuurrent_design)
def show_completed_models(completed_models):
"""显示打印好的所有模型"""
print("\nThe following models have been printed: ")
for completed_model in completed_models:
print(completed_model)
unprinted_designs = ['iphone case','robot pandant','dodecaheron']
completed_models = []
print_models(unprinted_designs,completed_models)
show_completed_models(completed_models)
这些代码输出如下:
Printing model: dodecaheron
Printing model: robot pandant
Printing model: iphone caseThe following models have been printed:
dodecaheron
robot pandant
iphone case
有时候,需要禁止函数修改列表;
有时候, 你预先不知道函数需要接受多少个实参,好在python允许函数从调用语句中收集任意数量的实参。
def make_pizza(*toppings):
"""打印顾客点的所有配料"""
print(toppings)
make_pizza('peppereoni')
make_pizza('mushrooms','green peppers','extra cheese')
这些代码输出如下:
(‘peppereoni’,)
(‘mushrooms’, ‘green peppers’, ‘extra cheese’)
现在我们可以将print语句换为一个循环,对配料表进行遍历,并对顾客点的披萨进行描述:
def make_pizza(*toppings):
"""概述要制作的披萨"""
print("\n Making a pizza with the following toppings:")
for topping in toppings:
print("- " + topping)
make_pizza('peppereoni')
make_pizza('mushrooms','green peppers','extra cheese')
这些代码输出如下:
Making a pizza with the following toppings:
- peppereoniMaking a pizza with the following toppings:
- mushrooms
- green peppers
- extra cheese
不管收到的实参是多少个,这种语法都是管用的;
def make_pizza(size,*toppings):
"""概述要制作的披萨"""
print("\nMaking a " + str(size) + "-inch pizza with the following toppings:")
for topping in toppings:
print("- " + topping)
make_pizza(16,"pepperoni")
make_pizza(12,'mushrooms','green peppers','extra cheese')
这些代码输出如下:
Making a 16-inch pizza with the following toppings:
- pepperoniMaking a 12-inch pizza with the following toppings:
- mushrooms
- green peppers
- extra cheese
有时候,需要接受任意数量的实参,但预先不知道传递给函数的会是什么样的信息;
在这种情况下,可将函数编写出能够接受任意数量的键-值对——调用语句提供了多少就接受多少;
def build_profile(first,last,**user_info):
profile = {}
profile['first_name'] = first
profile['last_name'] = last
for key, value in user_info.items():
profile[key] =value
return profile
user_profile = build_profile('albert','einsterin',
loaction = 'princeton',
field = 'physics')
print(user_profile)
这些代码输出如下:
{‘first_name’: ‘albert’, ‘last_name’: ‘einsterin’, ‘loaction’: ‘princeton’, ‘field’: ‘physics’}
pizza.py
def make_pizza(size,*toppings):
print("\nMaking a " + str(size) + "-inch pizza with the following toppongs: ")
for topping in toppings:
print("- " + topping)
import pizza
pizza.make_pizza(16,"peperoni")
pizza.make_pizza(21,'mushrooms','green peppers','extra cheese')
这些代码输出如下:
Making a 16-inch pizza with the following toppongs:
- peperoniMaking a 21-inch pizza with the following toppongs:
- mushrooms
- green peppers
- extra cheese
你还可以导入模块中的特定函数,这种导入方法的语法如下:
from pizza import make_pizza
通过用逗号分隔函数名,可根据需要从模块中导入任意数量的函数:
from pizza import make_pizza,make_pizza2,make_pizza3
如果要导入的函数名称可能与程序中现有的名称冲突,或者函数名称太长,可指定简短而独一无二的别名——函数的另一个名称;
from pizza import make_pizza as mp
mp(16,"peperoni")
mp(21,'mushrooms','green peppers','extra cheese')
这些代码输出如下:
Making a 16-inch pizza with the following toppongs:
- peperoniMaking a 21-inch pizza with the following toppongs:
- mushrooms
- green peppers
- extra cheese
你还可以给模块指定别名;通过给模块指定简短的别名,让你更加轻松调用模块中的函数;
import pizza as p
p.make_pizza(16,"peperoni")
p.make_pizza(21,'mushrooms','green peppers','extra cheese')
这些代码输出如下:
Making a 16-inch pizza with the following toppongs:
- peperoniMaking a 21-inch pizza with the following toppongs:
- mushrooms
- green peppers
- extra cheese
使用星号(*)运算符可让python导入模块中的所有函数:
from pizza import *
make_pizza(16,"peperoni")
make_pizza(21,'mushrooms','green peppers','extra cheese')
这些代码输出如下:
Making a 16-inch pizza with the following toppongs:
- peperoniMaking a 21-inch pizza with the following toppongs:
- mushrooms
- green peppers
- extra cheese
使用类几乎可以模拟任何东西;
根据Dog类创建的每个示例都将存储名字和年龄;我们赋予了每条小狗蹲下(sit())和打滚(roll_over())的能力:
Dog.py
class Dog():
"""一次模拟小狗的简单尝试"""
def __init__(self,name,age):
"""初始化属性name和age"""
self.name = name
self.age = age
def sit(self):
"""模拟小狗被命令是蹲下"""
print(self.name.title() + "is now sitting.")
def roll_over(self):
"""模拟小狗被命令时打滚"""
print(self.name.title() + " rolled over!")
1、方法_init()_
类中的函数称为方法;你前面学到的有关函数的一切都适用于方法,就目前而言,唯一重要的差别是调用方法的方式;
可将类视为有关如何创建实例的说明;
class Dog():
"""一次模拟小狗的简单尝试"""
def __init__(self,name,age):
"""初始化属性name和age"""
self.name = name
self.age = age
def sit(self):
"""模拟小狗被命令是蹲下"""
print(self.name.title() + "is now sitting.")
def roll_over(self):
"""模拟小狗被命令时打滚"""
print(self.name.title() + " rolled over!")
my_dog = Dog('whllie',6)
print("My dos's name is " + my_dog.name.title() + ".")
print("My dog is " + str(my_dog.age) + "years old.")
这些代码输出如下:
My dos’s name is Whllie.
My dog is 6years old.
1、访问属性
要访问实例的属性,可使用句点表示法;
2.调用方法
根据Dog类创建实例后,就可以使用句点来调用Dog类中定义的任何方法
class Dog():
"""一次模拟小狗的简单尝试"""
def __init__(self,name,age):
"""初始化属性name和age"""
self.name = name
self.age = age
def sit(self):
"""模拟小狗被命令是蹲下"""
print(self.name.title() + " is now sitting.")
def roll_over(self):
"""模拟小狗被命令时打滚"""
print(self.name.title() + " rolled over!")
my_dog = Dog('whllie',6)
print("My dos's name is " + my_dog.name.title() + ".")
print("My dog is " + str(my_dog.age) + "years old.")
my_dog = Dog('whille',6)
my_dog.sit()
my_dog.roll_over()
这些代码输出如下:
My dos’s name is Whllie.
My dog is 6years old.
Whille is now sitting.
Whille rolled over!
class Car():
"""一次模拟汽车的简单尝试"""
def __init__(self,make,model,year):
"""初始化描述汽车属性"""
self.make = make
self.model = model
self.year = year
def get_descriptive_name(self):
"""返回整洁的描述性"""
long_name = str(self.year) + ' ' + self.make + ' ' + self.model
return long_name.title()
my_new_car = Car('audi','a4',2016)
print(my_new_car.get_descriptive_name())
这些代码输出如下:
2016 Audi A4
class Car():
"""一次模拟汽车的简单尝试"""
def __init__(self,make,model,year):
"""初始化描述汽车属性"""
self.make = make
self.model = model
self.year = year
self.odometer_reading = 0
def get_descriptive_name(self):
"""返回整洁的描述性"""
long_name = str(self.year) + ' ' + self.make + ' ' + self.model
return long_name.title()
def read_odometer(self):
"""打印一条指出汽车理财的消息"""
print("This car has " + str(self.odometer_reading) + " miles on it")
my_new_car = Car('audi','a4',2016)
print(my_new_car.get_descriptive_name())
my_new_car.read_odometer()
这些代码输出如下:
2016 Audi A4
This car has 0 miles on it
有三种不同的方式修改属性的值:
1、直接修改属性的值
要修改属性的值,最简单的方式是通过实例直接访问到它;
class Car():
"""一次模拟汽车的简单尝试"""
def __init__(self,make,model,year):
"""初始化描述汽车属性"""
self.make = make
self.model = model
self.year = year
self.odometer_reading = 0
def get_descriptive_name(self):
"""返回整洁的描述性"""
long_name = str(self.year) + ' ' + self.make + ' ' + self.model
return long_name.title()
def read_odometer(self):
"""打印一条指出汽车理财的消息"""
print("This car has " + str(self.odometer_reading) + " miles on it")
my_new_car = Car('audi','a4',2016)
print(my_new_car.get_descriptive_name())
my_new_car.odometer_reading = 23
my_new_car.read_odometer()
这些代码输出如下:
2016 Audi A4
This car has 23 miles on it
2、通过方法修改属性的值
如果有替你更新属性的方法,将大有裨益;这样你就午休直接访问属性,而可将值传递给一个方法,由它在内部进行更新;
class Car():
"""一次模拟汽车的简单尝试"""
def __init__(self,make,model,year):
"""初始化描述汽车属性"""
self.make = make
self.model = model
self.year = year
self.odometer_reading = 0
def get_descriptive_name(self):
"""返回整洁的描述性"""
long_name = str(self.year) + ' ' + self.make + ' ' + self.model
return long_name.title()
def update_odometer(self,mileage):
"""将里程表读数设置为指定的值"""
self.odometer_reading = mileage
print("This car has " + str(self.odometer_reading) + " miles on it")
my_new_car = Car('audi','a4',2016)
print(my_new_car.get_descriptive_name())
my_new_car.update_odometer(23)
这些代码输出如下:
2016 Audi A4
This car has 23 miles on it
3、通过方法对属性的值进行递增
class Car():
"""一次模拟汽车的简单尝试"""
def __init__(self,make,model,year):
"""初始化描述汽车属性"""
self.make = make
self.model = model
self.year = year
self.odometer_reading = 0
def get_descriptive_name(self):
"""返回整洁的描述性"""
long_name = str(self.year) + ' ' + self.make + ' ' + self.model
return long_name.title()
def update_odometer(self,mileage):
"""将里程表读数设置为指定的值"""
self.odometer_reading = mileage
print("This car has " + str(self.odometer_reading) + " miles on it")
def increment_odometer(self,miles):
"""将里程表读数设置为指定的值"""
self.odometer_reading += miles
print("This car has " + str(self.odometer_reading) + " miles on it")
my_used_car = Car('SUBARU','OUTBACK',2013)
print(my_used_car.get_descriptive_name())
my_used_car.update_odometer(23500)
my_used_car.increment_odometer(100)
这些代码输出如下:
2013 Subaru Outback
This car has 23500 miles on it
This car has 23600 miles on it
创建子类实例时,python首先需要完成的任务时给父亲的所有属性赋值;为此,子类的方法_init_()需要父类施以援手;
class Car():
"""一次模拟汽车的简单尝试"""
def __init__(self,make,model,year):
"""初始化描述汽车属性"""
self.make = make
self.model = model
self.year = year
self.odometer_reading = 0
def get_descriptive_name(self):
"""返回整洁的描述性"""
long_name = str(self.year) + ' ' + self.make + ' ' + self.model
return long_name.title()
def update_odometer(self,mileage):
"""将里程表读数设置为指定的值"""
self.odometer_reading = mileage
print("This car has " + str(self.odometer_reading) + " miles on it")
def increment_odometer(self,miles):
"""将里程表读数设置为指定的值"""
self.odometer_reading += miles
print("This car has " + str(self.odometer_reading) + " miles on it")
class ElectricCar(Car):
"""电动车的独特之处"""
def __init__(self,make,mddel,year):
"""初始化父类的属性"""
super().__init__(make,mddel,year)
my_tesla = ElectricCar('tesla','model s',2016)
print(my_tesla.get_descriptive_name())
这些代码输出如下:
2016 Tesla Model S
让一个类继承另一个类后,可添加区分子类和父类所需的新属性和方法;
class Car():
"""一次模拟汽车的简单尝试"""
def __init__(self,make,model,year):
"""初始化描述汽车属性"""
self.make = make
self.model = model
self.year = year
self.odometer_reading = 0
def get_descriptive_name(self):
"""返回整洁的描述性"""
long_name = str(self.year) + ' ' + self.make + ' ' + self.model
return long_name.title()
def update_odometer(self,mileage):
"""将里程表读数设置为指定的值"""
self.odometer_reading = mileage
print("This car has " + str(self.odometer_reading) + " miles on it")
def increment_odometer(self,miles):
"""将里程表读数设置为指定的值"""
self.odometer_reading += miles
print("This car has " + str(self.odometer_reading) + " miles on it")
class ElectricCar(Car):
"""电动车的独特之处"""
def __init__(self,make,mddel,year):
"""初始化父类的属性"""
super().__init__(make,mddel,year)
self.battery_size = 70
def describe_battery(self):
"""打印一条描述电瓶容量的消息"""
print("this car has a " + str(self.battery_size) + "-KWh battery.")
my_tesla = ElectricCar('tesla','model s',2016)
print(my_tesla.get_descriptive_name())
my_tesla.describe_battery()
这些代码输出如下:
2016 Tesla Model S
this car has a 70-KWh battery.
注意:注意缩进!注意缩进!注意缩进!
不断给ElectricCar类添加细节时,我们可能会发现其中包含很多专门针对汽车电瓶的属性和方法;
至此,你掌握了编写组织有序而易于使用的程序所需的基本技能,该考虑让程序目标更明确、用途更大了。
文本文件可存储的数据量多的难以置信:天气数据、交通数据、、社会经济数据、文学作品等;
pi.digits
3.1415926535 8979323846 2643383279
with open('pi_digits') as file_object:
contents = file_object.read()
print(contents)
当你将类似pi_digit这样的简单文件名传递给函数open()时,Python将在执行的文件所载的目录中查询文件;
在Windows系统中,在文件路径中使用反斜杠(\)而不是(/):
#coding=utf8
with open(r'C:\Users\DELL\Desktop\pi_digits.txt') as file_object:
contents = file_object.read()
print(contents)
这些代码输出如下:
3.1415926535
8979323846
2643383279
读取文件时,常常需要检查其中每一行:
#coding=utf8
file_name = (r'C:\Users\DELL\Desktop\pi_digits.txt')
with open(file_name) as file_object:
for line in file_object:
print(line)
这些代码输出如下:
3.1415926535
8979323846
2643383279
我们打印每一行时,发现空白行更多了;
#coding=utf8
file_name = (r'C:\Users\DELL\Desktop\pi_digits.txt')
with open(file_name) as file_object:
for line in file_object:
print(line.rstrip())
这些代码输出如下:
3.1415926535
8979323846
2643383279
#coding=utf8
file_name = (r'C:\Users\DELL\Desktop\pi_digits.txt')
with open(file_name) as file_object:
lines = file_object.readlines()
for line in lines:
print(line.rstrip())
这些代码输出如下:
3.1415926535
8979323846
2643383279
将文件读取到内存中后,就可以以任何方式使用这些数据了;
#coding=utf8
file_name = (r'C:\Users\DELL\Desktop\pi_digits.txt')
with open(file_name) as file_object:
lines = file_object.readlines()
pi_string = ''
for line in lines:
pi_string += line.rstrip()
print(pi_string)
print(len(pi_string))
这些代码输出如下:
3.1415926535 8979323846 2643383279
38
注意 读取文本文件时,Python将其中的所有文本都解读为字符串,如果你读取的时数字,并要将其作为数值使用,就必须使用函数int()将其转换为整数,或使用函数float()将其转化为浮点数
#coding=utf8
file_name = (r'C:\Users\DELL\Desktop\python_work\《Python编程》源代码文件\chapter_10\pi_million_digits.txt')
with open(file_name) as file_object:
lines = file_object.readlines()
pi_string = ''
for line in lines:
pi_string += line.rstrip()
birthday = input("Enter your birthday,in the form mmddyy: ")
if birthday in pi_string:
print("your birthday appears in the first million digits of pi!")
else:
print("your birthday does not appear in the first million digits of pi")
这些代码输出如下:
Enter your birthday,in the form mmddyy: 19950813
your birthday does not appear in the first million digits of pi
要将文本写入文件 ,你在调用open()时需要另一个实参,告诉Python你要写入打开的文件;
明白其中的工作原理,我们来将一条简单的消息存储到文件中,而不是将其打印到屏幕上:
filename = 'programing.txt'
with open(filename,'w') as file_object:
file_object.write("I lvoe programing.")
这些代码输出如下:
programing.txt
I lvoe programing.
函数write()不会在你写入的文本末尾添加换行符,因此如果你写入多行时没用指定换行符,文件看起来可能不是你希望的那样;
filename = 'programing.txt'
with open(filename,'w')as file_object:
file_object.write("I love programing.")
file_object.write("I love creating new games.")
这些代码输出如下:
programing.txt
I love programing.I love creating new games.
如果你打开programing.txt,将发现两行内容挤在一起;
要让每个字符串都单独占用一行,需要在write()语句中包含换行符:
filename = 'programing.txt'
with open(filename,'w')as file_object:
file_object.write("I love programing.\n")
file_object.write("I love creating new games.\n")
这些代码输出如下:
programing.txt
I love programing. I love creating new games.
像显示到终端输出一样,还可以使用空格、制表符和空行来设置这些输出的格式;
filename = 'programing.txt'
with open(filename,'a') as file_object:
file_object.write("I also lvoe finding meaning in large datasets.\n")
file_object.write("I love creating apps that can run in a brower.\n")
programing.txt
I love programing. I love creating new games. I also lvoe finding meaning in large datasets. I love creating apps that can run in a brower.
最终的结果是,文件原来的内容还在,它们后面是我们刚添加的内容;
print(5/0)
显然,Python无法这样做,因此你将会看到一个traceback:
Traceback (most recent call last):
File “C:\Users\DELL\PycharmProjects\pythonProject\hollo_world.py”, line 1, in
print(5/0)
ZeroDivisionError: division by zero
当你认为可能发生了错误时,可编写一个try-except代码块来处理可能引发的异常;
print("give me two numbers , I'll divide them.")
print("Enter 'q' to quit.")
while True:
first_number = input("\n First number: ")
if first_number == 'q':
break
second_number = input("\n Second number:")
if second_number == 'q':
break
answer = int(first_number) / int(second_number)
print(answer)
这些代码输出如下:
give me two numbers , I’ll divide them.
Enter ‘q’ to quit.First number: 88
Second number:0
Traceback (most recent call last):
File “C:\Users\DELL\PycharmProjects\pythonProject\hollo_world.py”, line 10, in
answer = int(first_number) / int(second_number)
ZeroDivisionError: division by zero
print("give me two numbers , I'll divide them.")
print("Enter 'q' to quit.")
while True:
first_number = input("\n First number: ")
if first_number == 'q':
break
second_number = input("\n Second number:")
try:
answer = int(first_number) / int(second_number)
except ZeroDivisionError:
print("you can't divide by 0")
else:
print(answer)
这些代码输出如下:
give me two numbers , I’ll divide them.
Enter ‘q’ to quit.First number: 99
Second number:0
you can’t divide by 0First number: 5
Second number:2
2.5First number: q
filename = 'alice.txt'
with open(filename) as f-obj:
contents = f_obj.read()
这些代码输出如下:
File “C:\Users\DELL\PycharmProjects\pythonProject\demo12.py”, line 2
with open(filename) as f-obj:
^^^^^
SyntaxError: cannot assign to expression
你可以分享包含整本书地文本文件;很多经典地文学作品都是以简单地文本文件地方式提供地,因为他们不受版权限制;
#coding=utf8
filename = r'C:\Users\DELL\Desktop\xxx.txt'
try:
with open(filename,"rb") as f_obj:
contents = f_obj.read()
except FileNotFoundError:
msg = "Sorry, the file " + filename + "does not exist."
print(msg)
else:
words = contents.split()
num_words = len(words)
print("the file " + "has about " + str(num_words) + "words.")
这些代码输出如下:
the file has about 45410words.
模块json让你能够将简单的python数据结构转储到文件中,并在程序再次运行时加载该文件中的数据;
注意:json(JavaScript object notation)格式最初是为JavaScript开发的,但随后成了一种常见格式,被包括在python在内的众多语言采用;
函数json.dump接受了两个实参:要存储的数据以及可用于存储数据的文件对象:
import json
numbers = [2,3,5,7,11,13]
filename = 'numbers.json'
with open(filename,'w')as f_obj:
json.dump(numbers,f_obj)
这些代码输出如下:
number.json
\[2, 3, 5, 7, 11, 13\]
对于用户生成的数据,使用json保存它们大有裨益,因为如果不以某种方式进行存储,等程序停止运行时用户的信息将都丢失;
import json
username = input("what is your name ? ")
filename = 'username.json'
with open(filename,'w')as f_obj:
json.dump(username,f_obj)
print("We'll remember you when you come back," + username + "!")
这些代码输出如下:
username.json
"zhouzhouzhou"
import json
def greet_user():
"""问候用户,并指出其姓名"""
filename = 'username.json'
try:
with open(filename) as f_obj:
username = json.load(f_obj)
except FileNotFoundError:
username = input("what is your name? ")
with open(filename,'w') as f_obj:
json.dump(username,f_obj)
print("We'll remnmber you when you came back," + username + "!")
else:
print("Welcome back, " + username + "!")
greet_user()
这些代码输出如下:
Welcome back, zhouzhouzhou!
import json
def get_stored_username():
"""如果存储了用户名,就获取它"""
filename = 'username.json'
try:
with open(filename) as f_obj:
username = json.load(f_obj)
except FileNotFoundError:
return None
else:
return username
def greet_user():
"""问候用户,并指出其姓名"""
username = get_stored_username()
if username:
print("Welcome back, " + username + "!")
else:
username = input("what is your name? ")
filename = 'username.json'
with open(filename,'w') as f_obj:
json.dump(username,f_obj)
print("we'll remember you when you come back, " + username +"!")
print("Welcome back, " + username + "!")
greet_user()
这些代码输出如下:
Welcome back, zhouzhouzhou!
Welcome back, zhouzhouzhou!
要学习测试,得有要测试地代码;
创建测试用例的语法需要一段时间才能习惯,但测试用例创建后,在添加针对函数的单元测试就很简单了;
要为函数编写用例,可先导入模块unittest以及要测试的函数,在创建一个继承unittest.testcase的类,并编写一系列方法对函数的不同方面进行测试;
测试未通过时结果是怎么样的呢?
数据可视化化指的是通过可视化表示来探索数据,它与数据挖掘紧密相关,而数据挖掘指的是使用代码来探索数据集的规律和关联;
pycharm下载安装第三方库
用pycharm下载过第三方库。用的是在pycharm中点击
file -> Settings->Project untitled ->Project Interpreter
然后点击“+”号
需要下载什么输入对话框,点击Install Pac****kage 即可
成功如下图所示:
import matplotlib.pyplot as plt
squares = [1,4,9,16,25]
plt.plot(squares)
plt.show()
上图图形表明数字是越来越大的,但标签文字太小,线条太细;
所幸matplotlib让你能够调整可视化的各个方面;
import matplotlib.pyplot as plt
squares = [1,4,9,16,25]
plt.plot(squares,linewidth = 5)
#设置图表标题,并给坐标轴加上标签
plt.title("Square Numbers",fontsize = 24)
plt.xlabel("Value",fontsize = 14)
plt.ylabel("Square of Value",fontsize = 14)
# 设置刻度标记的大小
plt.tick_params(axis='both' , labelsize = 14)
plt.show()
如下图所示:
图形更容易阅读后,我们发现没有正确地绘制数据:折线图地终点指出4.0的平方为25!下面来修复这个问题:
当你向plot()提供一系列数字时,它假设第一个数据点对应的x坐标轴为0,但我们的第一个点对应的x值为1;
为改变这种默认行为,我们可以提供给plot()同时提供输入值和输出值;
import matplotlib.pyplot as plt
input_values = [1,2,3,4,5]
squares = [1,4,9,16,25]
plt.plot(input_values,squares,linewidth = 5)
#设置图表标题,并给坐标轴加上标签
plt.title("Square Numbers",fontsize = 24)
plt.xlabel("Value",fontsize = 14)
plt.ylabel("Square of Value",fontsize = 14)
# 设置刻度标记的大小
plt.tick_params(axis='both' , labelsize = 14)
plt.show()
输出如下图所示:
有时候,需要绘制散点图并设置各个数据点的样式;
import matplotlib.pyplot as plt
plt.scatter(2,4)
plt.show()
输出如下图所示:
要绘制一系列的点,可向scatter()传递两个分别包含x值和y值的列表,如下图所示:
import matplotlib.pyplot as plt
x_values = [1,2,3,4,5]
y_values = [1,4,9,16,25]
plt.scatter(x_values,y_values,s=100)
#设置图表标题,并给坐标轴加上标签
plt.title("Square Numbers",fontsize = 24)
plt.xlabel("Value",fontsize = 14)
plt.ylabel("Square of Value",fontsize = 14)
# 设置刻度标记的大小
plt.tick_params(axis='both' , labelsize = 14)
plt.show()
输出如下图所示:
import matplotlib.pyplot as plt
x_values = list(range(1,1001))
y_values = [x**2 for x in x_values]
plt.scatter(x_values,y_values,s=20)
#设置图表标题,并给坐标轴加上标签
plt.title("Square Numbers",fontsize = 24)
plt.xlabel("Value",fontsize = 14)
plt.ylabel("Square of Value",fontsize = 14)
# 设置刻度标记的大小
plt.tick_params(axis='both' , labelsize = 14)
plt.show()
输出如下图所示:
import matplotlib.pyplot as plt
x_values = list(range(1,1001))
y_values = [x**2 for x in x_values]
plt.scatter(x_values,y_values,edgecolors= 'none',s=20)
#设置图表标题,并给坐标轴加上标签
plt.title("Square Numbers",fontsize = 24)
plt.xlabel("Value",fontsize = 14)
plt.ylabel("Square of Value",fontsize = 14)
# 设置刻度标记的大小
plt.tick_params(axis='both' , labelsize = 14)
plt.show()
输出如下图所示:
15.2.7 自定义颜色
要修改数据点的颜色,可向scatter()传递参数c,并将其设置为要使用的颜色的名称,如下所示:
import matplotlib.pyplot as plt
x_values = list(range(1,1001))
y_values = [x**2 for x in x_values]
plt.scatter(x_values,y_values,c=(0,0.5,0.2),edgecolors= 'none',s=20)
#设置图表标题,并给坐标轴加上标签
plt.title("Square Numbers",fontsize = 24)
plt.xlabel("Value",fontsize = 14)
plt.ylabel("Square of Value",fontsize = 14)
# 设置刻度标记的大小
plt.tick_params(axis='both' , labelsize = 14)
plt.show()
import matplotlib.pyplot as plt
x_values = list(range(1,1001))
y_values = [x**2 for x in x_values]
plt.scatter(x_values,y_values,c=y_values,cmap=plt.cm.Blues,edgecolors= 'none',s=20)
#设置图表标题,并给坐标轴加上标签
plt.title("Square Numbers",fontsize = 24)
plt.xlabel("Value",fontsize = 14)
plt.ylabel("Square of Value",fontsize = 14)
# 设置刻度标记的大小
plt.tick_params(axis='both' , labelsize = 14)
plt.show()
输出如下图所示:
要让程序自动将图表保存到文件中,可将plt.show()的调用替换为plt.savefig()的调用:
import matplotlib.pyplot as plt
x_values = list(range(1,1001))
y_values = [x**2 for x in x_values]
plt.scatter(x_values,y_values,c=y_values,cmap=plt.cm.Blues,edgecolors= 'none',s=20)
#设置图表标题,并给坐标轴加上标签
plt.title("Square Numbers",fontsize = 24)
plt.xlabel("Value",fontsize = 14)
plt.ylabel("Square of Value",fontsize = 14)
# 设置刻度标记的大小
plt.tick_params(axis='both' , labelsize = 14)
plt.savefig('squares_plot.png',bbox_inches = 'tight')
在本节中,我们将使用Python来生成随机漫步数据,再使用matplotlib以引人瞩目的方式将这学数据呈现出来;
随机漫步是这样行走得到路径:每次形状都完全是随机的,没有明确的方向,结果是由一系列随机决策决定的;
下面先来看看init(),如下所示:
from random import choice
class RandomWalk():
"""一个生成随机漫步数据的类"""
def __init__(self,num_points=5000):
"""初始化随机漫步的属性"""
self.num_points = num_points
#所有随机漫步都始于(0,0)
self.x_values = [0]
self.x_values = [0]
为做出随机决策,我们将所有可能选择都存储在一个列表中,并在每次做决策时都使用choice()来决定使用哪种选择;
我们将使用fill_walk()来生成漫步包含的点,并决定每次漫步的方向,如下所示,请将这个方法添加到random_walk.py中:
from random import choice
class RandomWalk:
"""一个生成随机漫步数据的类"""
def __init__(self,num_points=5000):
"""初始化随机漫步的属性"""
self.num_points = num_points
# 所有随机漫步都始于(0,0)
self.x_values = [0]
self.y_values = [0]
def fill_walk(self):
"""计算随机漫步包含的所有点"""
# 不断漫步,直到列表达到指定长度
while len(self.x_values) < self.num_points:
x_direction = choice([1,-1])
x_distance = choice([0,1,2,3,4])
x_step = x_direction * x_distance
y_direction = choice([1,-1])
y_distance = choice([0, 1, 2, 3, 4])
y_step = y_direction * y_distance
# 拒绝原地踏步
if x_step == 0 and y_step == 0:
continue
#计算下一个点的x和y值
next_x = self.x_values[-1] + x_step
next_y = self.y_values[-1] + y_step
self.x_values.append(next_x)
self.y_values.append(next_y)
下面的代码将随机漫步的所有点都绘制出来:
import matplotlib.pyplot as plt
from random_walk import RandomWalk
#创建一个RandomWalk实例,并将其包含的点都绘制出来
rw = RandomWalk()
rw.fill_walk()
plt.scatter(rw.x_values,rw.y_values,s=15)
plt.show()
输出如下图所示:
每次随机漫步都不同,因此探索可能生成的各种模式很有趣;
要在不多次运行程序的情况下使用前面的代码模拟多次随机漫步,一种方法是将这些代码放在一个while循环中:
import matplotlib.pyplot as plt
from random_walk import RandomWalk
#只要程序处于活动状态,就不断的模拟随机漫步
while True:
#创建一个RandomWalk实例,并将其包含的点都绘制出来
rw = RandomWalk()
rw.fill_walk()
plt.scatter(rw.x_values,rw.y_values,s=15)
plt.show()
keep_runing = input("Make anther walk?(y/n): ")
if keep_runing == 'n':
break
Make anther walk?(y/n): y
Make anther walk?(y/n): y
Make anther walk?(y/n): n
import matplotlib.pyplot as plt
from random_walk import RandomWalk
#只要程序处于活动状态,就不断的模拟随机漫步
while True:
#创建一个RandomWalk实例,并将其包含的点都绘制出来
rw = RandomWalk()
rw.fill_walk()
point_bumbers = list(range(rw.num_points))
plt.scatter(rw.x_values,rw.y_values,c=point_bumbers,cmap=plt.cm.Blues,edgecolors='none',s=15)
plt.show()
keep_runing = input("Make anther walk?(y/n): ")
if keep_runing == 'n':
break
输出如下图所示:
import matplotlib.pyplot as plt
from random_walk import RandomWalk
#只要程序处于活动状态,就不断的模拟随机漫步
while True:
#创建一个RandomWalk实例,并将其包含的点都绘制出来
rw = RandomWalk()
rw.fill_walk()
point_bumbers = list(range(rw.num_points))
plt.scatter(rw.x_values,rw.y_values,c=point_bumbers,cmap=plt.cm.Blues,edgecolors='none',s=15)
#突出起点和终点
plt.scatter(0,0,c='green',edgecolors='none',s=100)
plt.scatter(rw.x_values[-1],rw.y_values[-1],c='red',edgecolors='none',s=100)
plt.show()
keep_runing = input("Make anther walk?(y/n): ")
if keep_runing == 'n':
break
输出如下图所示:
import matplotlib.pyplot as plt
from random_walk import RandomWalk
#只要程序处于活动状态,就不断的模拟随机漫步
while True:
#创建一个RandomWalk实例,并将其包含的点都绘制出来
rw = RandomWalk()
rw.fill_walk()
point_bumbers = list(range(rw.num_points))
plt.scatter(rw.x_values,rw.y_values,c=point_bumbers,cmap=plt.cm.Blues,edgecolors='none',s=15)
#突出起点和终点
plt.scatter(0,0,c='green',edgecolors='none',s=100)
plt.scatter(rw.x_values[-1],rw.y_values[-1],c='red',edgecolors='none',s=100)
#隐藏坐标,注意书上代码错误!!!
plt.xticks([])
plt.yticks([])
plt.show()
keep_runing = input("Make anther walk?(y/n): ")
if keep_runing == 'n':
break
输出如下图所示:
注意:书上隐藏坐标轴的代码存在问题
15.3.9 增加点数
import matplotlib.pyplot as plt
from random_walk import RandomWalk
#只要程序处于活动状态,就不断的模拟随机漫步
while True:
#创建一个RandomWalk实例,并将其包含的点都绘制出来
rw = RandomWalk(50000)
rw.fill_walk()
point_bumbers = list(range(rw.num_points))
plt.scatter(rw.x_values,rw.y_values,c=point_bumbers,cmap=plt.cm.Blues,edgecolors='none',s=15)
#突出起点和终点
plt.scatter(0,0,c='green',edgecolors='none',s=1)
plt.scatter(rw.x_values[-1],rw.y_values[-1],c='red',edgecolors='none',s=1)
#隐藏坐标,注意书上代码错误!!!
plt.xticks([])
plt.yticks([])
plt.show()
keep_runing = input("Make anther walk?(y/n): ")
if keep_runing == 'n':
break
输出如下图所示:
import matplotlib.pyplot as plt
from random_walk import RandomWalk
#只要程序处于活动状态,就不断的模拟随机漫步
while True:
#创建一个RandomWalk实例,并将其包含的点都绘制出来
rw = RandomWalk(50000)
rw.fill_walk()
#设置绘图窗口的尺寸
plt.figure(figsize=(10, 6))
point_bumbers = list(range(rw.num_points))
plt.scatter(rw.x_values,rw.y_values,c=point_bumbers,cmap=plt.cm.Blues,edgecolors='none',s=15)
#突出起点和终点
plt.scatter(0,0,c='green',edgecolors='none',s=1)
plt.scatter(rw.x_values[-1],rw.y_values[-1],c='red',edgecolors='none',s=1)
#隐藏坐标,注意书上代码错误!!!
plt.xticks([])
plt.yticks([])
plt.show()
keep_runing = input("Make anther walk?(y/n): ")
if keep_runing == 'n':
break
输出如下图所示:
同之前操作,不在赘述
from random import randint
class Die():
"""表示一个骰子的类"""
def __init__(self,num_sides=6):
"""骰子默认为6面"""
self.num_sides = num_sides
def roll(self):
"""返回一个位于1和骰子面试之间的随机值"""
return randint(1,self.num_sides)
使用这个类来创建图表前,先来掷D6骰子,将结果打印出来,并检查是否合理:
from die import Die
#创建一个D6
die =Die()
#掷几次骰子,并将结果存储再一个列表中
results = []
for roll_num in range(100):
result = die.roll()
results.append(result)
print(results)
输出如下所示:
[1, 1, 3, 3, 5, 5, 6, 6, 3, 5, 2, 4, 4, 2, 2, 4, 5, 1, 4, 1, 1, 1, 5, 4, 3, 5, 2, 5, 3, 4, 4, 6, 6, 1, 4, 5, 2, 6, 3, 1, 4, 2, 6, 4, 5, 3, 3, 3, 4, 2, 6, 6, 3, 3, 5, 6, 3, 4, 2, 6, 1, 5, 1, 5, 6, 6, 1, 5, 2, 4, 4, 3, 5, 2, 5, 4, 2, 5, 3, 2, 5, 3, 1, 5, 6, 3, 3, 2, 3, 4, 5, 1, 6, 2, 4, 6, 4, 6, 2, 6]
为分析掷一个D6骰子的结果,我们计算每个点数出现的次数:
from die import Die
#创建一个D6
die =Die()
#掷几次骰子,并将结果存储再一个列表中
results = []
for roll_num in range(1000):
result = die.roll()
results.append(result)
#分析结果
frequencies = []
for value in range(1,die.num_sides+1):
frequency = results.count(value)
frequencies.append(frequency)
print(frequencies)
输出如下所示:
[182, 179, 132, 185, 171, 151]
有了频率列表后,我们就可以绘制一个表示结果的直方图;
import pygal
from die import Die
#创建一个D6
die =Die()
#掷几次骰子,并将结果存储再一个列表中
results = []
for roll_num in range(1000):
result = die.roll()
results.append(result)
#分析结果
frequencies = []
for value in range(1,die.num_sides+1):
frequency = results.count(value)
frequencies.append(frequency)
print(frequencies)
hist = pygal.Bar()
hist.title = "Results of rolling one D6 1000 times."
hist.x_labels = ['1','2','3','4','5','6']
hist.x_title = 'Result'
hist.y_title = 'Frequency of Result'
hist.add('D6',frequencies)
hist.render_to_file('die_visual.svg')
要查看生成的直方图,最简单的方式是使用Web浏览器;
import pygal
from die import Die
#创建一个D6
die_1 = Die()
die_2 = Die()
#掷骰子多次,并将结果存储再一个列表中
results = []
for roll_num in range(1000):
result = die_1.roll()+die_2.roll()
results.append(result)
#分析结果
frequencies = []
max_result = die_1.num_sides +die_2.num_sides
for value in range(2,max_result+1):
frequency = results.count(value)
frequencies.append(frequency)
print(frequencies)
hist = pygal.Bar()
hist.title = "Results of rolling two D6 1000 times."
hist.x_labels = ['2','3','4','5','6','7','8','9','10','11','12']
hist.x_title = 'Result'
hist.y_title = 'Frequency of Result'
hist.add('D6+D6',frequencies)
hist.render_to_file('die_visual.svg')
要查看生成的直方图,最简单的方式是使用Web浏览器;
[26, 52, 89, 121, 142, 160, 133, 102, 78, 62, 35]
15.4.8 同时掷面数不同骰子
from die import Die
import pygal
#创建一个D6和一个D10
die_1 = Die()
die_2 = Die(10)
#掷骰子多次,并将结果存储再一个列表中
results = []
for roll_num in range(50000):
result = die_1.roll() + die_2.roll()
results.append(result)
#分析结果
frequencies = []
max_result = die_1.num_sides +die_2.num_sides
for value in range(2,max_result+1):
frequency = results.count(value)
frequencies.append(frequency)
print(frequencies)
hist = pygal.Bar()
hist.title = "Results of rolling a D6 and a D10 50000 times."
hist.x_labels = ['2','3','4','5','6','7','8','9','10','11','12','13','14','15','16']
hist.x_title = 'Result'
hist.y_title = 'Frequency of Result'
hist.add('D6+D10',frequencies)
hist.render_to_file('die_visual.svg')
[803, 1660, 2495, 3377, 4164, 5017, 4962, 5048, 5019, 5023, 4213, 3368, 2425, 1629, 797]
要查看生成的直方图,最简单的方式是使用Web浏览器;
要在文本中存储数据,最简单的方式是将数据作为一系列以逗号分隔的值(CSV)写入文件
import csv
filename = r'C:\Users\DELL\Desktop\python_work\《Python编程》源代码文件\chapter_16\sitka_weather_07-2014.csv'
with open(filename) as f:
reader =csv.reader(f)
header_row = next(reader)
print(header_row)
输出为:
[‘AKDT’, ‘Max TemperatureF’, ‘Mean TemperatureF’, ‘Min TemperatureF’, ‘Max Dew PointF’, ‘MeanDew PointF’, ‘Min DewpointF’, ‘Max Humidity’, ’ Mean Humidity’, ’ Min Humidity’, ’ Max Sea Level PressureIn’, ’ Mean Sea Level PressureIn’, ’ Min Sea Level PressureIn’, ’ Max VisibilityMiles’, ’ Mean VisibilityMiles’, ’ Min VisibilityMiles’, ’ Max Wind SpeedMPH’, ’ Mean Wind SpeedMPH’, ’ Max Gust SpeedMPH’, ‘PrecipitationIn’, ’ CloudCover’, ’ Events’, ’ WindDirDegrees’]
注意:
import csv
filename = r'C:\Users\DELL\Desktop\python_work\《Python编程》源代码文件\chapter_16\sitka_weather_07-2014.csv'
with open(filename) as f:
reader =csv.reader(f)
header_row = next(reader)
for index,column_header in enumerate(header_row):
print(index,column_header)
输出为:
0 AKDT
1 Max TemperatureF
2 Mean TemperatureF
3 Min TemperatureF
4 Max Dew PointF
5 MeanDew PointF
6 Min DewpointF
7 Max Humidity
8 Mean Humidity
9 Min Humidity
10 Max Sea Level PressureIn
11 Mean Sea Level PressureIn
12 Min Sea Level PressureIn
13 Max VisibilityMiles
14 Mean VisibilityMiles
15 Min VisibilityMiles
16 Max Wind SpeedMPH
17 Mean Wind SpeedMPH
18 Max Gust SpeedMPH
19 PrecipitationIn
20 CloudCover
21 Events
22 WindDirDegreesProcess finished with exit code 0
知道需要那些列中的数据后,我们来读取一些数据;首先读取每天的最高气温:
import csv
filename = r'C:\Users\DELL\Desktop\python_work\《Python编程》源代码文件\chapter_16\sitka_weather_07-2014.csv'
with open(filename) as f:
reader =csv.reader(f)
header_row = next(reader)
highs = []
for row in reader:
highs.append(row[1])
print(highs)
输出为:
[‘64’, ‘71’, ‘64’, ‘59’, ‘69’, ‘62’, ‘61’, ‘55’, ‘57’, ‘61’, ‘57’, ‘59’, ‘57’, ‘61’, ‘64’, ‘61’, ‘59’, ‘63’, ‘60’, ‘57’, ‘69’, ‘63’, ‘62’, ‘59’, ‘57’, ‘57’, ‘61’, ‘59’, ‘61’, ‘61’, ‘66’]
下面使用int()将这些字符串转化为数字
import csv
filename = r'C:\Users\DELL\Desktop\python_work\《Python编程》源代码文件\chapter_16\sitka_weather_07-2014.csv'
with open(filename) as f:
reader =csv.reader(f)
header_row = next(reader)
highs = []
for row in reader:
high = int(row[1])
highs.append(row[1])
print(highs)
输出为:
[‘64’, ‘71’, ‘64’, ‘59’, ‘69’, ‘62’, ‘61’, ‘55’, ‘57’, ‘61’, ‘57’, ‘59’, ‘57’, ‘61’, ‘64’, ‘61’, ‘59’, ‘63’, ‘60’, ‘57’, ‘69’, ‘63’, ‘62’, ‘59’, ‘57’, ‘57’, ‘61’, ‘59’, ‘61’, ‘61’, ‘66’]
为可视化这些气温数据,我们首先使用matplotlib创建一个显示每日最高气温的简单图形:
import csv
from matplotlib import pyplot as plt
filename = r'C:\Users\DELL\Desktop\python_work\《Python编程》源代码文件\chapter_16\sitka_weather_07-2014.csv'
with open(filename) as f:
reader =csv.reader(f)
header_row = next(reader)
highs = []
for row in reader:
high = int(row[1])
highs.append(high)
#根据数据绘制图形
fig = plt.figure(dpi=128, figsize=(10, 6))
plt.plot(highs, c='red')
#设置图形的格式
plt.title("Daily high temperatures,July 2014")
plt.xlabel('', fontsize=16)
plt.ylabel("Temperature(F)")
plt.tick_params(axis='both', which='major', labelsize=16)
plt.show()
输出为:
下面在图表添加日期,使其更有用;
import csv
from matplotlib import pyplot as plt
from datetime import datetime
filename = r'C:\Users\DELL\Desktop\python_work\《Python编程》源代码文件\chapter_16\sitka_weather_07-2014.csv'
with open(filename) as f:
reader =csv.reader(f)
header_row = next(reader)
dates,highs = [],[]
for row in reader:
current_date = datetime.strptime(row[0],'%Y-%m-%d')
dates.append(current_date)
high = int(row[1])
highs.append(high)
#根据数据绘制图形
fig = plt.figure(dpi=128, figsize=(10, 6))
plt.plot(dates,highs, c='red')
#设置图形的格式
plt.title("Daily high temperatures,July 2014", fontsize=16)
plt.xlabel('', fontsize=16)
fig.autofmt_xdate()
plt.ylabel("Temperature(F)", fontsize=16)
plt.tick_params(axis='both', which='major', labelsize=16)
plt.show()
输出为:
模块datetime中设置日期和时间格式的实参
import csv
from matplotlib import pyplot as plt
from datetime import datetime
filename = r'C:\Users\DELL\Desktop\python_work\《Python编程》源代码文件\chapter_16\sitka_weather_2014.csv'
with open(filename) as f:
reader =csv.reader(f)
header_row = next(reader)
dates,highs = [],[]
for row in reader:
current_date = datetime.strptime(row[0],'%Y-%m-%d')
dates.append(current_date)
high = int(row[1])
highs.append(high)
#根据数据绘制图形
fig = plt.figure(dpi=128, figsize=(10, 6))
plt.plot(dates,highs, c='red')
#设置图形的格式
plt.title("Daily high temperatures - 2014", fontsize=16)
plt.xlabel('', fontsize=10)
fig.autofmt_xdate()
plt.ylabel("Temperature(F)", fontsize=16)
plt.tick_params(axis='both', which='major', labelsize=16)
plt.show()
输出为:
import csv
from matplotlib import pyplot as plt
from datetime import datetime
filename = r'C:\Users\DELL\Desktop\python_work\《Python编程》源代码文件\chapter_16\sitka_weather_2014.csv'
with open(filename) as f:
reader =csv.reader(f)
header_row = next(reader)
dates,highs,lows= [],[],[]
for row in reader:
current_date = datetime.strptime(row[0],'%Y-%m-%d')
dates.append(current_date)
high = int(row[1])
highs.append(high)
low = int(row[3])
lows.append(low)
#根据数据绘制图形
fig = plt.figure(dpi=128, figsize=(10, 6))
plt.plot(dates,highs, c='red')
plt.plot(dates,lows,c='blue')
#设置图形的格式
plt.title("Daily high and low temperatures - 2014", fontsize=24)
plt.xlabel('', fontsize=10)
fig.autofmt_xdate()
plt.ylabel("Temperature(F)", fontsize=16)
plt.tick_params(axis='both', which='major', labelsize=16)
plt.show()
输出为:
import csv
from matplotlib import pyplot as plt
from datetime import datetime
filename = r'C:\Users\DELL\Desktop\python_work\《Python编程》源代码文件\chapter_16\sitka_weather_2014.csv'
with open(filename) as f:
reader =csv.reader(f)
header_row = next(reader)
dates,highs,lows= [],[],[]
for row in reader:
current_date = datetime.strptime(row[0],'%Y-%m-%d')
dates.append(current_date)
high = int(row[1])
highs.append(high)
low = int(row[3])
lows.append(low)
#根据数据绘制图形
fig = plt.figure(dpi=128, figsize=(10, 6))
plt.plot(dates,highs, c='red',alpha=0.5)
plt.plot(dates,lows,c='blue',alpha=0.5)
plt.fill_between(dates,highs,lows,facecolor='blue',alpha=0.1)
#设置图形的格式
plt.title("Daily high and low temperatures - 2014", fontsize=16)
plt.xlabel('', fontsize=10)
fig.autofmt_xdate()
plt.ylabel("Temperature(F)", fontsize=16)
plt.tick_params(axis='both', which='major', labelsize=16)
plt.show()
输出为:
Web API是网站的一部分,用于于使用非常具体的URL请求特定信息的程序交互,这种请求称为API调用;
请求的数据将易于出来的格式(如JSON或CSV)返回;
-END-
读者福利:如果大家对Python感兴趣,这套python学习资料一定对你有用
对于0基础小白入门:
如果你是零基础小白,想快速入门Python是可以考虑的。
一方面是学习时间相对较短,学习内容更全面更集中。
二方面是可以根据这些资料规划好学习计划和方向。
包括:Python激活码+安装包、Python web开发,Python爬虫,Python数据分析,人工智能、机器学习、Python量化交易等习教程。带你从零基础系统性的学好Python!
① Python所有方向的学习路线图,清楚各个方向要学什么东西
② 600多节Python课程视频,涵盖必备基础、爬虫和数据分析
③ 100多个Python实战案例,含50个超大型项目详解,学习不再是只会理论
④ 20款主流手游迫解 爬虫手游逆行迫解教程包
⑤ 爬虫与反爬虫攻防教程包,含15个大型网站迫解
⑥ 爬虫APP逆向实战教程包,含45项绝密技术详解
⑦ 超300本Python电子好书,从入门到高阶应有尽有
⑧ 华为出品独家Python漫画教程,手机也能学习
⑨ 历年互联网企业Python面试真题,复习时非常方便
Python所有方向的技术点做的整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。(全套教程文末领取哈)
温馨提示:篇幅有限,已打包文件夹,获取方式在:文末
观看零基础学习视频,看视频学习是最快捷也是最有效果的方式,跟着视频中老师的思路,从基础到深入,还是很容易入门的。
光学理论是没用的,要学会跟着一起敲,要动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。
检查学习结果。
上述这份完整版的Python全套学习资料已经上传CSDN官方,朋友们如果需要可以微信扫描下方CSDN官方认证二维码 即可领取↓↓↓