Working with variables in Python | How to use variables in Python

Working with variables in Python | How to use variables in Python

A variable in Python represents named location that refers to a value and whose values can be used and processed during program run.

Example :

Name=”Lovejot”
Percentage=91

There are two variables Name and Percentage with values “Lovejot” and 91 respectively.

Create a Variable in Python

In Python , we can simply write a statement containing a valid identifier assigned with a valid value. Python automatically creates the variable of the same data type as the data type of value assigned.

Example 1:

marks=90

In above statement, variable marks is an integer variable containing value 90.

Example 2:

Sname=”Lovejot”

In above statement, variable Sname is a string variable containing value “Lovejot”.

Example 3:

Balance=1000.50

In above statement, variable Balance is a float variable containing value 1000.50.

Lvalues and Rvalues in Python

lvalue : it is the variable or expressions that can come on the left hand side of an assignment statement.
rvalue : it is the expression that can come on the right hand side of an assignment statement.
Example:

Name=”Lovejot”

Here Name is lvalue and “Lovejot” is rvalue.
“Lovejot”=Name is wrong as rvalue “Lovejot” can’t come at left hand side of assignment statement.
** Lvalues can come on left hand side as well as right hand side of an assignment statement.
**Rvalues can only appear at right hand side of assignment statement.

Multiple Assignments in Python

1. Assigning same value to multiple variables in Python

We can assign same value to multiple variables in a single statement
Example:

a=b=c=20

It will assign value 20 to variables a, b, c i.e. all three variables a,b and c will refer to same location with value 20.

2. Assigning multiple values to multiple variables

We can even assign multiple values to multiple variables in single statement.
Example:

x,y,z = 10, 20, 30

First variable is x is given first value i.e. 10, second variable y gets second value i.e. 20 and third variable z gets value 30.
Because x is having value 25 and y is having 50. Now, if you want to swap values of x and y, you just need to write :

3. Interchanging values of two variables

We can also interchange values of two variables in single statement.
Example:

x,y=10,20 #x gets 10 and y gets 20
x, y = y , x #x gets values of y i.e. 20 and y gets values of x i.e. 10
print (x,y) will give output as interchanged values i.e. 20 10

Variable Definition in Python

A variable in Python is not created until some value is assigned to it.
Example:

print (x) #Line 1
x = 20
print (x)

When we run the above code, it will produce an error for the first statement (Line 1) i.e. name ‘x’ not defined.
This is because x has not been assigned any value but it is printed in line 1.
So, to correct the above code, we need to assign some value to x before using it in a statement as

x = 0 #variable x created now
print (x)
x = 20
print (x)

Now the above code will run without any error.

Dynamic Typing in Python 

Dynamic typing means that data type of a variable can change during program execution.
Example:

x = 10 #Statement 1
x = “Hello” #Statement2
print(x)

In Statement1 variable x is referring to an integer value 10.
In Statement2 variable x has been assigned a string value ”Hello”.
The output of above code will be
Hello
Because value of variable x has been modified by value ‘Hello’

Initially variable x is first referring to an integer value 10 and then to a string value “Hello”.
This is not an error as Python will accept it . This is known as Dynamic Typing.
Caution with Dynamic Typing
There is no problem in Python to change data type of a variable but programmer needs to be careful while assigning values as wrong assignment may generate errors .
Example

X = 10
Y = X/2
X = ‘Hello’
Y = X/2 #this lines will give error as string value can’t be divided .

Click here for Quiz on Python Basics

Popular Books of Computer Science

Spread the love
Lesson tags: Assigning same value to multiple variables in Python, Create a Variable in Python, Dynamic Typing in Python, Lvalues and Rvalues in Python, Multiple Assignments in Python
Back to: Python Programming Tutorial
Spread the love