A Detailed Guide to Python Variables

Variables are one of the most fundamental concepts in programming. They act as containers in which you can store data and change it at will within the program.

In algebra, you use symbols like ‘x’ or ‘y’ to represent values. Variables work the same way, but they are more powerful and flexible.

Python Variable Featured Image

What are variables in Python?

A variable is a named storage location that holds a value. It allows you to give data a descriptive name so you can reference and use it later. You can write flexible and readable code without having to hardcode values.

For example, instead of writing the number 35 over and over again, you can store it in a variable user_age and use user_age everywhere.

How to Create (Declare) Variables in Python

Creating variables in Python is very simple. You don’t need to explicitly declare a type. Just assign a value using the assignment operator =.

Structure:

variable_name = value

Example:

first_name = "Alice"
user_age = 30

print(first_name)
print(user_age)

In this example:

  • We created the first_name variable and assigned it the text value “Alice”.
  • We created the user_age variable and assigned it the number 30.

Python automatically understands that “Alice” is text (string) and 30 is a number (integer), looking at the value.

Free Course

Python Fundamentals for Beginners Free Course

Master Python basics, from variables to data structures and control flow. Solve real-time problems and build practical skills using Jupyter Notebook.

13.5 hrs
4.55
Enroll for Free

Naming Rules and Conventions

There are certain rules for naming variables in Python, and some community-accepted conventions that make the code more readable.

Rules (Must Follow)

  • Variable names must start with a letter (a-z, A-Z) or an underscore (_).
  • Variable names cannot start with a number.
  • Variable names can only contain alphanumeric characters and underscores (A-Z, 0-9, and _).
  • Variable names are case-sensitive. Python treats age, Age, and AGE as three separate variables.
  • Python reserved keywords (such as if, for, while, class) cannot be used for variable names.

The standard convention in Python is snake_case. Write all letters in lowercase and separate words with underscores. This is best for readability. Other formats work as well, but snake_case is preferred.

Good Examples:

  • user_first_name
  • total_amount
  • is_active

Not Recommended:

  • userFirstName # camelCase
  • UserFirstName # PascalCase

Common Data Types of Variables

A variable can hold different types of data. Python automatically assigns a data type when you create a variable. The most common types are:

  1. String (str)

    Used for text. To create a string, enclose the text in single or double quotes.


    user_greeting = "Hello, World!"
  2. Integer (int)

    Used for whole numbers, positive or negative, without decimals.


    item_count = 100
  3. Float (float)

    Used for numbers with a decimal point.


    price = 19.99
  4. Boolean (bool)

    Represents a value of either True or False. Booleans are often the result of comparisons.


    is_logged_in = True

How to Check a Variable’s Type

If you’re ever in doubt about the data type stored in a variable, use Python’s built-in type() function. This is very helpful for debugging or understanding other people’s code.

How to Use:

  1. Call the type() function.
  2. Pass the variable’s name within parentheses.

Example:

user_name = "John Doe"
items_in_cart = 5
total_cost = 124.75

print(type(user_name))      # Output: <class 'str'>
print(type(items_in_cart))  # Output: <class 'int'>
print(type(total_cost))     # Output: <class 'float'>

This immediately identifies which variable is a string, which is an integer, and which is a float.

How to Reassign Variables

You can change the value of any variable at any time by simply assigning a new value to it. This process is called reassigning. Python is a dynamically-typed language, meaning you can also change the data type of a variable.

Example:

# First assign an integer value
user_id = 123
print(user_id) # Output: 123

# Now assign a new integer value
user_id = 456
print(user_id) # Output: 456

# Now assign a different data type (string)
user_id = "user-abc-456"
print(user_id) # Output: user-abc-456

Deleting Variables

If you no longer need a variable, you can delete it with the del keyword. If you try to access it after deleting it, you will get an error.

Syntax:

del variable_name

Example:

customer_name = "temporary user"
print(customer_name)

# Delete the variable
del customer_name

# The next line will give a NameError because the variable no longer exists.
# print(customer_name)

Key Points

  • Create variables by assigning a value with the = operator.
  • Use snake_case to give them readable names and follow Python’s naming rules.
  • Check the variable’s data type with the type() function.
  • You can assign new values to variables or delete them completely.
Avatar photo
Great Learning Editorial Team
The Great Learning Editorial Staff includes a dynamic team of subject matter experts, instructors, and education professionals who combine their deep industry knowledge with innovative teaching methods. Their mission is to provide learners with the skills and insights needed to excel in their careers, whether through upskilling, reskilling, or transitioning into new fields.
Scroll to Top