Python Variables

Python Variables

May 7th, 2026
195
15:00 Minutes

Many people often look into what a programming language does on its own and what is going on behind the scenes within the programming language. I have been working with Python for over three years, using it for projects and resolving issues and figuring out how different areas of the language operate.

As a result of my past experiences, I have developed a good knowledge of how variables, data types and logical structures work in a real world environment.

In this blog, I want to give you some of those answers in a simple way that you can apply today. Let's get started!

What are Python Variables?

In Python, variables are symbolic names that act as references to objects stored in memory. You can think of them as labeled containers or tags that hold data values, such as numbers or text, so you can easily access and manipulate them throughout your code.

For example:

# Assigning values to variables
name = "Juliana"
age = 27
height = 5.8

# Printing variables
print("Name:", name)
print("Age:", age)
print("Height:", height)

what are python variables

How it works:

  • name, age and height are variables
  • The = sign is used to assign values
  • print() is used to display the values

Read Also: Python Tutorial for Beginners

Rules for Naming Variables

When you are writing Python programs, it is very important to choose the right variable name. It will not only avoid errors but also make your code easier to read and understand. Python follows some rules for naming variables that every beginner should know.

By following these rules, you can write clean, clear and professional code that others can easily understand.

1. A variable name should begin with a letter or an underscore and not just a number.

2. Only letters, digits and underscores are allowed in variable names.

3. Variable names are case-sensitive, so value, Value and VALUE are all different.

4. Do not use reserved words such as if, else, for, or while as variable names.

5. Choose names that clearly describe what the variable is storing.

6. Spaces are not allowed in variable names and for that you can use styles like totalMarks or total_marks.

Assigning Values to Variables in Python

In Python, a variable is like a container storing data. When you assign a value to a variable, it means you are putting some data into that container so you can use it later in your program.

Here are some steps and methods by which you can assign values to variables in Python:

  • Direct Initialization Method

In this method, you have to directly assign the value in Python but in other programming languages like C and C++, you have to first initialize the data type of the variable. In Python, there is no need for explicit declaration of variables as compared to using some other programming languages. You can simply start using the variable right away.

For example:

# initializing variable directly
b = 10

# printing value of b
print("The value of b is: " + str(b))

direct initialization method in python variable

  • Using Conditional Operator

You can also call it the Ternary operators. The Basic Syntax of a Conditional Operator is:

value_if_true if condition else value_if_false

The conditional operator is a one line shorthand for an if-else statement. It allows you to evaluate a condition and return one of two values based on whether that condition is true or false.

For example:

# initializing variable
age = 18

# using conditional operator
result = "Adult" if age >= 18 else "Minor"

# printing result
print(result)

using conditional operator in python variable

How it works:

  • If age >= 18 is True, it returns "Adult"
  • If the condition is False, it returns "Minor"

Read Also: Python Interview Questions and Answers

Multiple Variable Assignments

In Python, you can also assign values to multiple variables in a line. This makes your code shorter and readable.

1. Assigning multiple variables at once

You can assign different values to different variables in one line. For example:

# assigning multiple variables
x, y, z = 5, 10, 15

print(x)
print(y)
print(z)

assigning multiple python variables at once

Explanation:

  • x, y and z are three variables written together.
  • 5, 10, 15 are the values assigned to those variables.
  • Python assigns values based on position (left to right):

a. x gets 5

b. y gets 10

c. z gets 15

2. Assigning the same value to multiple variables

You can also assign the same value to multiple variables. For example:

# assigning same value to multiple variables
a = b = c = 20

print(a)
print(b)
print(c)

assigning the same value to multiple python variables

Explanation:

  • a, b and c are three variables.
  • 20 is the value being assigned.
  • Python assigns the same value (20) to all three variables at once.

How it works:

  • First, 20 is created.
  • Then all variables (a, b and c) are linked to that value.

Read Also: Python Operators

Type Casting and Checking Variable Type

In Python, type checking allows you to identify a variable's data type, while type casting lets you convert a value from one type to another.

1. Checking Variable Type

By this, you can check the type of a variable using the built-in type() function.

For example:

a = 10
b = 3.14
c = "Hello"

