In this article, I am going to discuss Variables in Python with Examples. All the data which we create in the program will be saved in some memory location on the system. The data can be anything, an integer, a complex number, a set of mixed values, etc.

What is a variable in Python?

Variable is the name that we give to the memory location which holds some data. With a variable, we can store the data, access the data, and also manipulate the data. We can also refer to variables as containers which store some information/data.

Python variables key Points:

  • Python has no command for declaring a variable.
  • A variable is created the moment you first assign a value to it.
  • Variables do not need to be declared with any particular type and can even change type after they have been set.
  • If you want to specify the data type of a variable, this can be done with casting.

Creating a variable in Python:

Python is a dynamically typed programming language. There is no need of specifying the data type for a variable. See example below

x = 4              # x is of type int
y = "Code Config"  # y is of type string
print(x)
print(y)

Python variable casting:

If you want to specify the data type of a variable, this can be done with casting.

x = str(2)    # x will be '2'
y = int(2)    # y will be 2
z = float(2)  # z will be 2.0

Get the Type of Python Variable:

You can get the data type of a variable with type() unction.

x = 5
y = "Code-Config"
print(type(x))
print(type(y))

Single or Double Quotes?

String variables can be declared either by using single or double quotes, both are same:

x = "CodeConfig"
# is the same as
x = 'CodeConfig'

Case-Sensitive:

Python variable names are case-sensitive.

a = 4
A = "Nitika"

#A will not overwrite a because it is case sensitive 

Can not use reserve Keyword in variables:

In Python creating a variable with keyword name is not allowed. like with using if, else or for keyword

if = 10   # 'if' is reserved keyword & cannot be used as variable.
print(if)

Assigning multiple variables in single line:

In Python it is also possible to assigning multiple variables in single line, Check example

a,b,c = 1,2,3

print(a,b,c)

#Output: 1 2 3

Re-initialize the variables in Python:

In Python we can reinitialize the variable values. The old values will be overridden or replaced with the new values.

a = 10
print("At first I have assigned value: ", a)
a = 20
print("On re-initialization of variable, now its value is: ", a)

#Please note value 10 will get overridden with 20

Explore More

Python tutorials

We are starting a Python Tutorials Series for Beginners and Professionals. In these Python Tutorials, we will cover all the features of Python. You will learn from basic to advanced-level features

f-strings in Python

Python offers a powerful feature called f-strings (formatted string literals) to simplify string formatting and interpolation. f-strings is introduced in Python 3.6 it provides a concise and intuitive way to embed expressions and variables

Input and Output in Python

Understanding input and output operations is fundamental to Python programming. With the print() function, you can display output in various formats, when the input() function/method enables interaction with users by gathering