项目地址:网页链接
full-speed-python书籍地址:https://github.com/joaoventura/full-speed-python
参考链接:https://www.chzzz.club/post/17.html
-
5.2 Exercise with the while statement
1.Implement a function that receives a number as parameter and prints, in decreasing
order, which numbers are even and which are odd, until it reaches 0.def even_odd(n): while n>0: if n%2 == 0: print('Even number:',n) n -= 1 else: print('Odd number:',n) n -= 1 even_odd(11)
Odd number: 11
Even number: 10
Odd number: 9
Even number: 8
Odd number: 7
Even number: 6
Odd number: 5
Even number: 4
Odd number: 3
Even number: 2
Odd number: 1 -
6.1 Exercise with dictionaries
1.How many students are in the dictionary? Search for the “len” function.
2.Implement a function that receives the “ages” dictionary as parameter and return
the average age of the students. Traverse all items on the dictionary using the
“items” method as above.
3.Implement a function that receives the “ages” dictionary as parameter and returns
the name of the oldest student.
4.Implement a function that receives the “ages” dictionary and a number “n” and
returns a new dict where each student is n years older. For instance, new_ages(ages,
10) returns a copy of “ages” where each student is 10 years older.ages = {'Peter':10, 'Isabel':11, 'Anna':9, 'Thomas':10, 'Bob':10, 'Joseph':11, 'Marie':12, 'Gabriel':10,} print('the num of students:',len(ages)) def ave_ages(ages): sumage = 0 for age in ages.values(): sumage += age return sumage/len(ages) print('average ages of the students:',ave_ages(ages)) def find_oldest(ages): maxage = 0 oldestname = '' for name,age in ages.items(): if age > maxage: maxage = age oldestname = name return oldestname,maxage print('the oldest onee:',find_oldest(ages)) def new_ages(ages,n): new_ages = {} for name,age in ages.items(): if age == n: new_ages[name] = n return new_ages print('the 10 years old student:',new_ages(ages,10))
the num of students: 8
average ages of the students: 10.375
the oldest onee: (‘Marie’, 12)
the 10 years old student: {‘Peter’: 10, ‘Thomas’: 10, ‘Bob’: 10, ‘Gabriel’: 10} -
6.2 Exercise with sub-dictionaries
1.How many students are in the “students” dict? Use the appropriate function.
2.Implement a function that receives the students dict and returns the average age.
3.Implement a function that receives the students dict and an address, and returns
a list with the name of all students which address matches the address in the
argument. For instance, invoking “find_students(students, ’Lisbon’)” should return
Peter and Anna.students = {"Peter": {"age": 10, "address": "Lisbon"}, "Isabel": {"age": 11, "address": "Sesimbra"}, "Anna": {"age": 9, "address": "Lisbon"},} print('there are',len(students),'students') def ave_age(dict): sumage = 0 for stu in students.values(): sumage += stu['age'] return sumage/len(students) print('the average age:',ave_age(students)) def find_students(dict,address): stulist = [] for name,stu in students.items(): if stu['address'] == address: stulist.append(name) return stulist print('the students with address Lisbon',find_students(students,'Lisbon'))
there are 3 students
the average age: 10.0
the students with address Lisbon [‘Peter’, ‘Anna’] -
7.1 Exercise with classes
1.Implement a class named “Rectangle” to store the coordinates of a rectangle given
by (x1, y1) and (x2, y2).
2.Implement the class constructor with the parameters (x1, y1, x2, y2) and store
them in the class instances using the “self” keyword.
3.Implement the “width()” and “height()” methods which return, respectively, the
width and height of a rectangle. Create two objects, instances of “Rectangle” to
test the calculations.
4.Implement the method “area” to return the area of the rectangle (widthheight).
5.Implement the method “circumference” to return the perimeter of the rectangle
(2width + 2*height).
6.Do a print of one of the objects created to test the class. Implement the “str“
method such that when you print one of the objects it print the coordinates as (x1,
y1)(x2, y2).class Rectangle: def __init__(self,x1,y1,x2,y2): self.x1 = x1 self.y1 = y1 self.x2 = x2 self.y2 = y2 def __str__(self): return 'coordinate:%s,%s'%((self.x1,self.y1),(self.x2,self.y2)) def width(self): width = abs(self.x1-self.x2) return width def height(self): height = abs(self.y1-self.y2) return height def area(self): return self.width()*self.height() def circumference(self): return 2*self.width()+2*self.height() a = Rectangle(1,1,3,3) print(a.area()) print(a)
4
coordinate:(1, 1),(3, 3) -
7.3 Exercise with inheritance
1.Create a “Square” class as subclass of “Rectangle”.
2.Implement the “Square” constructor. The constructor should have only the x1, y1
coordinates and the size of the square. Notice which arguments you’ll have to use
when you invoce the “Rectangle” constructor when you use “super”.
3.Instantiate two objects of “Square”, invoke the area method and print the objects.
Make sure that all calculations are returning correct numbers and that the coordinates
of the squares are consistent with the size of the square used as argument.class Square(Rectangle): def __init__(self,x1,y1,size): super().__init__(x1,y1,x1+size,y1+size) b = Square(1,2,2) print(b.area()) print(b)
4
coordinate:(1, 2),(3, 4)