[Link].
in
Python Variables
1. What is a Variable?
• A variable is a name assigned to a memory location that stores a value.
• Python variables are dynamically typed, meaning they don’t require an
explicit type declaration.
2. Variable Naming Rules (Identifiers)
A variable name (also called an identifier) follows these rules:
• Starts with a letter (A-Z or a-z) or an underscore ( _ ).
• Can contain letters (A-Z and a-z), numbers (0-9), and underscores ( _
).
• Case-sensitive (Fruits and fruits are different variables).
• Cannot start with a number.
• Cannot contain spaces or special characters (@, $, %).
• Cannot use Python keywords (like class, def, if).
Examples
1
[Link]
3. Naming Conventions in Python
Variable names with more than one word can be difficult to read.
To make variable names readable, follow these conventions:
Snake Case (Recommended for variables & functions)
• Words are separated by underscores (_).
Pascal Case (Used for Class Names)
• Each word starts with an uppercase letter, without underscores.
Camel Case (Used in some libraries, not common in Python)
• First word is lowercase, following words start with uppercase.
Uppercase (For Constants)
• Constants are written in all uppercase with underscores.
2
[Link]
4. Assigning Values to Variables
Single Assignment
Multiple Assignments
5. Type Checking
We can get the data type of a variable with the type() Class
Best Practices for Naming Variables
• Use descriptive names (user_age instead of x).
• Follow snake_case for variable names.
• Use UPPERCASE for constants.
• Avoid single-letter variable names (except in loops, like i, j).
Note:
The concepts of global variables and local variables will be taught in the
Functions topic because they require an understanding of functions.