BrightPath
Back to Course
Year 9 Coding

Python Variables and Types

Learn how to store data using variables and understand the main data types in Python: integers, floats, strings, and booleans.

What Are Variables?

A variable is like a labelled box that stores a piece of data. You give it a name and assign it a value using the = sign. You can then use that name to refer to the data later.

# Creating variables
name = "Alex"
age = 14
height = 1.65
is_student = True

# Using variables
print("Name:", name)
print("Age:", age)
print("Height:", height, "metres")
print("Student:", is_student)

Variable names should be descriptive and use lowercase letters. If a name has multiple words, use underscores: favourite_colour. Variable names cannot start with a number or contain spaces.

Python Data Types

Every value in Python has a type. The four most common types are:

int (integer)

Whole numbers: 42, -7, 0

float

Decimal numbers: 3.14, -0.5, 100.0

str (string)

Text in quotes: "Hello", 'Python'

bool (boolean)

True or False: True, False

# Check the type of a variable using type()
score = 95
print(type(score))       # <class 'int'>

temperature = 36.5
print(type(temperature)) # <class 'float'>

city = "Sydney"
print(type(city))        # <class 'str'>

passed = True
print(type(passed))      # <class 'bool'>

User Input and Type Conversion

The input() function lets your program ask the user for data. It always returns a string, even if the user types a number. You need to convert it to use it as a number.

# Getting user input
name = input("What is your name? ")
print("Hello,", name)

# Converting string input to a number
age_text = input("How old are you? ")
age = int(age_text)          # Convert string to integer
print("Next year you will be", age + 1)

# Shortcut: convert on the same line
height = float(input("Enter your height in metres: "))
print("Your height is", height)

Use int() to convert to a whole number, float() for decimals, and str() to convert a number to text.

Key Vocabulary

Variable

A named container that stores a value. Created using the = assignment operator.

Data Type

The category of data a value belongs to, such as int, float, str, or bool.

input()

A function that pauses the program and waits for the user to type something. Always returns a string.

Type Conversion

Changing a value from one type to another, e.g. int("5") converts the string "5" to the integer 5.

Worked Examples

1

Create variables for a student's name, year level, and average mark, then print them.

# Store student information in variables
student_name = "Mia"
year_level = 9
average_mark = 87.5

# Display the information
print("Student:", student_name)
print("Year:", year_level)
print("Average:", average_mark)

Step 1: Create a string variable for the name, an integer for year, and a float for the mark.

Step 2: Use print() to display each variable with a label.

Output: Student: Mia, Year: 9, Average: 87.5

2

Ask the user for two numbers and print their sum.

# Get two numbers from the user
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))

# Calculate and display the sum
total = num1 + num2
print("The sum is:", total)

Step 1: Use input() to get user data and int() to convert it to a number.

Step 2: Add the two numbers and store the result in total.

Key point: Without int(), Python would join the strings instead of adding them (e.g. "3" + "5" = "35").

3

Swap the values of two variables.

# Start with two variables
a = 10
b = 20
print("Before swap: a =", a, "b =", b)

# Python makes swapping easy!
a, b = b, a
print("After swap: a =", a, "b =", b)

Step 1: Assign values to a and b.

Step 2: Use Python's tuple unpacking a, b = b, a to swap them in one line.

Output: Before swap: a = 10 b = 20 then After swap: a = 20 b = 10

Knowledge Check

Select the correct answer for each question. Click "Check Answer" to see if you are right.

Question 1

What data type is the value 3.14?

Question 2

What does input() always return?

Question 3

Which of these is a valid Python variable name?

Question 4

What will print(type(True)) output?

Question 5

What does int("7") + 3 evaluate to?

Key Concepts Summary

Prev: Introduction to Python Next: Making Decisions