site logo

Learn to Python. Build the Future.


Category: (All)
❮  Go Back

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:

x = 5
y = "Alice"
print(x)
print(y)

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:

x = 4 # x is integer
x = "Hello" # now x becomes a string
print(x)

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:


x = str(3) # x becomes "3"
y = int(3) # y becomes 3
z = float(3) # z becomes 3.0

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():


x = 5
y = "Bob"
print(type(x)) # output: <class 'int'>
print(type(y)) # output: <class 'str'>


Variable Naming Rules

To write correct and maintainable code, variable names in Python must follow specific rules.

Key rules:

  1. A variable name must start with a letter or underscore (_). It cannot start with a digit.
  2. It can only contain letters, digits, and underscores (i.e. alphanumeric + _).
  3. Names are case-sensitive: for example, age, Age, and AGE are distinct variables.
  4. A variable name cannot be a reserved keyword in Python (such as if, for, class, etc.).


Examples of valid names:

myvar = "John"
my_var = "Jane"
_myvar = "Alice"
myVar = "Bob"
MYVAR = "Carol"
myvar2 = "Dave"

Examples of invalid names:

2myvar = "John" # starts with a digit – invalid
my-var = "Alice" # hyphen not allowed
my var = "Eve" # space not allowed


When you have multiword variable names, here are common styles:

  1. Camel case: myVariableName
  2. Pascal case: MyVariableName
  3. Snake case (most common in Python): my_variable_name
  4. 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:

x, y, z = "Orange", "Banana", "Cherry"
print(x)
print(y)
print(z)

Make sure the number of variables matches the number of values, or you will encounter an error.

One value to multiple variables:

x = y = z = "Apple"
print(x)
print(y)
print(z)

Unpacking collections (list, tuple, etc.):

fruits = ["apple", "banana", "cherry"]
x, y, z = fruits
print(x) # apple
print(y) # banana
print(z) # cherry

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:

x = "awesome"

def my_function():
print("Python is " + x)

my_function()
print("Python is " + x)

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:

x = "awesome"

def my_function():
global x
x = "fantastic"

my_function()
print("Python is " + x) # now prints “Python is fantastic”


Best Practices & Tips

  1. Use snake_case for multiword variable names (e.g. user_name, total_amount).
  2. Choose descriptive names instead of single letters (unless in small loops): e.g. price, count, username.
  3. Avoid naming variables after built-in types or functions (e.g. don’t name a variable list, str, int).
  4. Keep variable scope minimal: use local variables when possible to avoid unintended side effects.
  5. 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)





Showing comments related to this post.




Member's Sites: