0% found this document useful (0 votes)
2K views

Programming Technique - Python 1 - Workbook

This document provides an introduction to programming techniques using Python. It discusses key concepts like variables, constants, data types, selection, iteration, and sequencing. It also provides exercises for students to practice skills like taking user input, using variables, and identifying different error types. The document is divided into sections on Python introduction, variables and input/output, and data types. It includes objectives, definitions, examples, and tasks for students to complete to help them learn programming logic and techniques.

Uploaded by

api-272637257
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2K views

Programming Technique - Python 1 - Workbook

This document provides an introduction to programming techniques using Python. It discusses key concepts like variables, constants, data types, selection, iteration, and sequencing. It also provides exercises for students to practice skills like taking user input, using variables, and identifying different error types. The document is divided into sections on Python introduction, variables and input/output, and data types. It includes objectives, definitions, examples, and tasks for students to complete to help them learn programming logic and techniques.

Uploaded by

api-272637257
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 128

Programming technique workbook.

Python 1 section
Name:

Class:
• Title: Programming technique – Python 1

• Big Question: Solve complex problem using computational thinking


techniques and programming constructs.

• Small Questions: What do we mean by selection, iteration,


sequencing, variable, constants. Use selection to solve a problem.
Comment on code. Identify errors. Use data types appropriately.

• Key Words: logic errors, syntax errors, run time errors, constants,
variables, data types, iteration, selection, sequencing.
Python Introduction
Starter
activity
• Create a folder in your area
named: Python.

• Create another folder inside


the Python folder and name it:
Lesson 1.

• Open IDLE Python. Click on


FILE  New FILE.
VARIABLES, INPUT
AND OUTPUT
Thursday, August 29, 2019

Python Introduction
Lesson objectives…

Understand the purpose of programming.


Use the print and input functions successfully.
Explain the purpose of a variable.

IAG LINK:Software Engineer (designs programs) £31,496 average salary


Knowledge phase

1. Type print(“Hello world”)


2. Press F5
3. Save your program in the python folder as
print.
4. Task 2: Change the code to display your
name.
5. Task 3: Change your code to display your
favourite film and age.

KEYWORDS: programming language, Python, IDLE, syntax


Python Introduction

KEYWORDS:
Syntax error – where you’ve broken the rules of the language.

print(“Hello year 7)

primt(“How are you?”)

