Lesson: Python Variables
Introduction
Imagine telling your computer to remember numbers for you, then using those numbers to add, subtract, or even calculate powers – all with just a few lines of code.
In this lesson, you’ll explore Python variables and basic operations, forming the foundation for building more advanced programs. Whether you’re automating a simple task or analysing complex data, these concepts are essential.
What Is a Variable?
A variable in Python is like a labelled container that stores information. You can:
-
Save values like numbers or text
-
Change them later
-
Use them in operations and functions
Python automatically detects the data type (no need to declare it).
Real-World Analogy:
Think of a variable as a storage box:
-
You label it (
user_name) -
Put something inside (
"Alex") -
Use that label later to access or change the content
Example:
user_name = "Alex"
age = 30
print(user_name)
print(age)
Output:
Alex
30
Variable Naming Rules & Best Practices
To make your code readable and error-free, follow these naming rules:
| Rule | Description |
|---|---|
Must begin with a letter or underscore _ |
Valid: user, _name |
| Cannot start with a number | Invalid: 1user |
| Can contain letters, numbers, and underscores | Valid: user_name1 |
| Avoid reserved keywords | Invalid: for, if, class, etc. |
Use lowercase and underscores (snake_case) |
Follows PEP 8 (Python style guide) |
