๐ Python from scratch ยท Session 1
Hello, Python: print, text and comments: practice sheet
Part 1: What is a program?
Remember: A program is a list of instructions for a computer. Python reads it from the top line to the bottom line, one line at a time.
1. Warm-up Print exactly: Hello, world!
2. Warm-up Print three lines: your name, your class and your school.
3. Core Print this poem exactly, on two lines:
Roses are red
Python is fun4. Stretch ๐ฅ Print a triangle of stars exactly like this, one print per line:
*
**
***
****
Part 2: Text and numbers
Remember: Text inside quotes is a string. Numbers need no quotes, and Python can do maths with them: + adds, - subtracts, * multiplies, / divides.
5. Warm-up Make Python work out 25 + 17 and print the answer.
6. Core Print the sum as text on line 1 and its answer on line 2:
7 + 5
127. Core Print exactly: Hours in a week: 168, with Python doing the multiplying.
8. Stretch ๐ฅ A cinema has 18 rows of 24 seats. Print two lines, worked out by Python:
Seats: 432
Two shows: 8649. Stretch ๐ฅ Asha scored 85, 90 and 77 in three tests. Print her average, worked out by Python (it should show 84.0).
Part 3: Comments, joining and repeating text
Remember: A line starting with # is a comment. Python skips it; it is a note for people.
10. Warm-up Print a line of exactly 15 # signs using * (do not type 15 of them).
11. Core Print this box exactly, using * for the dashes:
+----------+
| PYTHON |
+----------+12. Core Print a staircase of five lines: #, ##, ###, ####, ##### (use * on every line).
13. Stretch ๐ฅ Write a program with at least two comment lines that prints a flag:
*****=====
*****=====
*****=====
Use * to build each line from "*" and "=".
Part 4: Reading an error and fixing it
Remember: When Python cannot understand a line it stops and shows an error with a line number.
14. Warm-up Fix the program so it prints: Good morning
print("Good morning)15. Core Fix both lines so the program prints Hello and then World.
Print("Hello") print("World"16. Stretch ๐ฅ There are three mistakes. Fix them so the output is exactly:
Score: 10
Total: 15
Byeprint("Score:" 10) print("Total:", 5 + ) print(Bye)
Activity 1: Name badge
17. Build a name badge one step at a time. Press Check after each step to see which parts already work.
- Print a top border of exactly 30 = signs, using *.
- Print NAME: followed by your name on one line.
- Print your age, then on the next line your age in months worked out by Python (age ร 12).
- Print one hobby you love.
- Print the bottom border, the same as the top.
# Step 1: top border of 30 = signs # Step 2: NAME: and your name # Step 3: your age, then your age in months (let Python multiply) # Step 4: a hobby # Step 5: bottom border
Activity 2: Shop receipt
18. Print a receipt that looks exactly like the one below. The total must be worked out by Python.
------------------------
SUPER STATIONERY
------------------------
Pencil 12
Notebook 45
Eraser 8
Total 65
Thank you!- Print a line of 24 dashes using *.
- Print the shop name, then another line of 24 dashes.
- Print each item and its price on its own line (use a comma between them).
- Print Total and the sum 12 + 45 + 8, worked out by Python.
- Print Thank you! at the end.
# Step 1 and 2: the header # Step 3: the three items # Step 4: the total (let Python add) # Step 5: thank you
Activity 3: Times-table strip
19. Print the first six lines of the 7 times table, with Python doing every multiplication:
7 x 1 = 7
7 x 2 = 14
โฆ up to 7 x 6 = 42. Then print a line of 20 stars and the line: Total of the answers: 147 (worked out by Python).- Print 7 x 1 = and the answer 7 * 1, using a comma.
- Do the same for 2 to 6.
- Print a line of 20 stars.
- Print Total of the answers: and let Python add 7 + 14 + โฆ + 42 (or 7 * 21).
# Step 1 print("7 x 1 =", 7 * 1) # Step 2: lines for 2 to 6 # Step 3: 20 stars # Step 4: the total
Predict the output
Work it out on paper first, then type it into the lesson page to check.
20. What does this print?
print("5 + 3")21. What does this print?
print(5 + 3)
22. What does this print?
print("Hi", "there")23. What does this print?
print("ab" + "c" * 3)24. What does this print?
print(2 * 3 + 1)
25. What does this print?
print("-" * 3 + ">")
Homework
26. Warm-up Print a three-line address label: your name, your city and your country.
27. Core Print the number of seconds in one day, worked out by Python, with the label Seconds in a day:
28. Stretch ๐ฅ Print a hollow square 5 wide, using * and +:
*****
* *
* *
*****
Answer key
print("Hello, world!")print("Asha") print("Class 5") print("Green Valley School")print("Roses are red") print("Python is fun")print("*") print("**") print("***") print("****")print(25 + 17)
print("7 + 5") print(7 + 5)print("Hours in a week:", 24 * 7)print("Seats:", 18 * 24) print("Two shows:", 18 * 24 * 2)print((85 + 90 + 77) / 3)
print("#" * 15)print("+" + "-" * 10 + "+") print("| PYTHON |") print("+" + "-" * 10 + "+")print("#" * 1) print("#" * 2) print("#" * 3) print("#" * 4) print("#" * 5)# my flag # three equal stripes print("*" * 5 + "=" * 5) print("*" * 5 + "=" * 5) print("*" * 5 + "=" * 5)print("Good morning")print("Hello") print("World")print("Score:", 10) print("Total:", 5 + 10) print("Bye")# Step 1 print("=" * 30) # Step 2 print("NAME: Kabir") # Step 3 print("Age:", 11) print("Age in months:", 11 * 12) # Step 4 print("I love: football") # Step 5 print("=" * 30)print("-" * 24) print("SUPER STATIONERY") print("-" * 24) print("Pencil", 12) print("Notebook", 45) print("Eraser", 8) print("Total", 12 + 45 + 8) print("Thank you!")print("7 x 1 =", 7 * 1) print("7 x 2 =", 7 * 2) print("7 x 3 =", 7 * 3) print("7 x 4 =", 7 * 4) print("7 x 5 =", 7 * 5) print("7 x 6 =", 7 * 6) print("*" * 20) print("Total of the answers:", 7 + 14 + 21 + 28 + 35 + 42)- 5 + 3
- 8
- Hi there
- abccc
- 7
- --->
print("Kabir") print("Pune") print("India")print("Seconds in a day:", 24 * 60 * 60)print("*" * 5) print("*" + " " * 3 + "*") print("*" + " " * 3 + "*") print("*" * 5)