Python Programming course

This Python course is aimed to be completed in one semester. It is an introductory course to Python, meaning that the students will not require prior knowledge of Python for this course

Lesson plan:

Lesson # Lesson
Lesson 1 print()
Lesson 2 Using math
Lesson 3 Variables
Lesson 4 input()
Lesson 5 Comments
Lesson 6 if(), elif(), else()
Lesson 7 Understanding strings, integers, and floats
Lesson 8 Operators
Lesson 9 While loops
Lesson 10 For loops
Lesson 11 Randomness
Lesson 12 Unit test 1
Lesson 13 Tuples
Lesson 14 Lists
Lesson 15 Dictionaries
Lesson 16 Functions
Lesson 17 Understanding scopes
Lesson 18 Modules
Lesson 19 Unit test 2
Lesson 20 Reading from text files
Lesson 21 Writing to a text file
Lesson 22 Pickling
Lesson 23 Hnadling exceptions
Lesson 24 Unit test 3
Lesson 25 Objects
Lesson 26 Classes
Lesson 27 Methods
Lesson 28 Attributes
Lesson 29 Printing objects
Lesson 30 Properties
Lesson 31 Sending and recieveing messages between classes
Lesson 32 Extending a class through inheritance
Lesson 33 Unit test 4

Unit test 1:

1) Print the string "Hello world!"


print("Hello world!")

2) Create a program that adds two numbers together

Example answer:

number1 = int(input("Enter the first number: "))

number2 = int(input("Enter the second number: "))

answer = number1 + number2


3) How do you create comments, how does Python treat them, and why do we use comments

Example answer:

Comments are started with #, Python doesn't do anything with comments, however comments are useful for others to understand our code

4) Create a program that simulates throwing a six-sided die, by having the throw be random, and then printing the result to the user

Example answer:

inport random

throw = random.randint(1,6)

print("The dice landed on", throw)

5) Create a program that prints "Hello!" then the user says "Hello", exits the program when the user says "Goodbye", and tells the user that it did not understand what they said if the user does not say "Hello" or "Goodbye"

Example answer:

exit_program = 0

while(exit_program == 0):

answer = input("What will you say to the program?: ")

if(answer == "Hello"):


elif(answer == "Goodbye"):

exit_program = 1


print("I don't understand")

6) Create a program that exits itself if the user answers "y" or "Y" to the question: "Exit the program?" using an operator

Example answer:

loop_exit = 0

while (loop_exit == 0):

answer = input("Exit program? y/n")

if(answer == "y" or answer == "Y"):

loop_exit = 1

7) Using a for loop, create a program that increments a number by 1 5 times, and then prints the number after each increment

Example answer:

number = 0

for x in range(0, 5):

number += 1


Unit test 2

1) Create a tuple containing: "chair", "table", "floor"

Example answer:

example_tuple = ("chair", "table", "floor")

2) Create a list containing: "chair", "table", "floor"

Example answer:

example_list = ["chair", "table", "floor"]

3) Create a dictionary containing three items

Example answer:

example dictionary = {"item1_1":"item1_2",



4) Create a function that prints "Hello world!" and run it

Example answer:

def hello_world():

print("Hello world!")


5) In a new python script, assuming the file name is, import the previous function and run it in the new script

Example answer:

from functions import hello_world


Unit test 3

1) Open a text file called test.txt in read only mode from python (assuming the test.txt file is located in the Python directory), then print out the first 3 lines from the file

Example answer:

text_file = open("test.txt", "r")

for x in range(0, 3):



2) Create a text file called test.txt in write only mode from python, then have Python write the following lines:

This is line 1

This is line 2

This is line 3

Example answer:

text_file = open("test.txt", "w")

text_file.write("This is line 1\n")

text_file.write("This is line 2\n")

text_file.write("This is line 3\n")


3) Create 3 lists, each with 3 elements. Pickle the 3 lists and then unpickle them and print the lists

Example answer:

import pickle, shelve

print("Creating lists...")

list1 = ["element1_1", "element2_1", "element3_1"]

list2 = ["element1_2", "element2_2", "element3_2"]

list3 = ["element1_3", "element2_3", "element3_3"]

print("Pickling lists...")

f = open("pickles1.dat", "wb")

pickle.dump(list1, f)

pickle.dump(list2, f)

pickle.dump(list3, f)

print("Unpickling lists...")

f = open("pickles1.dat", "rb")

list1 = pickle.load(f)

list2 = pickle.load(f)

list3 = pickle.load(f)

print("Printing lists...")




4) You have created a program that takes a float given by the user and multiplies it by 5. Give you code the ability to recognize if a value is not a number and have the user try again assuming you have the following code:

number = float(input("Enter the number you need multiplied by 5: "))

answer = number * 5


Example answer:



number = float(input("Enter the number you need multiplied by 5: "))



print("Thats was not a number, please try again!")

answer = number * 5


Unit test 4

1) You have been hired by a bank to create a program that uses objects to store accounts. Use attributes to more easily specify the starting funds of an account, as well as having the funds show if you print the object. Make sure to code in a way to add and remove funds from an account

Example answer:

class CheckingAccount(object):

"""A checking account"""

def __init__(self, funds):

self.funds = funds

def __str__(self)

k = "This account currently has $"

k += str(self.funds)

return k

account1 = CheckingAccount(100)

account2 = CheckingAccount(1000)


print("Welcome to test bank")

print("1 - Check the funds of an account")

print("2 - Remove funds from an account")

print("3 - Add funds to an account")

print("4 - Exit")

answer = input()

if (answer == "1"):

accountNumber = input("Specify which account to check: ")

if (accountNumber == "1"):


elif (accountNumber == "2"):



print("That is not a valid account!")

elif (answer == "2"):

accountNumber = input("Specify which account to change: ")

if (accountNumber == "1"):

removeFunds = int(input("How much money to remove?: "))

account1.funds -= removeFunds

elif (accountNumber == "2"):

removeFunds = int(input("How much money to remove?: "))

account2.funds -= removeFunds


print("That is not a valid account!")

elif (answer == "3"):

accountNumber = input("Specify which account to change: ")

if (accountNumber == "1"):

addFunds = int(input("How much money to add?: "))

account1.funds += addFunds

elif (accountNumber == "2"):

addFunds = int(input("How much money to add?: "))

account2.funds += addFunds


print("That is not a valid account!")

elif (answer == "4"):



print("That is not a valid choice!")

2) Create a private attribute and use a property to return its value

Example answer:

class Example(object):

"""An object to test private attributes"""

def __init__(self, vatiable)


self.__variable = variable


def variable(self):

return self.__variable

def test_variable(self):

print("The private variable is:", self.variable)

example_object = Example("test")


3) Send a message from one class to another, and have the other class do something with it

Example answer:

class Object1(object):

"""The first object"""

def send_message(self, reciever):

print("Sending the message")


class Object2(object):

"""The second object"""

def recieve_message(self):

print("Message recieved")

test1 = Object1()

test2 = Object2()


4) Create two classes and have one class inherit all the methods from the other

Example answer:

class Parent(object):

"""The parent class"""

def test1(self):

print("This is the inherited method

class Child(Parent):

"""The child class"""

child = Child()