Computer Science
Grade 9
20 min
Variables in Python
Variables in Python
Tutorial Preview
1
Introduction & Learning Objectives
Learning Objectives
Define what a variable is in the context of programming.
Declare and initialize variables in Python using the assignment operator.
Identify and use three fundamental data types: strings, integers, and floats.
Use variables to store, retrieve, and manipulate data within a program.
Update the value of an existing variable (re-assignment).
Follow Python's naming conventions and rules for creating valid variable names.
Ever wonder how a game remembers your high score or how an app greets you by name? 🤔 It's all done using special containers called variables!
In this tutorial, you will learn how to create and use variables, which are the most basic building blocks in programming. Think of them as labeled boxes where you can store information like t...
2
Key Concepts & Vocabulary
TermDefinitionExample
VariableA named storage location in a computer's memory that holds a value. You can access and change the value through the variable's name.`player_score = 100` (Here, `player_score` is the variable name holding the value `100`.)
Assignment Operator (`=`)The symbol used to assign a value to a variable. The value on the right is stored in the variable on the left.`user_name = "Alex"` (The string `"Alex"` is assigned to the variable `user_name`.)
Data TypeThe classification or category of a value, which determines the types of operations that can be performed on it.The number `10` is an integer, while the text `"Hello"` is a string.
String (str)A data type used to represent text. In Python, strings are enclosed in single (`'...
3
Core Syntax & Patterns
Variable Declaration and Initialization
variable_name = value
This is the fundamental syntax for creating a new variable. You choose a name for your variable, use the equals sign (`=`), and provide the value you want to store. This single line both declares the name and initializes it with a value.
Variable Naming Rules
Must start with a letter or underscore (_). Can contain letters, numbers, and underscores. Cannot be a Python keyword (like `if`, `for`, `class`). Case-sensitive.
Python has strict rules for naming variables. To keep code readable, it's a convention to use `snake_case`, where words are all lowercase and separated by underscores (e.g., `high_score`, `user_first_name`).
Re-assigning a Variable
variable_name = new_value
To update or change the value...
4 more steps in this tutorial
Sign up free to access the complete tutorial with worked examples and practice.
Sign Up Free to ContinueSample Practice Questions
Challenging
A program needs to swap the values of two variables, `a` and `b`. Initially, `a = 10` and `b = 20`. Which code block successfully swaps their values so that `a` becomes 20 and `b` becomes 10?
A.a = b
b = a
B.a = b
b = 10
C.temp = a
a = b
b = temp
D.a = 20
b = 10
Challenging
Trace the values of the variables through this code. What is the final output?
val_a = 5
val_b = 10
val_a = val_b
val_b = val_a + 5
print(val_b)
A.10
B.15
C.20
D.5
Challenging
A student is trying to write a program to track inventory. They wrote the following code, but it produces an unexpected result. What is the logical flaw?
ItemCount = 50
item_count = 10
ItemCount = ItemCount - item_count
print(ItemCount)
A.The variable `item_count` was never initialized.
B.The code is trying to subtract two variables with different capitalization, which is not allowed.
C.The student likely intended to use one variable but accidentally created two distinct variables (`ItemCount` and `item_count`) due to case-sensitivity.
D.The print statement should have used `item_count` instead of `ItemCount`.
Want to practice and check your answers?
Sign up to access all questions with instant feedback, explanations, and progress tracking.
Start Practicing Free