Python

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 58

5 in 5

• What is a network? All this term


• What is a LAN?
• What is software?
• What is a computer program?
• What is an application?
Challenge: How do you
think computer programs
and applications are made?
Be as specific as you can.
Algorithms:
What is an algorithm?
Algorithms:
What is an algorithm?
• A step-by-step set of instructions for solving problems. The order in
which you carry out the steps in this process is very important.
• Can you think of any algorithms we may use in the real world?
Who uses algorithms?
• Search engines(Google)
• Game Makers(eg Fifa)
• School timetabling(SIMS)
• Self driving cars
• Apps(Instagram/Tiktok)
• Chefs(eg Recipes)
• Doctors(making a diagnosis)
Algorithm for cleaning teeth

Here is a simple algorithm for Go to bathroom


cleaning your teeth. We know it Pickup toothbrush
is an algorithm because it is a Pickup toothpaste
series of steps used to solve a Put toothpaste on tooth brush
problem. Put toothbrush in mouth
Put toothpaste down
Clean teeth
Can this algorithm be improved? Take toothbrush out of mouth
Put toothbrush down
Leave bathroom
Algorithm for cleaning teeth
Go to bathroom
One step shown is “Clean teeth”. Pickup toothbrush
Pickup toothpaste
Could we develop our algorithm
Put toothpaste on tooth brush
by including more steps to show
Put toothbrush in mouth
what happens when you are
Put toothpaste down
actually cleaning your teeth?
Clean teeth
What steps do you take when Take toothbrush out of mouth
you are cleaning your teeth? Put toothbrush down
Leave bathroom
Algorithm for cleaning teeth
Go to bathroom Move brush at an angle of 90 degrees (in
Pickup toothbrush relation to the surface of the tooth) to the
bottom right tooth (back molar)
Pickup toothpaste Move the brush in a circular motion against
Put toothpaste on tooth brush the tooth (circles of radius 0.4 cm)
After 3 seconds move the brush one tooth
Put toothbrush in mouth
along towards the centre of the mouth.
Put toothpaste down (repeat this until you reach the bottom left
molar tooth)
Clean teeth
Move brush at an angle of 90 degrees (in
Take toothbrush out of mouth relation to the surface of the tooth) to the top
right tooth (back molar)
Put toothbrush down
Leave bathroom
Your turn
• Write an algorithm for Mr Bersey to throw a ball of paper in the bin.
He will start at the back corner of the classroom with the paper on his
desk.

• Be as specific as you can.

• If you finish go through the algorithm in your head – could anything


go wrong at any point? What could you change to avoid that
happening?
Your turn
• Log onto the computer
• Take note of each step as you do.
• Once you have logged on, open a word document and write an
algorithm for logging onto the computer.
5 in 5
• What is an algorithm?
• What can algorithms be used for?
• Give 3 examples of algorithms in real life. All last lesson

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?

Challenge: Do you think


some applications are
easier to make than others?
Why?
What is coding?
• Coding is creating a set of instructions for a computer to follow. By
creating code we can tell computers what to do or how to behave.
• A programming language is a set of rules we have to follow in order to
code.
Basics
• Go to your computers. Find a program called idle and open it. It
should look like this.
• Click on the file button and click new. This will make a new program.
Everyone's first program

• Type this code into your program.


• Once you have done this you need to save your program.
• After your program has saved click F5 to run it. See what it does.
• Change the program to say different things. You will need to save
your program each time.
Variables
Variable name
Information

• In programming a variable will store one piece of information


• Add a variable to your code like this
• Run your code to see if it works
• You can use more than one variable, each variable needs to be
separated by a comma.
• Add some more variable and some different print statements to
your code
InputsVariable nameTell us the
user has to Tells the user
input which
information information to
type

• Lots of code takes an input from the user.


• Change your code so it uses an input.
• Use more than one input in your code if you would like your user
to input more than one piece of information.
Task
• Write some code that asks the user to input their first name, then
their last name, then their date of birth and their favourite football
team.
• Use Print statements to show this information after the user has input
it.
Calculations

• You can do calculations in Python too


• Here we use a variable to store the number and then we print the
number to the screen.
• Try this code on your computer
• Change some of the numbers around, how big a number can you
make?
5 in 5 All last lesson

• 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

• Instead of storing text in variables we can store numbers in


variables too.
• We can change these numbers like we would in Maths by using +,
-. *, /
• Write a program that asks the user for their age and tells them
Task how old they will be in 20 years time. This will need to be shown
on the screen like this:

Challenge: Change your code


so it asks for the users
birthday and calculates the
same thing.
HINT: Use birth year if
unsure
Task
• We can use pi in python too. We need to use the code ‘import
math’ as out first line of code. We can then use math.pi to
represent the number pi.
• In maths the area of a circle is equal to pi times the radius of the
circle squared. We can square a number in python like this

• Create a program that takes a user input for the


radius of a circle and then calculates the area. This
should be output to the screen
Task
• We can also find the remainder of a calculation in python. For
example 7 divided by 3 is 2 remainder 1. The code 7%3 will output 1.

• Write a program that uses both pi and the % function.


5 in 5
All last lesson

• 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

• In coding what is sequence? Give an example.


• In coding what is selection? Give an example.
• What is an elif statement

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

One above number


Variable Number we want to we want to end at
start at

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

• Which command do I use to show a statement on the screen?


• Which command do I use to allow the user to give the program
some information?
• Which command do I use to give the user a choice?
• How could I get the program to repeat instructions?
• Write a program that counts from 1 to 10 then prints the word
‘finished’

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

One above number


Variable Number we want to we want to end at
start at

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: write the same


code in a for loop. Which
do you think is better?
Why?
• What have we used so far?
• Print
• Variable Checkpoint
• Input Write a program using at least 6
• Int(input) of these
• Calculations programming constructs
• If, elif, else
• For
• While
5 in 5
All this term
• What does iteration mean?
• Give 2 different examples of iteration.
• What is a variable?
• What can I store in a variable?
• Write some code that will print out the word hello 10 times.

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

• What does a for loop do? Write an example.


• What does a while loop do? Write an example.
• What is an array?
• Why would I use an array?

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

• What does a for loop do? Write an example.


• What does a while loop do? Write an example.
• What is an array?
• Why would I use an array?
Challenge:
How could we print a
noughts and crosses
grid using an array?
We can also have a list of lists.
This is sometimes called a 3D array.
• Python Basics Quiz - Details - Kahoot!
• Python Basics - Details - Kahoot!

You might also like