Variables in Python are used as a storage container that contains the value we assign them. It is a name with an assigned value inside used to reference during the program execution. Python variables are different from other programming languages; their type need not be explicitly declared. They pick the kind based on the value assigned to them.
Here, we are going to explain to you the complete concept of Variables in Python, along with their usages and best practices to implement them in your Python programs.
What Are Variables In Python?
Variables in Python are storage containers used to contain values assigned to them, which are later referenced for executing different operations. They are used to store data types including integers, numbers, strings, lists, and more. Variables make it easy to make modifications by referencing different elements in the program.
Variables In Python: Key Takeaways
- Variables in Python are containers used to store values, including integers, numbers, strings, and lists.
- You do not need to explicitly define the types of Python variables, as it is a dynamically typed language.
- You can assign a value to the variables in Python using the “equal sign” operator.
- You need to start Python variables with a letter or underscore, do not use hyphens.
- Variables in Python are case sensitive which must be taken care while defining names.
Variables In Python Syntax Rules
You can declare a Python variable using any names you like, while following the syntax rules. Let us check the syntax rules of Variables in Python below.
- When you name a Python variable, it automatically creates its space in memory.
- Variables in Python can only contain underscores (_), letters, or digits.
- You do not need to declare Python variables type as it is a dynamically typed language.
- You have to start variables in Python either with a letter or an underscore.
- Make sure you do not use special characters or spaces in Python variables.
- Variables in Python can contain letters or digits, but must not start with these.
- You must not use Python keywords like if, while, class, for, and more to define variables.
- Variables in Python are case sensitive, and hence “python” and “Python” are not the same.
Read More: Python OOPs Concepts Explained: Master Object-Oriented Programming in Python
Will Special Characters Work In Python Variables?
Variables in Python must not contain a special character inside. Let us verify the output with Python variables containing special characters.
- Hyphens are not allowed in Python variables
- The Dollar Sign ($) is not allowed in a Python variable
- You must not use @ in Python variables
- Space is not allowed in Python variables.
Make sure you keep these things in mind while naming your variables in Python; otherwise, Python will throw an “Invalid Syntax” error.
| user-name = “Alice”
total$ = 500 @value = 10 first name = “Bob” |
Output
![]() |
How to Use Variables In Python Programming?
Python variables are easy to implement and use. You can define variables using simple names of your preference or choice. You only have to assign a value to Python variables using the equal sign (=).
| variable_name = value |
- variable_name = The name of the variable that is used for referencing.
- =: It is used to assign a value to the variable.
- value: Value can be either a number, a string, or a list that is stored inside.
Python variables need no prefixes while defining, like other programming languages, where you need to explicitly declare the type of variable.
Read More: Learn Indentation in Python Code: Rules, Errors & Fixes Explained
Create Variables In Python using Assignment (=) Operator
Python values can easily be assigned using the equal ( = ) operator. You only need to come up with a relevant name for your variable. Python is a dynamically typed language, and hence you are not required to explicitly define the type of data to be stored in the variable.
Let us check simple examples to get familiar with how to assign your Python variables.
| x = “This is Physics Wallah.”
y = 3.5 z = “Hello, World!” |
The variables here i,e. x, y, and z, all contain different types of data, including strings, numbers, and more. You can print variables on scree using the print () method using any of the variables you defined with the values inside.
Output
![]() |
Multiple Assignments to Python Variables
Variables in Python support multiple variables to be assigned with some values in a single line. You can either assign the same value to all variables or different values. This feature makes the code concise and easier to read. Let us understand how to use multiple assignments to Python variables.
| a = b = c = 50
print (a, b, c) x = y = z = 1, 5.5, “Python Variables” print (x, y, z) |
Output
![]() |
Type Casting Variables In Python
You can easily convert the data type of your Python variables from one type to another. You can specify the data type of the variable to convert them.
- int ( ): Convert values into integer
- float ( ): Convert values into floating point numbers
- str ( ): Convert values into string
Let us take example for each type of data values converting one form into another.
1. int ( )
The first example here converts the float number into integer and string into integer using the int ( ) function.
| x = 5.9
y = int(x) print (type(y)) print(y) a = “10” b = int(a) print (type(b)) print(b) |
Output
![]() |
2. float ( )
The first example convert the integer value to float value using the float () function.
| x = 10
y = float(x) print (type(y)) print(y) a = “7.5” b = float(a) print (type(b)) print(b) |
Output
![]() |
3. str ( )
The given example convert a number (int) value to string using the str ( ) function.
| x = 25
y = str(x) print (type(y)) print(y) |
Output
![]() |
How to Get the Type of Variables?
If you want to know the type of variable contained inside a variable, then you can get the type using the type () method. The simple syntax to find the type of variables in Python is given below.
| type (variable_name) |
Let us check the types of data using simple examples given below.
| x = 10
y = 3.14 z = “Hello” a = True print(type(x)) print(type(y)) print(type(z)) print(type(a)) |
Output
![]() |
How to Delete Variables In Python?
You can use the del() method in Python to delete any variable you want. The del keyword can delete the variable and free up memory space in your system where it was used.
| name = “Ankit Kumar” |
You want to delete this variable “name” containing value “Ankit Kumar” inside. Let us check how we can delete it using the “del” keyword.
| name = “Ankit Kumar”
del name |
Now, the “name” variable is deleted and if you want to print it will raise an error saying name is not defined.
Temporary Variables In Python
In Python, temporary variables are used to store intermediate results of a process during computation. A simple example of temporary variables in Python, is when you need to swap values between different variables.
| >>> x = 6
>>> y = 7 >>> temp = x >>> x = y >>> y = temp >>> print (x) >>> print (y) |
Python Variables In for Loop
Now, as we were discussing that variables in Python are used to reference different entities during program execution. Let us understand it using a simple example given below.
| fruits = [“apple”, “banana”, “cherry”]
for fruit in fruits: print (fruit) |
Output
![]() |
Here, we are defining a list in Python containing three items within a list variable i,e. fruits. We can print each item from the list using the “fruit”, which references one item from the list at a time. With the help of print() function we print each item from the list one by one.
Read More: Python For Loops: Complete Overview For Beginners
Build Career As A Python Developer With PW Skills
You can become a perfect Python developer and start your career in top MNCs. We are offering Decode DSA With Python Course to help you strengthen your fundamentals of Python programming. Enroll in our course to experience interactive tutorials and hands on training exercises to build your logic making capabilities. Get doubt solving sessions, assignments, quizzes, real world projects, and more.
Get industry recognised certification from PW Skills which will help you strengthen your job portfolio and stand out from other candidates during recruitment season.
Variables In Python Programming FAQs
Q1. What are Variables in Python?
Ans: Variables in Python are used to store data value inside. The stored data can be manipulated and referenced during the program execution.
Q2. How to define variables in Python?
Ans: You can simply define Python variables using the equal “=” operator and name of the variable you want. Make sure you follow the naming rules while defining variables in Python.
Q3. What is dynamic typing in Python variable?
Ans: Python is a dynamic typed programming language where you do not explicitly need to declare the type of a variable. You only need to specify the name of the variable and assign it a value of your choice.
Q4. Can I delete a Python variable?
Ans: Yes, you can delete a Python variable using the simple del keyword. This will also remove the memory space used by that variable you deleted.








