05. Mastering Python Variables: Declaration, Rules & Multiple Assignments
Introduction
Variables are fundamental in programming—they act as containers for storing data values that your programs can manipulate and refer to. In Python, variables are especially flexible and dynamic. In this post you will learn how to create variables, understand naming rules, use multiple assignments, and apply casting. These basics will help you write clearer and more powerful Python code.
What Are Variables in Python?
In Python, a variable is a named reference to a value in memory. You don’t have to explicitly declare the variable type—one of Python’s strengths is that it handles type assignment automatically when you assign a value.
Example:
At this point, x refers to the integer 5, and y refers to the string "Alice".
You can also reassign a variable to a different type:
This dynamic typing is part of what makes Python very versatile.
Casting / Specifying a Variable Type
Sometimes you want to explicitly convert a value from one type to another. Python supports casting to let you do this.
Example conversions:
When you want to convert a string to a number or vice versa, casting comes in handy.
To check the type of a variable, use type():
Variable Naming Rules
To write correct and maintainable code, variable names in Python must follow specific rules.
Key rules:
- A variable name must start with a letter or underscore (_). It cannot start with a digit.
- It can only contain letters, digits, and underscores (i.e. alphanumeric + _).
- Names are case-sensitive: for example, age, Age, and AGE are distinct variables.
- A variable name cannot be a reserved keyword in Python (such as if, for, class, etc.).
Examples of valid names:
Examples of invalid names:
When you have multiword variable names, here are common styles:
- Camel case: myVariableName
- Pascal case: MyVariableName
- Snake case (most common in Python): my_variable_name
- Use underscores to separate words—readable and consistent.
Assigning Multiple Variables
Python allows convenient ways to assign values to multiple variables in a single line. This helps make your code more concise.
Many values to multiple variables:
Make sure the number of variables matches the number of values, or you will encounter an error.
One value to multiple variables:
Unpacking collections (list, tuple, etc.):
You can use this unpacking pattern to assign values from tuples and lists to variables directly.
Global Variables and Variable Scope
A variable defined outside of any function is called a global variable. Global variables can be accessed both inside functions and outside them.
Example:
If you define a variable inside a function with the same name as a global, that becomes a local variable, limited to the function’s scope.
If you want to modify a global variable inside a function, use the global keyword:
Best Practices & Tips
- Use snake_case for multiword variable names (e.g. user_name, total_amount).
- Choose descriptive names instead of single letters (unless in small loops): e.g. price, count, username.
- Avoid naming variables after built-in types or functions (e.g. don’t name a variable list, str, int).
- Keep variable scope minimal: use local variables when possible to avoid unintended side effects.
- When unpacking, ensure the structure matches exactly (same number of items).
Summary
Variables lie at the heart of Python programming. You don’t need to declare types ahead of time—just assign a value and Python infers it. You can also cast values explicitly, follow naming rules, assign multiple variables in elegant ways, and manage variable scope with global vs local context. Mastering these concepts will give you a strong foundation for writing clean, effective Python code.
05. Mastering Python Variables Declaration Rules Multiple Assignments
coldshadow44 on 2025-10-08
(0)