Python
Python
Challenge:
Write an algorithm for
something you do exactly
the same every day. Be as
specific as you can.
5 in 5
Last few
lessons
• What is an algorithm?
• Give 2 examples of an algorithm we use every day with technology
• How is an application made?
• What is coding?
• What is a programming language?
• Which command would I use in python to show some text on the
screen?
• How would I add a variable to my code?
• How would I take an input from the user
Challenge:
Write some code that
takes an input from the
user then shows the
input to the user
Sometimes we want to describe the exact
input we want
• This code asks the user for their age. If something that is not a
number is typed in then this error message will be shown.
Calculations
• Write a program in your books that prints the words ‘Hello world’
• Write a program in your books that takes an input of a number and
then outputs the next number (ie input 6, output 7)
• How do I use pi in python?
• How do I square a number in python?
Challenge: Write a
program that finds
the area of a circle
Sequence Selection and Iteration
• In programming and algorithms there are 3 main types of code we
can write.
• The first of these we have seen already: sequence
• This means we process one instruction after another: step –by-step
Variable Print Statement
Input
Calculation
Your
• Logturn
onto the computers and write a sequence of code with at
least 5 lines.
• Include at least one variable, print statement, calculation and
input.
• Test your code to ensure it works.
• Try to do this without looking at my code. Only use it if you need
it.
Selection
• What could the word selection mean?
Selection
• Selection means we have to make a choice.
• This happens by using something called an if-else statement.
• It works by saying: if condition is true then do this
else(otherwise) then
do this
5 in 5
All last lesson
Challenge: Write a
program with an elif
statement
Task
• Add an if-else statement to your code.
Things to remember: Colons after the if and else statement
Use == to check if something is
true
Anything you want the program
to do in the if or else needs to be indented(Use
the tabs button)
Use my example to help you
Challenge:
Research how
to use elif in
Python. Add
one to your
code.
Iteration
Iteration
• Iteration means to repeat something.
• In Python we can do this in two ways: using a for loop or a while loop.
• Both loops are useful in different ways.
• We use a for loop when we want to repeat something a certain
For loop
number of times
What we want it to
do each time
For •loop
Our variable goes up by 1 every time it repeats
• We can print the number our variable stores to count up
Task
• Create a for loop that prints out a user’s input 20 times.
• The user should be asked to input, then their input should be
printed out 20 times.
Challenge:
Is it possible to make a
loop that goes forever?
How? (HINT: research
while loops)
5 in 5
Last few lessons
Challenge: Write a
program that prints out
a word forever
• We use a for loop when we want to repeat something a certain
For loop
number of times
What we want it to
do each time
We use a while loop when we want to repeat something while a
While loop
condition is true.
Variable
Condition
What we want it to
do each time
Task
• Create a program that will print out the numbers from 1 to 20
using a while loop
Challenge:
Repeat question 5
but in a different
way
Which of these are sequence, selection,
iteration?
Arrays
• An array is a special type of variable that can hold more than one
item.
• We sometimes call an array a list.
• Arrays use square brackets instead of curved ones
• Items in an array are separated by commas
Information
Variable name separated by
commas
Task
• Create a list of 5 items.
• Print out each element of the list on a separate link
Finding out the length of a list
• We can also find out the length of a list.
• This tells us how many items are in the list.
Task
• This is particularly useful when we want to print out an entire list
without an error.
• This can be done using a for loop.
• Write a for loop that prints out an entire list. You will need to use the
len function to do this.
Challenge:
Do the same but with
a while loop
5 in 5
Last few
• What do sequence, selection and iteration mean? lessons
Challenge:
Information
Variable name separated by
commas
• This is particularly useful when we want to print out an entire list
without an error.
Task
• This can be done using a for loop.
• Write a for loop that prints out an entire list. You will need to use the
len function to do this.
Challenge:
Do the same but with
a while loop
5 in 5
Last few
• What do sequence, selection and iteration mean? lessons