print(“Good bye”

KEYWORDS: syntax, logic, repeat,


Python Introduction

GLOSSARY KEYWORDS:
Variable: A location in a memory Constant: A location in a
where data is stored. It can be changed memory where data is stored. It
while the program is running. cannot be changed while the program
is running.

KEYWORDS: Variables, Constant, Python, Execute, inputs, print


Python Introduction

Definitions:
Syntax: the order of instructions
or commands.

Assignment: Giving a variable or constant a value.

KEYWORDS: programming language, Python, IDLE, syntax


Python Introduction

Value
film = “Lion King”
“Jumanji”
print (film)
Displays the
Assign film as value of the
a variable variable film.
which stores
the value.

KEYWORDS: Python, IDLE, window, shell, editor, variables, value, print.


Python Introduction

What will this program display?

film = “Lion King”


print (“My favourite film is” ,
film)

KEYWORDS: Python, IDLE, window, shell, editor, variables, value, print.


Python Introduction

Demonstration

hobby = input(“Enter a Hobby”)


print (hobby)

name = input(“what is your name”)


print (“Oh you are”, name)

KEYWORDS: Syntax, Python, print, input, variables, constant


Consolidation phase

Copy the code below, then change the


variable name to car, change the question to
"favourite car?", then display: car, "is a
nice car"

name = input(“what is your name”)


print (“Oh you are”, name)

KEYWORDS: Syntax, Python, print, input, variables, constant


Python Introduction

Adding comments: Adding comments:


• Comments are useful to
help understand your code. #number is a variable.
• They will not affect the way number = 7
a program runs. #Displays the value of the
variable.
• You use # to comment on
code. print(number)
• Add comment to your
program from previous
slide.

KEYWORDS: Syntax, Python, print, input, variables, constant


Application phase

Complete task 1-6

KEYWORDS: Variables, Constant, Python, Execute, inputs, print


Q1 helpsheet
print("Hello World")
print(“I like cheese")
1. Display the following on 3 separate lines:
This is my first program.
It shows messages
Sometimes on different lines
Paste your code below:
2. Create a variable called food and store your favourite food
inside the variable. Print out the value of the variable onto the
screen.
Paste your code below:
Q2 helpsheet

food = "_____"
print(_____)
3) Ask the user for their favourite film. Display “I also
like watching”, film. E.G:
Paste your code below:
Q3 helpsheet

film = _____("Enter your favourite film")


print("_____", _____)
4. Create a program that ask the user for their firstname, surname, favourite
subject and age then display the above on 4 separate lines. “Remember you
can’t have space in a variable name.
Paste your code below:
Q4 helpsheet

firstname = input("Enter your firstname")


surname= _____(_______)
age = _______("_____")
subject = _____(_______)
print(______)
print(_____)
_____(age)
_____(subject)
5. Create a program that ask the user for their name. Display the
name 5 times on 1 line.
Paste your code below:
Q5 helpsheet

name = ______("Enter your name")


print(_____*5)
6. Create a program that ask the user for their favourite food.
Display “I also like”, food. Food should be replaced with the
user’s answer.
Paste your code below:
Q6 helpsheet

food = _____("_____")
print("I also like", _____)
Test phase

Complete the task on the next slide.

KEYWORDS: Variables, Constant, Python, Execute, inputs, print


7. Ask for a user’s name and age. Display “your name is”, name,
“and you are”, age, “year old”.E.G:
Paste your code below:
Q7 helpsheet

name = _____("Enter your name")


age = _____("______")
print("Your name is", _____, "and your age
is", _____)
Homework

https://www.youtube.com/watch?v=jhVu
yveJMgA&list=PLCiOXwirraUBO3Z2dxnIfu
NDspmJmorJB

KEYWORDS: Variables, Constant, Python, Execute, inputs, print


Revisit phase
Starter
activity
Open IDLE Python.
Click on FILE  New
FILE.

Create a program that


asks the user if they like
school or not, then
display the answer.
Thursday, August 29, 2019

Data types
Lesson objectives…

What are the advantages Compare different data types.


and disadvantages of
storing data on a Create a program using variables
computers? and input command.
Consolidate understanding of
inputs, data types and variables.

IAG LINK:Software engineer (designs programs) £20,698 - £50,908


Knowledge phase

Data type: A description about the type of data a variable


holds.
Why do we use data types?
The system will need to know the data type of the
variable so that it can allocate the correct size of memory
for the variable's data.
The computer will be unable to carry out the correct
calculations on the variables unless it knows what type of
data they contain.

KEYWORDS: Data types, Boolean, Real, Character, Integer.


Data Types
• String holds alphanumeric data as text.
• Integer holds whole numbers.
• Float/Real holds numbers with a decimal point.
• Character holds a single, alphanumeric character.
• Boolean holds either ‘True’ or ‘False’.

KEYWORDS: Data types, Boolean, Real, Character, Integer.


Consolidation
• Go on
• www.kahoot.it

Game pin will be on the board.

KEYWORDS: Data types, Boolean, Real, Character, Integer.


Casting
❏ Python assumes any input is a string.
❏ If you try and perform a calculation with strings it won’t work.
❏ You need to change the data type to either an integer or a float.
❏ Casting: Changing a variable from one data type to
another.

Example:

age=int(input(“How old are you?”))

age = str(age)

Above line will CAST age from integer to string.


Python task

1. Create a variable called number and allow the user to assign


a value to it.
2. Now add this line:
print (number + number)

3. Run the program. Did it work?


4. Try converting/casting the data type:
number = int(number)

KEYWORDS: casting, data type, variable, program, algorithm


Data Types

Demonstration:

number = int(input("Enter a number"))


print (number)

name = str(input("what is your name"))


print (name)

KEYWORDS: Syntax, Python, print, input, variables, constant


Application phase
Complete task 8-11

KEYWORDS: Data types, Boolean, Real, Character, Integer.


8. Complete the following table:

Data Data Type

Age Integer

House address

Name

Test tomorrow? Y/N

Weight

Price

Is 5 == 5
9. Ask the user for 2 numbers then divide the first number by the
second number. Display the answer. To divide numbers use /
Paste your code below:
Q9 helpsheet

number1 = int(input("Enter a number"))


number2 = _____________
answer = ____/_____
print(_____)
10. Asks for the width of a rectangle. Asks for the length of a
rectangle. Calculates the area of a rectangle. Print the area of a
rectangle. To multiply numbers use *
Paste your code below:
Q10 helpsheet

width = int(input("________"))
______ = int(input("Enter a height"))
area = _____*_____
print("The area of the rectangle is", _____)
11. Asks 2 users for their weight, calculate the average weight of
the 2 users.
Paste your code below:
Q11 helpsheet

weight1 = float(_____("User 1, enter your weight"))


weight2 = _____(input_________
average = (_____+_____) / 2
print(_______)
Test phase
Write a program which calculates how much money a
student will need to buy a meal and two drinks. The user
should be prompted to enter how much a meal costs, how
much a drink costs, and then calculate and display the
total required. Add comments to your code.
Paste your code below:

KEYWORDS: Data types, Boolean, Real, Character, Integer.


Revisit phase:
Complete MS Form assessment
on variables, constant, casting
& data types.
Link:
https://forms.office.com/Pages/ResponsePage.aspx?i
d=NRrmRxWbmk-
P3e1XFrwgvUKZUnHi_FpMtvQIsO11-
sxUNEc4TjVTR09ORExaMEFWSDk5SkswNFUzUC4u
Thursday, August 29, 2019

Operators
Lesson objectives…
Understand arithmetic operators.
Demonstrate using operators to solve problems.
Use operators to solve a real-world situation.

IAG LINK:Programmer analyst Salary: £25,000- £55,000


Knowledge phase

Demonstration:

number1 = 10
number2 = int(input(“Enter a
number”))
answer= number1 * number2
print(answer)

KEYWORDS: Syntax, Python, print, input, variables, constant


modulus

(Floor
KEYWORDS: Addition, subtraction, multiplication, division, modulus, exponent.
division)
Consolidation
• Go on
• www.kahoot.it

Game pin will be on the board.

KEYWORDS: Arithmetic operators


Application phase
• Complete task 12-16

KEYWORDS: Arithmetic operators


12. Complete the following table:

Arithmetic operator name Arithmetic operator symbol IN PYTHON

Addition +
Subtraction -
Multiplication
Division
Exponent “^”
Modulus “MOD”
Floor Division “DIV”
Q13 helpsheet
number1 = 8 # store the value 8 in a variable called number1
number2 = 6 #store the value 6 in a variable called number2.
answer= number1 + number2 #add 2 numbers together and store
them in a variable called answer.
print(answer) #displays the answer.
13. Create a variable x with a value of 5. Create a variable y
with a value of 3. Create a variable z with a value 10. Multiply
three numbers together and store them in a variable called
answer. Comment on the code.
Paste your code below:
Q14 helpsheet
number1 = 8
number2 = 6
answer= number1 + number2
print(answer)
14. Alex has £20. Spending:£5 on pens. £3 on pencils. Total
amount left?
Demonstrate this example using 4 variables and arithmetic
operators. Comment on your code.
Paste your code below:
15. Ask how many apples the user wants. Ask how many
people the user will share the apples with. Find out how many
apples will remain if you share the apples equally. Hint: use
modulus %.
Paste your code below:
Q15 helpsheet

apples = int(_____("How many apples do you want"))


people= ________
remain = _____ % _____
print(______)
16. Asks for the home team name. Asks for the opponent team
name. Asks for the number of goals scored by the home team.
Asks for the number of goals scored by the opposition team.
Calculates the goal difference for the home team.
Paste your code below:
Q16 helpsheet

homeTeam= ____(input("How many goals did the home team score??"))


opponent = _____________
difference= _______-_________
print(________)
Test phase
• Ask the user how many sweets do they want, ask the
shop owner how much each sweet cost “hint: Use float
instead of int”. Calculate the total cost, display the
total cost + 20% interest rate on top of the total.
Comment on the code #.
Paste your code below:

KEYWORDS: Data types, Boolean, Real, Character, Integer.


Homework
https://www.youtube.com/watch?v=4u2ClNCtcgY

KEYWORDS: Data types, Boolean, Real, Character, Integer.


Revisit phase
• 1. State the values of a, b, c, d and e after the following
operations are carried out:
(a) a = 26 mod 5 =
b) b = 142 div 7 =
(c) c = (7 + 3) * 4 – 1 =
(d) d = 15.6 / 3 + 4.8 / 2 =
(e) e = “4” + “56” =

KEYWORDS: Arithmetic operators


Thursday, August 29, 2019

If statement
Lesson objectives…
Identify the meaning of basic relational/comparison
operators.
Identify the purpose of selection
Prepare a python program using If statements.

IAG LINK:Software Quality Assurance Engineer Salary: £25,000- £50,000


Knowledge phase

Demonstration:
number1 = 5
number2 = 7
answer = number1 > number2
print(answer)

KEYWORDS: Equal to, not equal to, greater than, less than
Knowledge phase
Group demonstration:
Chelsea has scored 70 goals.
Arsenal has scored 64 goals.
In total, Chelsea has scored more goals than Arsenal.
Create the above example in python to display True.

KEYWORDS: Relational operators, greater than, less than, not equal to


RELATIONAL OPERATORS

KEYWORDS: Equal to, not equal to, greater than, less than
Consolidation
• Go on
• www.kahoot.it

Game pin will be on the board.

KEYWORDS: Relational/comparison operators


Programming constructs
There are 3 programming constructs:

•Sequencing
•Selection
•Iteration
KEYWORDS: Selection, if, else, then, variables, choice, decision, condition.
Sequencing:
A set of instructions given in a
particular order.
Example:
Name= input(“Enter name”)
print(Name)

KEYWORDS: Programming constructs


If statement

Selection: Depending on the condition


the algorithm follows a choice between
different alternatives.

KEYWORDS: Selection, if, else, then, variables, choice, decision, condition.


If statement

If Then

Else

KEYWORDS: Selection, if, then, else


If statement

If it’s cold outside then Demonstration:


Wear a jacket. How can we code this
Else example in python? Watch
Don’t wear a jacket. the demonstration.

KEYWORDS: Selection, if, else, then, variables, choice, decision, condition.


If statement
Indentation!

password = input("enter pass")


if password == "abc1":
• Python requires
print("Access Granted") indentation as part of
elif password == "abc":
print("Very close")
the syntax.
else: • Indentation signifies the
print("Access Denied")
start and end of a block
of code.

KEYWORDS: Selection, if, else, then, variables, choice, decision, condition.


Application phase
• Complete task 17-31

KEYWORDS: If statement
17- Relational operators
Question True/False
5 == 3
2 != 5
5>6
8<2
2!=2
7==7
1>=1
7>=2
9<=2
9<=9
Q18- Move to next slide
answer=input("What is your answer? ")
if answer == "chocolate":
print("yum")
elif answer == "biscuits":
print("crunchy")
elif answer == "sweets":
print("chewy")
else:
print("I don't know what that means.")
18. Use the code on the previous slide to complete the following
table:

Answer (answer=_______) Result


chocolate
biscuits
other
Sweets
crunchy
error
19. Mancity has scored 60 goals.
• Manutd has scored 51 goals.
• In total, Manutd has scored less goals than Mancity.
• Create the above example in python to display True.
Paste your code below:
Q19 helpsheet

mancity = ____
manutd = ___
_____= _____ < ______
_____(score)
20. Create a program that asks for a person’s age. If the age is
greater than or equal to 18, display “You are old enough to
vote”, else display “You are not old enough to vote”.
Paste your code below:
Q20 helpsheet

age = __________
if ____>=18:
_____("You are old enough to vote")
____:
print(_________)
21. Create a program that asks for a person’s name. If the name
is equal to Tom, display “Welcome Tom”, else display “Hello
stranger”.
Paste your code below:
Q21 helpsheet

name = ___________
if _____ == "Tom":
print_______
____:
_____("Hello stranger")
Q22 helpsheet
print("Hello user")
singer = input("Enter your favourite singer")
if singer == "Beyonce":
print("Good singer")
elif singer =="Ed":
print("Pretty decent")
else:
print("Not too bad")
22. Create a program that Greets the user.
• Asks the user how they are feeling
• If the user enters “happy”, print “glad to hear it”
• If the user enters “sad” will tell the user a joke
• Has an error message for any other entry
Paste your code below:
Q23 helpsheet

number = int(input("Enter a number"))


if number > 70:
print("above 70")
elif number > 40:
print("Above 40 but less than 71")
else:
print("Less than 41")
23. Ask user to enter a grade. If grade is >= 90, display A*, else if grade >=
80, display A, else if grade >= 70, display B, else if grade >= 60, display C, else
display fail.
Paste your code below:
Q24 helpsheet
hobby = input("Enter a hobby")
team = input("Enter your favourite team")
print("Your favourite hobby is", hobby, "and your
favourite team is", team)
24. Ask the user for their favourite music band. Ask the user for
their favourite song. Display the answers in a full sentence.
Paste your code below:
Q25 helpsheet
number1 = int(input("Enter a number"))
number2 = int(input("Enter another number"))
answer = number1+number2
print(answer)
25. Ask the user to input 2 numbers. Multiple these 2 numbers
together. Display the answer.
Paste your code below:
Q26 helpsheet
age = int(input("Enter your age"))
if age >= 18:
print("You are old enough to vote")
else:
print("You are not old enough to vote")
26) Create a program to allow the user to input a number. If the
number is more than 100, print out a “too large” message, else
display “too small”.
Paste your code below:
Q27 Helpsheet
name = input("Enter a name")
if name == "Steve":
print ("Hi Steve, how are you?")
elif name == "John" :
print ("Good to see you John")
else :
print ("I don’t know you")

You can only have one else and it has to be at the end.
27) Ask a user to enter a football team. If the user enters
Chelsea, display blue, else if user enters Liverpool, display red,
else display team not registered.
Paste your code below:
Q28 Helpsheet

number1 = int(input("Enter a number"))


number2 = int(input("Enter another
number"))
if number1 == 5:
print(number1**number2)
elif number1 > 7:
print(number1/number2)
else:
print(number1*number2)
28) Create a program to allow the user to input 2 numbers. If the
first number is bigger than 10, add the two numbers, otherwise
multiply the two numbers. Print out the result.
Paste your code below:
29) Allow the user to enter two numbers, then ask them if they
want the numbers added or multiplied. Depending on their
answer, print the right answer.
Paste your code below:
Q29 Helpsheet

num1 = ____(input("Enter a number"))


num2 = ___________
operator = ________
if ______ == "add":
_____(_____)
____ operator == "_____":
print(____*_____)
____:
_____("Wrong answer")
Q30 Helpsheet
name = input("Enter a name")
if name == "Steve":
print ("Hi Steve, how are you?")
elif name == "John" :
print ("Good to see you John")
else :
print ("I don’t know you")

You can only have one else and it has to be at the end.
30) Ask a user whether they want to take the red pill or the blue pill. If they
write “red” then print “red is the colour of blood”. Elif they write “blue”
then print “Are you sick?”. Else print “I don’t like that colour”
Paste your code below:
31) Ask the user to enter traffic light colour, if colour is = red,
display STOP, else if colour = yellow, display get ready, else if
colour is = green, display GO, else display an error.
Paste your code below:
Test phase:
Write a program to decide whether a year is a Leap year. The rules are:
A year is generally a Leap Year if it is divisible by 4, except that if the year is
divisible by 100, it is not a Leap year, unless it is also divisible by 400. Thus 1900
was not a Leap Year, but 2000 was a Leap year.
Paste your code below:

