封面
第一部分 基础(第1-11章)
#代码放乱了,懒得按照章节形式展示(...
#-*-coding:GBK -*-
#city_functions.py
def city_country(city,country,population=''):
"""定义一个返回城市名和国家名的函数"""
'''
将population设置为一个具有默认值的可选参数
'''
if population:
msg = city.lower().title() + ', ' + country.lower().title() + ' - ' + 'population ' + str(population)
else:
msg = city.lower().title() + ', ' + country.lower().title()
return msg
#print(city_country('wuhan','china'))
#codestesting.py
# -*- coding: utf-8 -*-
'''
测试函数
单元测试和测试用例:
python标准库中的unittest提供了代码测试工具
*单元测试:核实函数的某个方面没有问题
*测试用例:
一组单元测试,一起核实函数在各种情形下的行为都符合要求
良好的测试用例考虑到了函数可能收到的各种输入,包含所有针对这种情形的测试
*全覆盖式测试用例包含一整套单元测试,涵盖了各种可能的函数使用方式(广泛使用再考虑)
'''
'''
测试类
6个常用的断言方法
1.assertEqual(self, first, second, msg=None)
--判断两个参数相等:first == second
2.assertNotEqual(self, first, second, msg=None)
--判断两个参数不相等:first != second
3.assertIn(self, member, container, msg=None)
--判断是字符串是否包含:member in container
4.assertNotIn(self, member, container, msg=None)
--判断是字符串是否不包含:member not in container
5.assertTrue(self, expr, msg=None)
--判断是否为真:expr is True
6.assertFalse(self, expr, msg=None)
--判断是否为假:expr is False
一个需要测试的类:
测试类中方法的行为
方法setUp()
在TestCase类中创建对象一次,即可在之后的每个测试方法中对它们进行使用
可在setUp()方法中创建一系列实例并设置它们的属性,再在测试方法中直接使用这些实例
运行测试用例时,每完成一个单元测试,python均打印出一个字符。
测试通过.
测试引发错误E
测试导致断言失败F
'''
#上述训练按照课本要求分别创建对应的py文件
#dictionary.py
# coding=gbk
'''
字典可存储的信息量基本上不受限制
鉴于它“键值对”的特点,与键对应的值可以是Python中的任何对象
Python不关心键值对的添加顺序,只关心键与值之间的关联关系
'''
'''
6-1
person = {
'first_name' : 'jane',
'last_name' : 'smith',
'age' : 23,
'city' : 'helan',
}
print(person)
'''
'''
6-2和6-3的结合
person = {
'first_name' : 'jane',
'last_name' : 'smith',
'age' : 23,
'city' : 'helan',
}
print('the first_name is ' + person['first_name'] + '\n')
print('the last_name is ' + person['last_name'] + '\n')
print('the age is ' + str(person['age']) + '\n')
print('the city is ' + person['city'] + '\n')
'''
'''
遍历字典
返回值依然有可能与列表存储的顺序不同,Python只关心键值对应关系
循环时用到的方法
.items()遍历所有键值对
.keys()遍历所有键,Python中默认遍历所有键,所以该方法可省略。
同时该方法不仅仅用于遍历,它还返回一个列表,其中包含字典中的所有键。
sorted()按顺序遍历
.values()遍历所有值,返回一个值列表,而不包含任何键
该方法不考虑返回值是否重复
配合使用set()函数【集合】即可找出列表中独一无二的元素,并使用这些元素来创建一个列表
'''
'''
6-4
person = {
'first_name' : 'jane',
'last_name' : 'smith',
'age' : 23,
'city' : 'helan',
'1' : 'test1',
'2' : 'test2',
'3' : 'test3',
'4' : 'test4',
'5' : 'test5',
}
for key, value in person.items():
print('\nkey: ' + key + ' value: ' + str(value))
'''
'''
6-5
geo = {
'nile' : 'egypt',
'long river' : 'china',
'huang river' : 'china',
}
for key, value in geo.items():
print('The ' + key.title() + ' runs through ' + value.title())
print('\n')
for key in geo.keys():
print(key)
print('\n')
for value in set(geo.values()):
print(value)
'''
'''
6-6
alreadyinpoll = {
'person_a' : 'java',
'person_b' : 'c',
'person_c' : 'python',
}
persons = ['person_a','person_b','person_c','jane']
for person in persons:
if person in set(alreadyinpoll.keys()):
print(person.title() + ", thank you for taking the poll!")
else:
print(person.title() + ", please take the poll.")
'''
'''
嵌套
字典列表(列表中存储字典)
字典中存储列表:当需要一个键关联多个值时。
字典中存储字典
'''
'''
6-7、6-8
person_1 = {
'first_name' : 'jane',
'last_name' : 'smith',
'age' : 23,
'city' : 'japan',
}
person_2 = {
'first_name' : 'sam',
'last_name' : 'smith',
'age' : 33,
'city' : 'china',
}
person_3 = {
'first_name' : 'lily',
'last_name' : 'smith',
'age' : 13,
'city' : 'europe',
}
people = [person_1,person_2,person_3]
for person in people:
print(person)
'''
'''
6-9、6-10
favorite_places = {
'jane' : ['Beijing','Nanjing'],
'sam' : ['Wuhan','Shanghai'],
'sue' : ['Japan'],
}
for name, places in favorite_places.items():
if len(places) == 1:
#print(name.title() + ", your favorite place is : \n" + str(favorite_places.values()))
print('\n' + name.title() + ", your favorite place is : \n")
for place in places:
print(place)
else:
print('\n' + name.title() + ", your favorite places are : \n")
for place in places:
print(place)
#for place in favorite_places.values():
#print(place)
#需要运行出特定对象所对应的地点,上面两个全部是错误的。
'''
'''
6-11
cities = {
'Beijing' :{
'coutry' : 'china',
'population' : '1 billion',
'fact' : 'the capital of the china',
},
'Shanghai' :{
'coutry' : 'china',
'population' : '1.5 billion',
'fact' : 'the financial center of the china',
},
'Zhengzhou' :{
'coutry' : 'china',
'population' : '2 billion',
'fact' : 'a province of the china',
},
}
for key, values in cities.items():
print('\n' + key + '\n')
for value in values.values():
print(value)
'''
#employee.py
#-*-coding:GBK -*-
# -*- coding: utf-8 -*-
class Employee:
"""一个描述职员信息的类"""
def __init__(self,first_name,last_name,annual_salary):
"""初始化描述职员的属性"""
self.first_name = first_name
self.last_name = last_name
self.annual_salary = annual_salary
self.annual_raise = 5000
'''
def set_annual_raise(self, annual_raise):
"""定义一个方法修改薪水增长量"""
self.annual_raise = annual_raise
'''
def give_raise(self,annual_raise=''):
"""增加年薪的类,用户自定义的薪水量以实参形式传入"""
if annual_raise:
self.annual_salary += annual_raise#这边只写raise编译不通过,raise貌似是一个关键字无法通过编译
else:
self.annual_salary += 5000
def describe_employee_salary(self):
"""对雇员信息中的薪水进行描述"""
#msg = 'The name: ' + self.first_name + self.last_name + '\n'
return self.annual_salary
#files&exceptions.py
# -*- coding: utf-8 -*-
'''
从文件中读取数据
'''
'''
读取整个文件:
先打开
函数open()
接受一个参数,即要打开的文件的名称,Python在当前执行文件所在的目录查找指定文件
open('pi_digits.txt')返回一个表示文件pi_digits.txt的对象file_object,Python将这个对象存储在我们将在后面使用的变量中
关键词with在不再需要访问文件后将其关闭,防止close()函数在程序bug的存在下不会执行
'''
'''
with open('pi_digits.txt') as file_object:
contents = file_object.read()
#print(contents)#此处输出相比于原始文件多出一个空行,因为read()到达文件末尾时返回一个空字符串,而将这个空字符串显示出来就是一个空行
print(contents.rstrip())#消除最后的空行
'''
'''
文件路径
*'pi_digits.txt',数据文件存储在当前执行文件所在目录
*相对路径,数据文件存储在当前执行文件所在目录下的一个文件夹中
*准确位置,定义file_path,可以访问文件系统中的任意位置文件
'''
'''
逐行读取
'&