8000 formatting questions · zfoxpython/intro-to-python@cd4e2f5 · GitHub
[go: up one dir, main page]

Skip to content
< 8000 /div>

Commit cd4e2f5

Browse files
committed
formatting questions
2 parents a4193de + 4e2b444 commit cd4e2f5

File tree

7 files changed

+212
-9
lines changed

7 files changed

+212
-9
lines changed

session_10/answers/A1.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# 1. Create a Person class, initialise it with a name. Create a method for the Person class that will say hello to the name.
2+
3+
class Person:
4+
def __init__(self, name):
5+
self.name = name
6+
7+
def hello(self):
8+
print("Hello " + self.name)
9+
10+
saf = Person("Saf")
11+
jake = Person("Jake")
12+
13+
saf.hello()
14+
jake.hello()

session_10/answers/A2.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# 2. Create a Circle class, initialise it with a radius. Create two methods for the Circle class: get_area() and get_circumference() which give both respective areas and circumference, to 2 decimal placese.
2+
# - Note: Area of a circle = πr ** 2
3+
# - Circumference = 2πr
4+
# - Use the round() function to get the answer to 2 decimal places
5+
6+
7+
class Circle:
8+
pi = 3.14159265
9+
10+
def __init__(self, radius):
11+
self.radius = radius
12+
13+
def get_area(self):
14+
print("Area: " + str(round(self.pi * (self.radius ** 2), 2)))
15+
16+
def get_circumference(self):
17+
print("Circumference: " + str(round(self.pi * (self.radius * 2), 2)))
18+
19+
circle1 = Circle(9)
20+
circle1.get_area()
21+
circle1.get_circumference()
22+
23+
circle2 = Circle(6.87)
24+
circle2.get_area()
25+
circle2.get_circumference()

session_10/answers/A3.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# 3. Create a Employee class and initialise it with name and staff number.
2+
# - i. Make methods to:
3+
# - display_info - It should display all the information of the employee.
4+
# - set_department - It should assign the department to employee.
5+
# - set_bonus - It should assign a bonus amount to the employee.
6+
7+
class Employee:
8+
def __init__(self, name, staff_number):
9+
self.name = name
10+
self.staff_number = staff_number
11+
12+
def display_info(self):
13+
print("Employee Information: \n Name: " + self.name + "\n Staff ID: " + str(self.staff_number) + "\n Bonus: " + str(self.bonus) + "\n Department: " + str(self.department))
14+
15+
def set_department(self, department):
16+
self.department = department
17+
18+
def set_bonus(self, bonus):
19+
self.bonus = bonus
20+
21+
employee1 = Employee("Saf Mirza", 23435)
22+
employee1.set_bonus(1000)
23+
employee1.set_department("Technology")
24+
employee1.display_info()
25+
26+
# - ii. Create the instance attributes first name and last name instead of name.
27+
# - Create two methods full_name and email_address in the Employee class.
28+
# - Given a person's first and last names:
29+
# - Form the full_name method by simply joining the first and last name together, separated by a space.
30+
# - Form the email_address by joining the first and last name together with a . in between, and follow it with @company.com at the end. Make sure everything is in lowercase.
31+
32+
class Employee:
33+
def __init__(self, first_name, last_name, staff_number):
34+
self.first_name = first_name
35+
self.last_name = last_name
36+
self.staff_number = staff_number
37+
38+
def display_info(self):
39+
print(self.first_name, self.last_name, self.staff_number, self.bonus, self.department)
40+
41+
def set_department(self, department):
42+
self.department = department
43+
44+
def set_bonus(self, bonus):
45+
self.bonus = bonus
46+
47+
def full_name(self):
48+
fullname = self.first_name + " " + self.last_name
49+
print("Fullname: " + fullname)
50+
51+
def email_address(self):
52+
email = self.first_name + "." + self.last_name + "@company.com"
53+
print("Email: " + email.lower())
54+
55+
employee1 = Employee("Saf", "Mirza", 23435)
56+
employee1.full_name()
57+
employee1.email_address()

session_10/answers/A4.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
# 4. Create a Vehicle parent class, initialise it with, wheels, colour and a method to display all this information.
3+
# - i. Create a Tesla (or any car brand) child classs and add a method to get the miles and a method to display all this information.
4+
# - ii. Change the colour of the vehicle.
5+
# - iii. Delete the wheels.
6+
7+
class Vehicle:
8+
def __init__(self, wheels, colour):
9+
self.wheels = wheels
10+
self.colour = colour
11+
12+
def display_info(self):
13+
print("This car is " + self.colour + " and has " + str(self.wheels) + " wheels.")
14+
15+
class Tesla(Vehicle):
16+
def __init__(self, wheels, colour, miles):
17+
super().__init__(wheels, colour)
18+
self.miles = miles
19+
20+
def display_more_info(self):
21+
print("This car is " + self.colour + " and has " + str(self.wheels) + " wheels and " + str(self.miles) + " miles.")
22+
23+
tesla = Tesla(4, "black", 20000)
24+
tesla.display_more_info()
25+
26+
tesla.colour = "red"
27+
tesla.display_more_info()
28+
29+
del tesla.wheels
30+
tesla.display_more_info()
31+
32+
33+

