0% found this document useful (0 votes)
17 views

Python 2 While and For Loops and Functions

This document provides an introduction to Python loops and decisions through a series of coding examples and exercises. It covers if/else statements, while loops, for loops, nested for loops, and functions. The learner is guided through examples and tasks to practice each concept, with explanations of how the code works. More advanced concepts like nested loops and functions are introduced at a GCSE level.

Uploaded by

lsimm2020
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Python 2 While and For Loops and Functions

This document provides an introduction to Python loops and decisions through a series of coding examples and exercises. It covers if/else statements, while loops, for loops, nested for loops, and functions. The learner is guided through examples and tasks to practice each concept, with explanations of how the code works. More advanced concepts like nested loops and functions are introduced at a GCSE level.

Uploaded by

lsimm2020
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Name: Lloyd 81s

Python Introduction W3Schools Part 2


Loops and Decisions
FIRST TASK: Add your name to the header.
All the information you need is in the links, if you get stuck the links are there to help you.

Python website: https://repl.it/languages/python3 (Use Chrome)

1 If Statements (https://www.w3schools.com/Python/python_conditions.asp )

If statements can do things based on a decision. An "if statement" is written by using


the if keyword. For example, you might want to check if A is equal to B, and print
“well done” if it is.

Here are examples of logical conditions which are used to make decisions.

 Equals: a == b
 Not Equals: a != b
 Less than: a < b
 Less than or equal to: a <= b
 Greater than: a > b
 Greater than or equal to: a >= b

a = 33
b = 200
if b > a:
print("b is greater than a")

Add a printscreen below and explain what the code does.

Explain what would happen if a was changed to 201?


nothing
____________________________________________
#This code uses != which means not equal to.
a = 10
b = 20
if b != a:
print("b is not equal to a")
Name: Lloyd 81s

Explain what was printed to the screen, explain why it printed this.

B is not equal to a

What happens if you change the variable a to 20? Explain why.

__________________________________
a = 33
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
a and b are equal
Name: Lloyd 81s

Explain what will be printed when this code is run.

What happens if a is 44 and b is 33?


error
Add an else command which will print a suitable sentence if a is greater than b.
Print screen the code you created.
Name: Lloyd 81s
2 While Loops (https://www.w3schools.com/python/python_while_loops.asp)

Definition: With the while loop we can execute a set of statements as


long as a condition is true.
Explain what this means

#Basic While Loop


i = 1
while i < 6:
print(i)
i += 1
What does this code do?
Name: Lloyd 81s
This is an Algorithm for a While Loop

count = 0
while (count < 9):
print ('The count is:', count)
count = count + 1

print ("Good bye!")

What is the variable in this program?

What is the condition?


What happens if the condition is true?
What happens if the condition is false?
What happens if the condition is 7?
What happens if the condition is 10?
What does this line do? (count = count + 1)
Name: Lloyd 81s
For Loops https://www.w3schools.com/python/python_for_loops.asp

A FOR loop repeats a set number of times, we call it count


controlled. For example, for each member of the class, print a
certificate. Another example is for every item in the list, print the
item.

for i in range(1,10):
print(i)

What does this print out?

What happens if you change the 10 to a 100? Print


screen the result and explain what it does.

for i in range(2,10,2):
print(i)
What does this code do?

Explain what happens if you change the numbers to


1,20,2
Name: Lloyd 81s

For loop program: https://www.programiz.com/python-


programming/for-loop
Copy this code and run it.

# Program to find the sum of all numbers stored in a list


# List of numbers
numbers = [6, 5, 3, 8, 4, 2, 5, 4, 11]

# variable to store the sum


sum = 0

# iterate over the list


for val in numbers:
sum = sum+val

# Output: The sum is 48


print("The sum is", sum)

sum is a variable. Explain what it is used for.


Sum is used to

Explain what this part of the program does: sum = sum+val


Name: Lloyd 81s
Create your own program

This is an algorithm for working out times tables. Use this algorithm
to make a program which calculates times tables.

Printscreen your code below showing that it works:

Explain what each part of the code does.

Explain using bullet points the flow of this program.


Name: Lloyd 81s
Nested FOR Loops (Advanced, GCSE level)
A nested loop is a loop inside a loop.
The "inner loop" will be executed one time for each iteration of the
"outer loop":

adjective = ["red", "big", "tasty"] #Outer loop is X


fruits = ["apple", "banana", "cherry"] #Inner loop y

for x in adjective: # Outer loop


for y in fruits: # Inner loop
print(x, y)

Print screen what the code does below:

Explain how this code runs using words. This is called Pseudocode.
Step 1: The outer loop prints the first word “Red”, then it jumps to the inner loop and
prints
Step 2:
STEP 3:

Create a flow diagram to represent what is happening in this


program. You can use an online ( draw.io )
Name: Lloyd 81s
Functions (Advanced, GCSE level)
https://www.w3schools.com/python/python_functions.asp

 A function is a block of code which only runs when you tell it to run.
 Functions help break our program into smaller and modular chunks. As our
program grows larger and larger, functions make it more organized and
manageable.
 Functions help to avoid repetition and makes code reusable.

In Python a function is defined using the def keyword:


To call a function, use the function name followed by a pair of brackets.
#Define a function
def happyBirthdayEmily(): #program does nothing as written
print("Happy Birthday to you!")
print("Happy Birthday to you!")
print("Happy Birthday, dear Emily.")
print("Happy Birthday to you!")

#Call the function


happyBirthdayEmily()
Explain in your own words what a function is.

Explain the difference between defining a function and calling a function.

Here is another example of a Function but there is a mistake. I’ll give you a hint, you need to
delete one character to get it to work.
def hello_function():
print("Hello World")
print("How are you?")

#hello_function()
Printscreen to show that you got it working.
Name: Lloyd 81s
Example Program: Guessing Game

import random

randomnumber = random.randrange(1, 10)


print("This is the random number", randomnumber)
# print hidden

guess = int(input("Please enter your guess: "))

if guess == randomnumber:
print ("Hit!")
elif guess < randomnumber:
print ("Your guess is too low")
else:
print ("Your guess is too high")

Tasks:
1. Change the game so it doesn’t show the random number during the game.
Print screen it below.

2. Change the game so that it generates two random numbers and compares them
to each other. Print screen it below.

3. Change the program so it creates 2 random numbers, then add them


together. Ask the user what the answer would be and compare the answer.
Print screen it below.

4. Change the program so it allows you to guess more than once until you get
it right. You will need a while loop to keep the game playing and you
will need a variable such as tries. Print screen it below.

Example Program: Create a Program that uses a FOR loop and paste it below. Explain
what each part of the code does.

Example Program: Create a Program that uses a WHILE loop and paste it below. Explain
what each part of the code does.

Example Program: Create a program that uses FUNCTIONS and print screen it below

You might also like