print(type(a))  # <class 'int'>
print(type(b))  # <class 'float'>
print(type(c))  # <class 'str'>

checking python variable type

Explanation:

  • type(a) tells us that a is an integer (int)
  • type(b) tells us that b is a float (float)
  • type(c) tells us that c is a string (str)

2. Type Casting

Type casting means changing one data type into another using in built functions. Following are some common type casting functions:

  • int(): converts to integer
  • float(): converts to float
  • str(): converts to string

x = "10"   # string
y = int(x) # convert string to integer

print(y)         # 10
print(type(y))   # 

type casting in python variable

Explanation:

  • x is a string "10"
  • int(x) converts it into number 10
  • Now y becomes an integer

Read Also: Python Data Structures

Object Reference and Variable Deletion

In Python, variables act as references to objects stored in memory rather than holding data directly. When you understand the object references, it will help you see how data is shared and modified. Not only this, but knowing how to delete variables will ensure better memory management and cleaner code.

What is an Object Reference?

An object reference is a pointer or a "label" that points to the memory location where an actual object is stored. Compared to other languages where variables are "buckets" that contain a value, Python variables are names that simply refer to objects.

For example:

# Creating a list object
list1 = [1, 2, 3]

# Assigning the same object to another variable
list2 = list1

# Modifying list2
list2.append(4)

# Printing both variables
print(list1)  # Output: [1, 2, 3, 4]
print(list2)  # Output: [1, 2, 3, 4]

what is an object reference

Explanation:

  • list1 is created and refers to a list object [1, 2, 3]
  • list2 = list1 means both variables point to the same object
  • When we modify list2, the change is reflected in list1 as well

What is Variable Deletion?

Variable deletion in Python is the process of removing a variable's name from the current namespace, which makes it inaccessible for further use.

Syntax:

del variable_name

How to Delete a Variable?

You can remove a variable using the del keyword. It removes reference to the variable, which will make it no longer accessible in your program.

1. Deleting a Single Variable

del variable_name

2. Deleting Multiple Variables

del var1, var2, var3

3. Deleting Elements from a List

The del keyword can be used to remove specific elements or a range of elements from a list:

del my_list[index]          # removes one item
del my_list[start:stop]     # removes a slice of items

4. Deleting Keys from a Dictionary

del my_dict[key]

What Actually Happens When You Delete a Variable?

When you use del:

  • The variable reference is removed
  • The object is deleted only if no other variables are pointing to it

Example with multiple references:

a = 10
b = a

del a

print(b)  # 10 (still works)

delete a python variable

Explanation:

  • a is deleted
  • But b still points to the object 10
  • So the object is not deleted from memory

When Does Python Delete the Object?

In Python when you delete a variable, it does not always mean the object is removed from memory. Python only deletes an object when it is no longer being used.

Example: Object NOT Deleted

a = 10
b = a

del a

print(b)  # Output: 10

Explanation:

  • a is deleted
  • b still refers to the object
  • The object remains in memory

Wrapping Up

In conclusion, Variables in Python are references to objects, which enables dynamic assignment with no type declarations. Clear variables names help programmers read their code easily. By enabling each line of code to have multiple assigned variables, time is saved by limiting how many times code needs to run. The type checking and type casting features of Python provide tools to manage the data types of your code.

By being familiar with how object references and deletion work in Python, you will be better prepared for correctly handling memory while writing efficient Python programs.

FAQs

1. Do I need to declare a variable type in Python?

No, you do not have to do this as Python automatically detects the data type when you assign a value, which means that you do not need to declare it manually.

2. Are Python variable names case-sensitive?

Yes, Python is case sensitive. For example, value, Value and VALUE are treated as different variables.

3. How can I check the type of a variable in Python?

You can use the type function to check the data type of a variable.

About the Author
Sanjay Prajapat
About the Author

Sanjay Prajapat is a Data Engineer and technology writer with expertise in Python, SQL, data visualization, and machine learning. He simplifies complex concepts into engaging content, helping beginners and professionals learn effectively while exploring emerging fields like AI, ML, and cybersecurity in today’s evolving tech landscape.

Drop Us a Query
Fields marked * are mandatory

Programming Certification Courses

×

Your Shopping Cart


Your shopping cart is empty.