Programming technique workbook learn to write a python script
![Programming technique workbook learn to write a python script](/images/remos_downloads/id-12729.12729.pdf_thumb_prev.jpg)
Programming technique workbook.
Python 1 sectionName:
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
Thursday, August 29, 2019
Python Introduction
Lesson objectives…
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.
syntax, logic, repeat,
Python Introduction
Variables, Constant, Python, Execute, inputs, print programming language, Python, IDLE, syntax
Python Introduction
film =“Jumanji”Lion King”
print(film)
Python, IDLE, window, shell, editor, variables, value, print.
Python Introduction
What will this program display?film =“Lion King”print(“My favourite film is”, film)
Python, IDLE, window, shell, editor, variables, value, print.
Demonstrationhobby =input(“Enter a Hobby”)
print(hobby)
name =input(“what is your name”)
print(“Oh you are”, name)
Python Introduction
Syntax, Python, print, input, variables, constant
![](/images/remos_downloads/detail/127/id-12729.12729.pdf-035.jpg)
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)
Syntax, Python, print, input, variables, constant
Python Introduction
Adding comments:
• Comments are useful to help understand your code.
• They will not affect the way a program runs.
• You use # to comment on code.
• Add comment to your program from previous slide.
Adding comments:
#number is a variable.number = 7
#Displays the value of the variable.
print(number)
KEYWORDS: Syntax, Python, print, input, variables, constant
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
food = "_____" print(_____)
3) Ask the user for their favourite film. Display “I also like watching”, film. E.G:
Paste your code below:
Q3
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
firstname = input("Enter your firstname")
surname= _____(_______) age = _______("_____") subject = _____(_______) print(______) print(_____) _____(age)
_____(subject)
![](/images/remos_downloads/detail/127/id-12729.12729.pdf-072.jpg)
5. Create a program that ask the user for their name. Display the name 5 times on 1 line. Paste your code below:
Q5
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
food = _____("_____") print("I also like", _____)
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", _____)
Revisit phase
Thursday, August 29, 2019
Data types
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.
Data Types
Consolidation
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
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
Revisit phase:
![](/images/remos_downloads/detail/127/id-12729.12729.pdf-109.jpg)
Complete MS Form assessment on variables, constant, casting & data types. Link:
Thursday, August 29, 2019
Operators
Consolidation
Application phase
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 number1number2 = 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(________)
![](/images/remos_downloads/detail/127/id-12729.12729.pdf-130.jpg)
Test phase
Data types, Boolean, Real, Character, Integer.
Homework
Data types, Boolean, Real, Character, Integer.
Revisit phase
Arithmetic operators
Thursday, August 29, 2019
If statement
Lesson objectives…
IAG LINK:
Knowledge phase
Demonstration:
number1 = 5 number2 = 7
answer = number1 > number2 print(answer)
KEYWORDS: Equal to, not equal to, greater than, less than
Knowledge phase
Relational operators, greater than, less than, not equal to
RELATIONAL OPERATORS
Consolidation
Relational/comparison operators
Programming constructs
Programming constructs
If statement
KEYWORDS: Selection, if, then, else
If statement
Application phase
If statement
17- Relational operators
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
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
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
name = ___________ if _____ == "Tom":
print_______ ____:
_____("Hello stranger")
Q22
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
number = int(input("Enter a number")) if number > 70: print("above 70")
elif number > 40:
print("Above 40 but less than 71")
![](/images/remos_downloads/detail/127/id-12729.12729.pdf-230.jpg)
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
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
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
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:
![](/images/remos_downloads/detail/127/id-12729.12729.pdf-257.jpg)
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:
Thursday, August 29, 2019If statement
Knowledge phase
Demonstration
age = int(input(“Enter your age”)) if age>=18: print(“You are old enough to vote”)
elif age==17: print(“Try again in 1 year”)
elif age
print(“You are not old enough to vote”)
else:
print(“Error. Input not understood”)
Selection, if, else, then, variables, choice, decision, condition.
Knowledge phase
Booleanoperators can be used to make selection statements more efficient and versatile. We useAND,OR&NOT.
It’s important to use brackets in long Boolean expressions.
Order:Brackets,NOT,ANDthenOR.
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 = 0ifn == 2orn == 3:print("Correct")
else:print("Incorrect")
Boolean operators, OR, AND , NOT
Consolidation
Teacher Demonstration
name =input(“What is your name?”)ifname =="James"orname =="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?”)ifname =="James"orname =="Don":
print("I know you!")
else:print("Who are you?")
KEYWORDS: Selection, if, else, then, variables, choice, decision, condition.
Application phase
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.
![](/images/remos_downloads/detail/127/id-12729.12729.pdf-270.jpg)
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:
Homework:
End of unit test