Understanding Python Variables: A Comprehensive Guide

Variables are used to store and manipulate data in programming. In this tutorial, we’ll delve into the concept of variables in Python, exploring their definition, naming conventions, data types, and best practices.

Python variables declaration

In Python, a variable is essentially a name that refers to a value stored in memory. Unlike some other programming languages, you don’t need to declare the type of a variable explicitly; Python does this for you.

To declare a variable in Python, use the assignment operator (=) followed by the variable name. For example, age = 25 declares a variable named “age” and assigns it the value 25.

age = 25

In Python, variables are dynamically typed, which means you don’t need to explicitly declare their type. You simply assign a value to a variable, and Python will automatically determine its type based on the value you provide. Here’s how you declare and assign values to variables in Python:

# Assigning values to variables
name = "Alice"           # String
age = 25                 # Integer
height = 5.8             # Float
is_student = True         # Boolean
grades = [85, 90, 78]    # List
coordinates = (10, 20)   # Tuple
person = {"name": "Bob", "age": 30}  # Dictionary
unique_numbers = {1, 2, 3}  # Set

Naming Conventions

Python has some rules and guidelines for naming variables:

  1. Use lowercase letters for Python variables.
  2. Python variable names cannot start with a number.
  3. A variable name can only contain alphanumeric characters and underscores (A-z, 0-9, and _).
  4. Separate words with underscores (snake_case).
  5. Avoid using reserved words (e.g., “print”, “if”, “for”).
  6. Use descriptive names that indicate the variable’s purpose.
  7. Constants are typically written in uppercase (e.g., “MAX_VALUE”).
  8. Don’t use abbreviations.

Data Types

Python variables can hold various types of data, including:

  1. Integers: Whole numbers without decimal points, e.g., age = 25.
  2. Floats: Numbers with decimal points, e.g., pi = 3.14.
  3. Strings: Sequences of characters, e.g., name = "Alice".
  4. Booleans: Represents either True or False values, e.g., is_valid = True.
  5. Lists: Ordered collections of items, e.g., numbers = [1, 2, 3, 4, 5].
  6. Tuples: Similar to lists, but immutable, e.g., coordinates = (10, 20).
  7. Dictionaries: Key-value pairs, e.g., person = {"name": "Bob", "age": 30}.
  8. Sets: Unordered collections of unique items, e.g., unique_numbers = {1, 2, 3}.

Best Practices

To write clean and maintainable code, it’s important to follow some best practices when working with variables in Python:

  1. Descriptive Names: Choose meaningful variable names that reflect the purpose of the variable. For example, use total_students instead of just count.
  2. Consistent Casing: Stick to a consistent naming convention. Many Python developers prefer using lowercase letters and underscores (snake_case) for variable names.
  3. Initialize Variables: Always initialize variables before using them to avoid unexpected behavior.
  4. Avoid Magic Numbers: Instead of using raw numbers in your code, assign them to variables with descriptive names. This improves code readability and makes maintenance easier (e.g.: MAX_VALUE = 100).
  5. Use Comments: If a variable’s purpose might not be immediately obvious, provide comments to explain its significance and usage.

Python Global variables

A Python global variable is a variable that can be accessed and modified from any part of a program. It is defined outside of any function and can be used throughout the entire program. Global variables can be useful for storing data that is needed across different functions or modules. Here is an example :

greeting = "Hello"

def display_message():
  print( greeting + " nolowiz" )



Hello nolowiz

In the above code, the variable “greeting” is a global variable.

What happens if the same variable name is inside the function?

In the below example, the global variable name(Line number: 1) and variable( Line number: 4) inside the display_message function have the same name.

greeting = "Hello"

def display_message():
  greeting = "Hi"
  print( greeting + " nolowiz" )


print(greeting + " nolowiz")


Hi nolowiz
Hello nolowiz

So the “greeting” variable inside the display_message function is a local variable. However the global variable ( Line number: 1) value remains the same.

The global Keyword

The global keyword in Python is used to declare a variable as global within a function, allowing it to be accessed and modified outside the function. It is used to differentiate between local and global variables with the same name.

x = 10

def update_value():
    global x
    x = 20
print(x)  # Output: 10


print(x)  # Output: 20

Here global variable x is modified inside the update_value function using the global keyword.




In conclusion, variables are fundamental components of any programming language, and Python’s approach to variables adds a layer of simplicity and versatility to the coding experience. Read about the basics of a Python program.