Programming with Python: first steps · Lesson 1 of 3

Your first program: print and variables

Today you will

  • Show text on the screen with print().
  • Store a value in a variable and change it.
  • Choose names Python accepts for variables.

Before you start: Nothing. You only need a phone or computer with a browser.

What a program is

A program is a list of instructions the computer follows from top to bottom. Python is a popular language for writing them because it reads almost like English.

Showing text

print() shows whatever you put in the brackets:

print("Hello, world")

Text in quotes is called a string. You can join strings with +:

print("Good " + "morning")   # shows: Good morning

Anything after # is a comment: a note for people, which Python ignores.

Variables

A variable is a name that holds a value. The = sign stores the value on the right under the name on the left:

name = "Amina"
age = 21
print(name)   # shows: Amina

You can change a variable by storing something new in it, even using its old value:

age = age + 1
print(age)    # shows: 22

Good names

A variable name may use letters, digits and _, but it can't start with a digit, can't contain spaces, and can't be one of Python's own words such as if or class. total_price is fine; 2price and total price are not.

Asking the user

input() waits for the user to type something and gives it back as a string:

city = input("Where do you live? ")
print("Hello from " + city)

Guided practice

What does this show?

score = 10
score = score + 5
print(score)

The first line stores 10. The second takes the old value, adds 5 and stores 15. So it shows 15.

Practice

Marked as you go. Get 75% right to complete the lesson.

Getting your desk ready…

Build status · Flash Infinity AI™ is a trademark of AI-Flash Inc.. WorldClass is not affiliated with, approved or accredited by any exam board, curriculum provider or university unless a page says so.