KEYWORDS: If statement
Revisit phase: identify the error
1. What will be the result of running this code and
entering 52?
Solution:
score = int
Score must be casted into an
integer.
Thursday, August 29, 2019

If statement
Lesson objectives…
Use IF statement to solve real life problems.
Understand the purpose of Boolean operators.
Consolidate understanding of selection.

IAG LINK:Junior System Tester: £24,000 initial salary


Knowledge phase
Demonstration

age = int(input(“Enter your age”))


if age>=18:
Discuss the
print(“You are old enough to vote”) purpose of each
elif age==17:
print(“Try again in 1 year”) line of code:
elif age<17:
print(“You are not old enough to vote”)
else:
print(“Error. Input not understood”)

KEYWORDS: Selection, if, else, then, variables, choice, decision, condition.


Knowledge phase

Boolean operators can be used to make


selection statements more efficient and
versatile. We use AND, OR & NOT.

It’s important to use brackets in long Boolean


expressions.

Order: Brackets, NOT, AND then OR.

KEYWORDS: Boolean operators, OR, AND , NOT


If statement

Boolean expression True/False


11 > 5 AND 7 == 3 False
NOT(13 == 2) True
11 <= 2 OR 8 != 5 True
NOT(12 > 2 AND 5 < 1) True
n=0
if n == 2 or n == 3:
print("Correct")
else:
print("Incorrect")

