How can you Introduce Python programming to middle school students?

This is the third part of my article about teaching computer science at Community Montessori School, Tampa.

In previous article, I shared how I taught computer science algorithms. In this article, I will write about how I taught python programming to 7 to 9th grade students.

How I prepared?

Since I already taught them Search algorithms and guessing number game algorithm in previous session. I wanted to teach them just enough python programming to turn those algorithms to python code.

I used https://repl.it to do live coding demonstration for code examples in the slide. I also used a awesome tool pythontutor to visualize the execution of code step by step. Watching the variables change and repeating steps over and over again helped them easily understand how the program work.

Presentation

Transcript

Here is the complete transcript of the talk.

Introduction To Python

  • Python is a good general-purpose language that can be used in a variety of applications.
  • Python is a very human readable, programming language.
  • Python is a very good choice for beginners.

Basic Data Types

Python Variables

variableName = value

Examples

N = 100 # Type int
answer = "yes" # Type str
found = True # Type bool

Python Conditions

if condition1 :    
do something when condition1 is true
elif condition2 :
do something when condition2 is true
else:
do something when all conditions are false

Python condition example:

if signal=="red" :    
print ("Stop")
elif signal=="green" :
print ("Go")

Python while loop

while condition:    
repeat something

The loop will end either if condition becomes false or if there is a break statement.

While loop example

x = 1
while x <= 5:
print ("|")
x = x + 1
x = 1
while True:
print ("|")
if x >= 5:
break
x = x + 1

Input and Output

print ("Message you want to say to users") 
variable=input("Read something from users")

Input and Output Example

name = input(“What is your name? “)
print (“Your name is “ + name)
age = int(input(“What is your age? “))print (“Your age is “ + str(age))

Python Program for Guessing Number Game

  1. Think of a number between 1 and 100.
  2. Computer will make a guess and you will answer whether the guess is correct or low and higher than the computer’s guess.
  3. Computer will make next guess based on the information and you will respond again with correct or low or high.
  4. Repeat this until computer makes right guess.

Guessing Number Game Visualization

Python for loop

for item in list_of_times:    
do something with item

Examples

numbers=[1,2,3,4,5]
for n in numbers:
print (n)

Program for Sequential Search

Sequential Search Visualization

Python program for Binary Search

Binary Search Visualization

Exercise Inverse Guessing number Game

This is inverse Guess number game we programmed earlier. Instead of computer guessing your number, you have to guess computer’s number.

  1. Computer will think of a number between 1 and 100.
  2. You will make a guess and computer will answer whether the guess is correct or low and higher than the your guess.
  3. You will make next guess based on the information and computer will respond again with correct or low or high.
  4. Repeat this until you make a right guess.

First, I demoed the game in repl.it without showing them the code.

It will be hard for students to write the program but at the same time I don’t want to just show them the solution. So I designed an exercise — I printed the code in a paper and cut them in to several pieces. Then shuffled the pieces and asked them to arrange it in correct order. Also I left some blanks in the code for them to fill in. Here is how I gave it to students.

Guessing Number Game Programming Exercise

The students had fun in solving it. Here is the solution from one of the group.

Solved guessing number game by students

Searching for Max Value

Let’s say we have list of numbers:

numbers = [10,50,20,30,100,60]

You need to find the Maximum value in the list which is 100 in this example.

Solving using Best Score analogy

  • Let’s say you are playing a game severals times.
  • You will be getting a score at end of each game.
  • You will have to calculate the best score you have got so far.
  • You can use same approach to search for Max.

Example usage of Best score analogy

Let’s apply the best score analogy to above numbers to find max.

  • Initially, You haven’t played the game. So what will be best score ? 0 . The best score so far 0.
  • If the first try, you scored 10 points, so what is best score now? 10. Because 10 is greater than the best score so far ( 0 )
  • In the next try you score 50 points, so what is best score now? 50 . Because 50 is greater than the best score so far ( 10 )
  • In the next try you score 20 points , so what is the best score now ? 50. Because 20 is smaller than best score so far ( 50 )
  • In the next try you score 30 points , so what is the best score now ? 50. Because 30 is smaller than best score so far ( 50 )
  • In the next try you score 100 points, so what is best score now? 100 . Because 100 is greater than the best score so far ( 50 )
  • In the next try you score 60 points , so what is the best score now ? 100. Because 60 is smaller than best score so far ( 100 )
  • We checked all the score, So the final best score or max value is 100.

Program for Searching for Max

Searching for Max Visualization

Resources

Hello World: Computer Programming for Kids and Other Beginners — Book

Python for Kids: A playful introduction to Programming — Book