Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Python Home Work

Instructions: Answer all Questions


# Question 1:
Print the following pattern

12

123

1234

12345
Question 2:

Accept number from user and calculate the sum of all number between 1 and given
number

Question 3:

Given a list iterate it and display numbers which are divisible by 5 and if you find
number greater than 150 stop the loop iteration

list1 = [12, 15, 32, 42, 55, 75, 122, 132, 150, 180, 200]

Question 4:

Generate a Python list of all the even numbers between 4 to 30

Expected Output:

[4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28]

Question 5:

Write a recursive function to calculate the sum of numbers from 0 to 10


Expected Output:
55

Question 6:

Remove empty strings from the list of strings

list1 = ["Mike", "", "Emma", "Kelly", "", "Brad"]

Expected output:

["Mike", "Emma", "Kelly", "Brad"]

Question 7:

Given a Python list, remove all occurrence of 20 from the list

list1 = [5, 20, 15, 20, 25, 50, 20]

Expected output:

[5, 15, 25, 50]

Question 8:

Find the last position of a substring “Emma” in a given string

Given:

str1 = "Emma is a data scientist who knows Python. Emma works at google."

Where in the string is the last occurrence of the substring “Emma”?:

Expected Output:

Last occurrence of Emma starts at index 43

exercise 9:
Given a Python dictionary, Change Brad’s salary to 8500
sampleDict = {
'emp1': {'name': 'Jhon', 'salary': 7500},
'emp2': {'name': 'Emma', 'salary': 8000},
'emp3': {'name': 'Brad', 'salary': 6500}
}

Expected output:

sampleDict = {
'emp1': {'name': 'Jhon', 'salary': 7500},
'emp2': {'name': 'Emma', 'salary': 8000},
'emp3': {'name': 'Brad', 'salary': 8500}
}

OOP PROBLEMS

a) Create a Vehicle class with max_speed and mileage instance


attributes.
b) Create a Vehicle class without any variables and methods
c) Create child class Bus that will inherit all of the variables and
methods of the Vehicle class
d) Class Inheritance
Given: Create a Bus class that inherits from the Vehicle class.
Give the capacity argument of Bus.seating_capacity () a default
value of 50. Use the previous code for your parent Vehicle
class. You need to use method overriding.

Small project

Requirements

Part I: Members, Students and Instructors


You're starting your own web development school called Codebar! Everybody at
Codebar - whether they are attending workshops or teaching them - is a Member:

 Each member has a full_name.


 Each member should be able to introduce themselves (e.g., "Hi, my name is
Kevin!").

Each Member is also either a Student or an Instructor:

 Each Student has a reason for attending Codebar (e.g., "I've always wanted to
make websites!").
 Each Instructor a bio (e.g., "I've been coding in Python for 5 years and want to
share the love!").
 Each Instructor also has a set of skills (e.g., ["Python", "Javascript", "C++"]).
 An Instructor can gain a new skill using add_skill.

Part II: Workshops


Codebar also has Workshops. Each Workshop has:

 A date.
 A subject.
 A group of instructors.
 A roster of students.
 An add_participant method that accepts a member as an argument. If the
Member is an Instructor, add them to the instructors list. If a Member is a
Student, add them to the students list.

Create another method print_details that outputs the details of the workshop.

Test Your Code


Make your code work for the following calls and print out the response you can see in
the comments below:

workshop = Workshop("12/03/2014", "Shutl")

jane = Student("Jane Doe", "I am trying to learn programming and need some
help")
lena = Student("Lena Smith", "I am really excited about learning to
program!")
vicky = Instructor("Vicky Python", "I want to help people learn coding.")
vicky.add_skill("HTML")
vicky.add_skill("JavaScript")
nicole = Instructor("Nicole McMillan", "I have been programming for 5 years
in Python and want to spread the love")
nicole.add_skill("Python")
workshop.add_participant(jane)
workshop.add_participant(lena)
workshop.add_participant(vicky)
workshop.add_participant(nicole)
workshop.print_details
# =>
# Workshop - 12/03/2014 - Shutl
#
# Students
# 1. Jane Doe - I am trying to learn programming and need some help
# 2. Lena Smith - I am really excited about learning to program!
#
# Instructors
# 1. Vicky Ruby - HTML, JavaScript
# I want to help people learn coding.
# 2. Nicole McMillan - Ruby
# I have been programming for 5 years in Ruby and want to spread the love
#

Bonus
The print_details method currently does a number of different things, like printing out
workshop details, the list of Students and the list of Coaches.
Create separate methods to print the workshop details (date and classroom), a method
to print out the students and one to print out the coaches. Call these
from print_details instead of having all the code there.
Hint: look into defining private class methods.

You might also like