KEYWORDS: Boolean operators, OR, AND , NOT


Consolidation
• Go on
• www.kahoot.it

Game pin will be on the board.

KEYWORDS: Data types, Boolean, Real, Character, Integer.


Teacher Demonstration

name = input(“What is your name?”)


if name == "James" or name == "Don":
print("I know you!")
else:
print("Who are you?")

KEYWORDS: Selection, if, else, then, variables, choice, decision, condition.


Copy the code below and add
comments to it in python.

name = input(“What is your name?”)


if name == "James" or name == "Don":
print("I know you!")
else:
print("Who are you?")

KEYWORDS: Selection, if, else, then, variables, choice, decision, condition.


Application phase
• Complete task 32-35

KEYWORDS: Data types, Boolean, Real, Character, Integer.


Q32 HELPSHEET.

name = input("Enter a name")


surname = input("Enter a surname")
if name == "James" and surname == "Don":
print("I know you!")
else:
print("Who are you?")
32) Create a program to ask the user to enter a username and
password. If they get the username AND password right, display
a “logged in” message. Otherwise, tell them they are wrong.
Paste your code below:
33) Ask the user if they play games on pc then ask them if they play on
console, if pc = yes and console = yes, display Game master, else if pc = yes
and console = no, display pc master, else if pc = no and console = yes, display
console master, else display an error.
Paste your code below:
Q33 HELPSHEET.

