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 10 Programs for Beginners

Program 1:Write a program to create a 2D array using NumPy. Output: Program 2: Write a program to convert a python list to a NumPy array. Output: Program 3: Write a program to create matrix of 3×3 from 11 to 30. Output: Program 4: Write a program to create a data frame to store data of candidates who appeared in

Find index of first occurrence when an unsorted array is sorted

Find index of first occurrence when an unsorted an array is sorted Given an unsorted an array and a number x, find an index of first occurrence of x when

Find sum non repeating distinct elements array

Find sum of non-repeating (distinct) elements in an array Given an integer an array with repeated elements, the task is to find the sum of all distinct elements in the