session_10/answers/A5.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# 5. Create a class called Upper which has two methods called get_word and print_word.
2+
# - the get_word method should accept a string from the user
3+
# - the print_word method prints the string in upper case.
4+
5+
class Upper:
6+
def __init__(self):
7+
self.word = ""
8+
9+
def get_word(self):
10+
self.word = input("Input a word:\n")
11+
12+
def print_word(self):
13+
print(self.word.upper())
14+
15+
word1 = Upper()
16+
word1.get_word()
17+
word1.print_word()

session_10/answers/A6.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# 6. Create a Sandwich class with the attributes order_number and ingredients.
2+
# - i. The ingredients attributes is given as a list - (Note: use list(<atrribute>) to enable this).
3+
# - Only the ingredients attributes will be given as input.
4+
# - ii. The order attriubte will be a method that counts the order number.
5+
# - iii. Make three methods for the following favourite sandwiches, for customers who don't want to create a sandwich:
6+
# - vegan_hot - vegan cheese, meatless meatballs, jalapenos
7+
# - meat_feast - steak, peppers, cheese
8+
# - veggie - tomamto, spinach, mushroom, eggs
9+
10+
class Sandwich:
11+
orders = 0
12+
13+
def __init__(self, ingredients):
14+
self.ingredients = list(ingredients)
15+
self.order_number = self.get_order_number()
16+
17+
def get_order_number(self):
18+
Sandwich.orders += 1
19+
return Sandwich.orders
20+
21+
def vegan_hot():
22+
return Sandwich(["vegan cheese", "meatless meatballs", "jalapenos"])
23+
24+
def meat_feast():
25+
return Sandwich(["steak", "peppers", "cheese"])
26+
27+
def veggie():
28+
return Sandwich(["tomato", "spinach", "mushroom", "eggs"])
29+
30+
31+
sandwich1 = Sandwich.vegan_hot()
32+
print(sandwich1.ingredients)
33+
print(sandwich1.order_number)
34+
sandwich2 = Sandwich(["tuna", "mayo", "lettuce"])
35+
print(sandwich2.ingredients)
36+
print(sandwich2.order_number)

session_10/exercises/exercises_10.md

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,34 @@
11
## Section 10 Exercises
22

33
# Section A
4-
1. Create a Person class, initialise it with a name. Create a method that will say hello to the name.
5-
2. Create a Square class, initialise it with a width and height. Create a method for the perimeter and area.
6-
3. Create a Vehicle class, initialise it with, wheels, colour. Create a method for the miles and a method to display all this information.
7-
- i. Change the colour of the vehicle.
8-
- ii. Delete the wheels.
4+
1. Create a Person class, initialise it with a name. Create a method for the Person class that will say hello to the name.
5+
2. Create a Circle class, initialise it with a radius. Create two methods for the Circle class: get_area() and get_circumference() which give both respective areas and circumference, to 2 decimal placese.
6+
- Note: Area of a circle = πr ** 2
7+
- Circumference = 2πr
8+
- Use the round() function to get the answer to 2 decimal places
9+
3. Create a Employee class and initialise it with name and staff number.
10+
- i. Make methods to:
11+
- display_info - It should display all the information of the employee.
12+
- set_department - It should assign the department to employee.
13+
- set_bonus - It should assign a bonus amount to the employee.
914

10-
4. Create a Employee class and initialise it with name and staff number. Make methods to:
11-
- display_info - It should display all the information of the employee.
12-
- set_department - It should assign the department to employee.
13-
- set_bonus - It should assign a bonus amount to the employee.
15+
- ii. Create the instance attributes first name and last name instead of name.
16+
- Create two methods full_name and email_address in the Employee class.
17+
- Given a person's first and last names:
18+
- Form the full_name method by simply joining the first and last name together, separated by a space.
19+
- Form the email_address by joining the first and last name together with a . in between, and follow it with @company.com at the end. Make sure everything is in lowercase.
20+
4. Create a Vehicle parent class, initialise it with, wheels, colour and a method to display all this information.
21+
- i. Create a Tesla (or any car) child classs and add a method to get the miles and a method to display all this information.
22+
- ii. Change the colour of the vehicle.
23+
- iii. Delete the wheels.
24+
5. Create a class called Upper which has two methods called get_word and print_word.
25+
- the get_word method should accept a string from the user
26+
- the print_word method prints the string in upper case.
27+
6. Create a Sandwich class with the attributes order_number and ingredients.
28+
- i. The ingredients attributes is given as a list - (Note: use list(<atrribute>) to enable this).
29+
- Only the ingredients attributes will be given as input.
30+
- ii. The order attriubte will be a method that counts the order number.
31+
- iii. Make three methods for the following favourite sandwiches, for customers who don't want to create a sandwich:
32+
- vegan_hot - vegan cheese, meatless meatballs, jalapenos
33+
- meat_feast - steak, peppers, cheese
34+
- veggie - tomamto, spinach, mushroom, eggs

0 commit comments

Comments
 (0)
0