Programming · Variables & Constants
up to +100 XP
Stop 2.2 · Programming · 3 min

Variables & Constants

Learn to store and manage data in your programs

Learning objectives:
  • Know what variables and constants are and when to use them (2.2.1–2.2.2)
  • Use data type conversion functions (2.2.3, 2.2.9)
  • Understand scope, global and local variables (2.2.4–2.2.6)
  • Declare and use variables and constants (2.2.7–2.2.8)

Variables

A variable is a named storage location whose value can change while the program runs. Use one whenever a value is input, calculated or updated.

score = 0
score = score + 10

Constants

A constant is a named value that should not change while the program runs. Python has no special keyword, so by convention constants are written in UPPER_SNAKE_CASE at the top of the program.

VAT_RATE = 0.2
MAX_ATTEMPTS = 3

Use constants for fixed values such as rates and limits. If the value changes in future, you edit it in one place, and the name explains what the number means.

Data type conversion

Conversion functions change a value from one type to another, so that operations between types work. The ones in the specification are int(), float(), str() and bool().

age = int(input("Age: "))        # "17" becomes 17
price = float("4.99")             # 4.99
message = "You are " + str(age)   # join a number to text
print(bool(0), bool(5))           # False True

Converting text that is not a number, such as int("ten"), causes a ValueError, so input should be validated first.

Scope

Scope is the part of a program where a variable can be used.

TOTAL_STOPS = 20            # global (a constant)

def percent_done(done):
    percent = done / TOTAL_STOPS * 100   # local
    return percent

print(percent_done(5))      # 25.0
# print(percent) would fail: percent is local

Prefer local variables. They avoid one part of a program accidentally changing a value used elsewhere, and make functions easier to test and debug. Use globals only when a value is genuinely needed everywhere.

Quick check · 11 questions · up to 100 XP

Check what stuck.

+10 XP each
Question 1

Question 1 of 11

What is a variable?

Question 2

Question 2 of 11

How are constants usually written in Python?

Question 3

Question 3 of 11

Why use a constant for a VAT rate?

Question 4

Question 4 of 11

What does int("25") return?

Question 5

Question 5 of 11

What happens with int("ten")?

Question 6

Question 6 of 11

What is scope?

Question 7

Question 7 of 11

Where can a local variable be used?

Question 8

Question 8 of 11

Why are local variables usually preferred to global variables?

Question 9

Question 9 of 11

Which converts the number 7 into text so it can be joined to a string?

Question 10

Question 10 of 11 · Fill in the blanks

A variable created inside a function is a variable. One created outside all functions is a variable.

Question 11

Question 11 of 11 · Coding

Use a constant: VAT_RATE = 0.2. Write add_vat(price) that returns the price with VAT added.

Open the IDE
Hint

price + price * VAT_RATE