Actually learn Python by typing out the code yourselves: class 1
Learn your first programming language is hard. For that reason, a lot of people choose python to start from zero. I remember when I learnt my first programming language (Basic), I typed out the code character by character and found this worked for me. I’m writing this tutorial to teach my kids and I hope you’ll find this useful too.
This is for people with very little knowledge of programming. To make this method work, you have to type the code out by yourself. Do NOT copy and paste. Do NOT copy and paste. Do NOT copy and paste.
Where to type? ↑
Option 1: For this class, you can type in an online python playground, such as here.
Option 2: Most computers come with python preinstalled, open a terminal / command line, type
python3 -V
, if you saw something similar to this:
Python 3.11.4
you have python already installed on your computer. Open your favorite text editor and start typing. Save the file somewhere and run it in your terminal with:
python3 my_saved_file.py
Option 3: type
python3
in a terminal, and type your code there.
Play with odds ↑
Print all odd numbers less than 10.
Attention
: Python use white spaces to define the structure of the code. You have to indent the code consistently, we use four spaces in all the codes.
for i in range(5):
print(i, 2 * i + 1)
output
0 1
1 3
2 5
3 7
4 9
s = 0
for i in range(5):
s += 2 * i + 1
print(i, s)
print(s)
output
0 1
1 4
2 9
3 16
4 25
25
Multiplication Table ↑
Let’s print out all possible multiplications under 5:
for x in range(5):
for y in range(5):
print(f"{x} x {y} = {x * y}")
output
0 x 0 = 0
0 x 1 = 0
0 x 2 = 0
0 x 3 = 0
0 x 4 = 0
1 x 0 = 0
1 x 1 = 1
1 x 2 = 2
1 x 3 = 3
1 x 4 = 4
2 x 0 = 0
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
3 x 0 = 0
3 x 1 = 3
3 x 2 = 6
3 x 3 = 9
3 x 4 = 12
4 x 0 = 0
4 x 1 = 4
4 x 2 = 8
4 x 3 = 12
4 x 4 = 16
for x in range(5):
for y in range(5):
print(x * y, end="\t")
print()
output
0 0 0 0 0
0 1 2 3 4
0 2 4 6 8
0 3 6 9 12
0 4 8 12 16
for x in range(5):
for y in range(5):
if x == 0:
print(y, end="\t")
elif y == 0:
print(x, end="\t")
else:
print(x * y, end="\t")
print()
output
0 1 2 3 4
1 1 2 3 4
2 2 4 6 8
3 3 6 9 12
4 4 8 12 16
for x in range(5):
for y in range(5):
if x == 0:
if y == 0:
print(end="\t")
else:
print(y, end="\t")
elif y == 0:
print(x, end="\t")
else:
print(x * y, end="\t")
print()
output
1 2 3 4
1 1 2 3 4
2 2 4 6 8
3 3 6 9 12
4 4 8 12 16
Challenge ↑
- Write a program to calculate $1 + 2 + … + 100$.
- Write a program to print multiplication table of single digits.
Leave a comment if you have any questions (you don’t need to register an account).