pc = _____("Do you play games on a PC")


console = _________
if pc == "yes" and ______ == "yes":
_____("Game master")
____ pc == "yes" and ______ == "no":
print____
elif ___ == "no" and console == "yes":
print("Console master")
_____:
_____("Wrong option")
Q34 HELPSHEET.
age = int(input("Enter your age"))
if age >= 18 and age <= 20:
print("You are between 18 and 20 years old")
elif age < 18:
print("You are still a child")
else:
print("you are an adult")
34) Ask for the user’s age. If age > 12 AND age < 20 then print “You are a
teenager”. Else if the user is 11 or 12 year old, print “You are a tween.” Else
print “Invalid age”.
Paste your code below:
Q35 HELPSHEET.
name = input("Enter a name")
surname = input("Enter a surname")
if name == "James" and surname == "Don":
print("I know you!")
else:
print("Who are you?")
35) Ask the user for current temperature. Ask the user if it’s raining outside.
If temp is less than 12 degrees and it’s raining, display “Wear a coat and
bring an umbrella”. Else if temperature is less than 12 degrees and it’s not
raining, display “Wear a coat”. Else if temp is greater or equal to 12, and it’s
raining, display bring an umbrella. Else display “you don’t need a coat or an
umbrella”.
Paste your code below:
Test phase:
Write a program to do the following:
If the temperature is greater than 30, output “Too hot”.
If the temperature is between 21 and 30, output “Just right”
If the temperature is less than 21, output “A bit chilly”
Paste your code below:

KEYWORDS: If statement
Homework:
Use the workbook to revise for end of unit
assessment.

KEYWORDS: If statement
End of unit test
MS Form assessment:
Test link:
https://forms.office.com/Pages/ResponsePage.aspx?id=NRrmRxWbmk-
P3e1XFrwgvUKZUnHi_FpMtvQIsO11-
sxUNDAzTTRMQkdMUjFPSkJWOU82MzRGVDc5Sy4u

KEYWORDS: If statement

You might also like