0% found this document useful (0 votes)
19 views5 pages

Python Exercises for Beginners

The document provides instructions for 9 Python questions including printing patterns, calculating sums, iterating through lists, and modifying dictionaries. It also includes object-oriented programming problems defining Vehicle and Bus classes as well as a small project to create a code school workshop tracking system with Members, Students, Instructors, and Workshops.

Uploaded by

Herton Fotsing
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views5 pages

Python Exercises for Beginners

The document provides instructions for 9 Python questions including printing patterns, calculating sums, iterating through lists, and modifying dictionaries. It also includes object-oriented programming problems defining Vehicle and Bus classes as well as a small project to create a code school workshop tracking system with Members, Students, Instructors, and Workshops.

Uploaded by

Herton Fotsing
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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.

Common questions

Powered by AI

To find the last occurrence of a substring in a Python string, you can use the rfind() method, which returns the highest index at which the substring is found. For example, "Emma" in the string "Emma is a data scientist who knows Python. Emma works at google." has its last occurrence starting at index 43, obtained using str.rfind('Emma').

To implement class inheritance and method overriding in Python, define a base class with methods to be inherited. A derived class, such as Bus from Vehicle, inherits attributes and methods of Vehicle. Method overriding is done by redefining a method in the derived class with the same name and signature but with customized behavior. For example, overriding Bus.seating_capacity() to have a default capacity can redefine the inherited method to alter the default functionality from the base class .

To generate a list of even numbers within a specific range in Python, you can use the range function with a step of 2 starting from the first even number. For example, to get even numbers from 4 to 30, use range(4, 31, 2) and convert it into a list. This list comprehension efficiently captures all even numbers in the given range .

To generate a pattern where each row contains incremental numbers starting from 1 up to the number of the row, you can use nested loops. The outer loop runs from 1 to the desired number of rows, and the inner loop runs from 1 to the current row number. This approach prints each number on the same line for a row and starts a new line for each new row .

To remove empty strings from a list in Python, use list comprehension to iterate through the list and include only non-empty strings. The expression can be written as [string for string in list1 if string] which evaluates and constructs a new list excluding any empty string elements from list1 .

A Python function can take a user-provided integer as input and calculate the sum of all integers from 1 to that number using a loop or a mathematical formula. The loop-based approach involves initializing a total variable to 0 and iterating from 1 to the user's number, adding each integer to total. An alternative is using the formula n*(n+1)/2 for the sum of the first n natural numbers .

You can apply object-oriented principles to modify attributes in a nested dictionary by directly accessing the keys corresponding to the desired attribute and assigning them new values. In the dictionary sampleDict, setting Brad's salary to 8500 can be achieved by sampleDict['emp3']['salary'] = 8500. This approach optimally navigates the layers of the dictionary structure to update specific values .

A recursive method to compute the sum of numbers in Python involves defining a function that calls itself with a decremented value until reaching a base case that stops recursion. The base case for summing numbers from 0 to 10 would return 0 when reaching negative one, while otherwise returning the current number plus the result of the function with the previous number. Recursion thus allows continuous summing until the base case ceases further calls .

An algorithm to iterate over a list with specific conditions involves using a loop to go through each element. You apply a modulus operation to check divisibility by 5, and if true, print the number. If the element exceeds 150, use a break statement to exit the loop. This prevents any further iteration once the stop condition is met .

One efficient way to remove all occurrences of a specific value from a list is by using list comprehension to rebuild the list without the specified value. For instance, list1 = [5, 20, 15, 20, 25, 50, 20] can be reformed using [x for x in list1 if x != 20], which filters out all instances of 20 from list1 .

You might also like