Modules -> OOP -> Classes and functions -> End Chapter Exercises

End Chapter Exercises


Exercises


Create a class named Book


Create a book and assign it to book


Add two attributes to book, title and author, and assign them to two strings.




Create a class named Student


Create a student and assign it to st, add two attributes to it, name and grade, and assign them to a string and an integer respectively.


Write a function print_student, which takes a Student instance and prints it in the following format: name - grade.

Example:

>>> print_student(st)
Dudije - 3




Write a function, add_pupulation, which takes a City instance and an integer, city and p, and adds p to city's population. Example:

>>> add_population(city, 100)
>>> print(city.population)
600100




Write a function that takes a City instance as argument and returns:
   - It returns "small" if its population is less than 100000.
   - It returns "medium" if its population is between 100000 and 499999 (including both).
   - It returns "big" if its population is greater than or equal to 500000.


Example:

>>> get_city_size(city)
"medium"




Create a class named Country.
Create a country and assign it to c. Add an attribute, name to c and assign a string to it.


Add a new attribute, country, to city and assign c to it.