What are variables in python

Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory.

Based on the data type of a variable, the interpreter allocates memory and decides what can be stored in the reserved memory. Therefore, by assigning different data types to variables, you can store integers, decimals or characters in these variables.

Assigning Values to Variables in Python

Python variables do not need explicit declaration to reserve memory space. The declaration happens automatically when you assign a value to a variable. The equal sign (=) is used to assign values to variables.

The operand to the left of the = operator is the name of the variable and the operand to the right of the = operator is the value stored in the variable. For example −

 

age = 32 # An integer assignment
weight = 56.5 # A floating point
name    = "Jitendra" # A string
print age
print weight
print name

 

Here, 32, 56.5 and “Jitendra” are the values assigned to age, weight, and name variables, respectively. This produces the following result −

32
56.5
Jitendra

 

Multiple Assignment in Python

Python allows you to assign a single value to several variables simultaneously. For example −

a = b = c = 1

Here, an integer object is created with the value 1, and all three variables are assigned to the same memory location. You can also assign multiple objects to multiple variables. For example −

a,b,c = 1,2,”jitendra”

Here, two integer objects with values 1 and 2 are assigned to variables a and b respectively, and one string object with the value “jitendra” is assigned to the variable c.

Standard data types in python

The data stored in memory can be of many types. For example, a person’s age is stored as a numeric value and his or her address is stored as alphanumeric characters. Python has various standard data types that are used to define the operations possible on them and the storage method for each of them.

Python has five standard data types −

Copywrite © 2020-2024, CBSE Python,
All Rights Reserved