PROGRAMMING
IN PYTHON
Ms Mirza - Week 3
10 WEEK COURSE
Introduction to IDLE, basic input & output, data types, operators, casting, variables &
05/10/17
assignment.
12/10/17 Selection, nested selection 'if & elif', string manipulation, commenting code
Key Words
Using modules, Iteration 'For & While loops’
19/10/17
Variables
02/11/17 Strings & slicing, Lists -searching/sorting/list manipulation/operations Assignment
Casting
09/11/17 Multi-dimensional lists, exception handling, common mistakes and debugging Operators
Concatenate
16/11/17 Functions and procedures, functions with parameters Identifier
File handling, reading from files, writing to files
Sequencing
23/11/17 Selection
Code challenges, NEA examples & tasks Iteration
30/11/17
Arrays
Databases & SQLite
07/12/17
Regular expressions & Dictionaries
14/12/17
THE FOR LOOP
Iteration - definite
ITERATION – FIXED LOOP
Iteration is the process of repeating a process over and over.
Often in programming you need to repeat a block of code Key Words
several times.
Variables
Assignment
Casting
Operators
Concatenate
Identifier
Sequencing
Selection
Iteration
• A for loop will repeat for a set number of times, this is a Arrays
DEFINITE loop. range([start], stop[, step])
• It will repeat between two ranges
• It will start at the bottom value but not count the top value
EXAMPLE OF A FOR LOOP
What will be displayed Key Words
on the screen when
this program runs? Variables
Assignment
Casting
Operators
Concatenate
Identifier
IMPORTANT Sequencing
• 5 values are printed Selection
• From the start number Iteration
• But do not include the Arrays
end number
EXAMPLE OF A FOR LOOP WITH TEXT
What will be displayed Key Words
on the screen when
this program runs? Variables
Assignment
Casting
Operators
Concatenate
Identifier
Sequencing
Selection
This is the output Iteration
Arrays
EXAMPLE OF A FOR LOOP WITH TEXT
What will be displayed Key Words
on the screen when
this program runs? Variables
Assignment
Casting
Operators
Concatenate
Identifier
Sequencing
This is the output Selection
Iteration
Arrays
START….STOP…STEP
Step in 2’s
Start at 0
Stop at 10-1 Key Words
Variables
Assignment
Casting
Operators
Concatenate
Identifier
Sequencing
Selection
Iteration
Arrays
START….STOP…STEP
Key Words
What will be displayed
on the screen when Variables
this program runs? Assignment
Casting
Operators
Concatenate
Identifier
Sequencing
This is the output Selection
Iteration
Arrays
KEY LEARNING POINTS
▪ An iteration is a section of code that is repeated.
▪ Iterations are also known as loops.
Key Words
▪ Counter controlled iterations are used when you want to iterate a
known number of times. Variables
▪ Counter controlled iterations use a variable which can be used Assignment
within the code to know how many times the code has been run. Casting
Operators
▪ Code must be indented within an iteration.
Concatenate
▪ It would be good practice to comment code before an iteration to Identifier
explain what is happening. Sequencing
▪ Incrementing a variable increases its value by one. Selection
Iteration
▪ Decrementing a variable decreases its value by one. Arrays
▪ One ‘for’ loop can be put inside another ‘for’ loop. This is known as
nesting.
KEY WORDS
Key Words
Key word Example code Purpose
Variables
for() for counter in range(0,5): Counter is the variable that is counting
Assignment
print(counter) how many times the code has iterated. 0 is
the starting value (default). 5 is the stopping Casting
value. The starting value does not need to Operators
be specified if zero. Concatenate
Identifier
Sequencing
for counter in range(10,0,-1): By default this is 1 and therefore step 1 is Selection
print(counter) not necessary. To count backwards, a Iteration
negative value can be used Arrays
for step.
CHALLENGES
Square numbers challenge
Key Words
Variables
Assignment
Casting
Operators
Concatenate
Identifier
Sequencing
Selection
Iteration
Arrays
CHALLENGES
9 green bottles
▪ Write a program that prompts the user to enter the number of bottles.
The program outputs: Key Words
9 green bottles sitting on the wall
8 green bottles sitting on the wall Variables
7 green bottles sitting on the wall etc. etc. Assignment
Casting
Times table challenge Operators
▪ Write a program that asks the user to enter a number between 1 and Concatenate
Identifier
12. The program outputs the times table of that number between 1
Sequencing
and 12.
Selection
Fibonacci sequence challenge Iteration
Arrays
A number in the Fibonacci sequence is generated by adding the
previous two numbers. By starting with 0 and 1, the first 10 numbers
will be: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55. Write a program to produce the
sequence of 20 numbers.
CHALLENGES
The average challenge
▪ Write a program that asks the user to enter how many numbers are to be
averaged. The user can then enter the numbers. The program outputs the Key Words
total and the mean.
Variables
Fizz buzz challenge
Assignment
▪ Write a program that outputs the numbers from 1 to 100. But for multiples Casting
of three output “Fizz” instead of the number and for the multiples of five Operators
output “Buzz”. For numbers which are multiples of both three and five Concatenate
output “FizzBuzz” Identifier
Extended times table challenge Sequencing
Selection
▪ Write a program that could be used in an infant school to prompt a child Iteration
with ten simple random 1 digit maths calculation, e.g. 4*7= The user Arrays
enters their answer and the computer tells them if they are correct.
▪ At the end of the test the program outputs how many problems the child
got right.
CHALLENGES
Letter game challenge
Key Words
Variables
Assignment
Casting
Operators
Concatenate
Identifier
Sequencing
Selection
Iteration
Arrays
CHALLENGES
ROT13
• ROT13 is a simple letter substitution cipher that replaces Key Words
a letter with the letter 13 letters after it in the alphabet.
Variables
• ROT13 is a special case of the Caesar cipher, developed Assignment
in ancient Rome. Casting
• ROT13 is used in online forums as a means of hiding Operators
Concatenate
spoilers, punchlines and puzzle solutions from the casual Identifier
glance. Sequencing
• Create a program that allows the user to enter plain text, Selection
Iteration
and the ROT13 cipher is output. Arrays
• Extend the program to allow cipher text to be input, with
the plain text output.
THE WHILE LOOP
Iteration - indefinite
ITERATION – WHILE LOOP
▪While loop repeats the sequence of actions many times
until some condition evaluates to True Key Words
while [test condition]: Check the Variables
[command] condition Assignment
Casting
Operators
Initialise Concatenate
variables Identifier
Sequencing
Selection
Iteration
Condition must Arrays
change
GUESS THE OUTPUT
Key Words
Variables
Your inputs are: Assignment
john, kate and Casting
bob Operators
Concatenate
Identifier
Sequencing
Selection
Iteration
Arrays
This is the output
WHILE LOOP FLOWCHART
Key Words
Variables
Assignment
Casting
Operators
Important: There must be Concatenate
statements within the while Identifier
loop that will allow the condition Sequencing
to become false, or else Selection
we could be in a situation where Iteration
a loop executes forever Arrays
ITERATION – WHILE LOOP
What will be displayed
on the screen when Key Words
this program runs?
Enter the numbers Variables
4,5,6,0 Assignment
Casting
Operators
Concatenate
Identifier
Sequencing
Selection
Iteration
Arrays
This is the output
ITERATION – WHILE LOOP
Key Words
Variables
Assignment
Casting
Operators
Concatenate
Identifier
Sequencing
Selection
Iteration
Arrays
This is the output
WHAT IS THE OUTPUT?
Key Words
Variables
Assignment
Casting
Operators
Concatenate
Identifier
Sequencing
Selection
Iteration
This is the output Arrays
KEY LEARNING POINTS
▪ Subroutines are sections of code to break a longer programs into smaller
pieces. Thereby making them easier to read and more manageable for teams
of programmers to work together on one program.
▪ Subroutines are also known as procedures.
Key Words
▪ Functions carry out small tasks on data by taking one or more parameters Variables
and returning a result. Assignment
▪ Subroutines/procedures may also take parameters, but do not return a result. Casting
▪ It is good practice to comment each subroutine or function to explain its Operators
purpose. Concatenate
Identifier
▪ Variables declared outside of all subroutines are known as global variables.
Sequencing
They are available to all subroutines and functions.
Selection
▪ Global variables are not memory efficient since they hold memory for the Iteration
entire time a program is running. They should therefore be kept to a minimum. Arrays
▪ Variables declared inside a subroutine or function are known as local
variables. They lose their value when the routine ends, releasing memory.
▪ Passing variables between subroutines is known as parameter passing.
KEY WORDS
Key word Example code Purpose
.upper() x = [Link]() To turn a string into uppercase.
x is the name of the variable to return the Key Words
result to. y is the original variable.
Variables
.lower() x = [Link]() To turn a string into lowercase. Assignment
Casting
Operators
len() x = Len(y) To return the number of characters in a string. Concatenate
x becomes the number of characters in y. Identifier
Sequencing
Selection
Iteration
Arrays
CHALLENGES
Menu challenge
▪ Write a program that presents a simple menu to the user: 1. Play Game 2.
Instructions 3. Quit Key Words
▪ The program should ask the user for a number between 1 and 3 and will output
“Let’s go” only when a valid entry is made, repeating the menu selection until a valid Variables
entry is made. Assignment
Guess the number game challenge Casting
▪ The computer guesses a number between 1 and 100. The player has to try and guess Operators
the number in as few attempts as possible. When the user enters a number they are Concatenate
told if the guess is too high or too low until the number is guessed correctly. The Identifier
player is told how many guesses they made. Write a program to play the game. Sequencing
▪ Rock, paper, scissors challenge Selection
▪ The computer and player choose one of rock, paper, or scissors. The output of the
Iteration
encounter is then displayed with rock beating scissors, scissors beating paper, and Arrays
paper beating rock.
▪ The winner scores 1 point for a win. The score for both players should be output. The
game is won when one player reaches 10 points.