Learn Python
Python Introduction |
Python Installation |
Python Hello World |
Python Tokens |
Python Program Structure |
Python Variables and Assignments |
Python Input Output |
Python Data Types |
Python Mutable Immutable |
Python Opeartors |
Python Expression |
Python if statement |
Python range method |
Python Loop |
Python break continue |
Python Loop else |
Python Nested Loop |
Variables and Assignments
A variable is a just like a container that stores values, which you can access and changed.
Variables and Assignments
A variable is a just like a container that stores values, which you can access and changed. As its name suggests that value of variables can change during the program execution.
With the help of variable we can store value in computer’s memory. Variable is a named storage location. In Python, every element is termed as an Object. Hence, a variable is also an object in Python.
Creating a Variable
In Python, Variables are created by assigning value of desired type to them i.e. Python created the variable of the type similar to the type of value assigned.
Example -
To create a number variable, assign a numeric value to variable_name
age = 25
marks = 56
To create a string variable, assign a string value to variable.
name = ‘Anjeev Singh’
msg = “ Thanks for watching my videos’
To create a float variable, assign a float value to variable.
amt = 2589.36
pi = 3.14
To create a Boolean variable, assign a Boolean value (True / False) to variable
choice = True
Wish = False
In Python, Variable is the reference type, which always point to certain memory location.
In Python, Memory has literals/values at defined memory locations, and each memory location has a memory address. So when we write age = 25, a variable age will created as a label pointing to memory location where 25 is stored. And when we give statement age = 30, label age start referring different location.
age = 25
t = 25
Value 25 26 27 28 29 30
Address 98 94 90 02 06 10
t
age
After writing, age = 30
Value 25 26 27 28 29 30
Address 98 94 90 02 06 10
t age
Lvalue and Rvalue
Lvalues are the objects to which we can assign a value. It can come on the lhs (left hand side) of an assignment operator.
Rvalues are the objects / literals / expression that are assigned to lvalues. It can come on the rhs(right hand) of an assignment operator.
Example : lvalue = rvalue
X = 25 #Here Rvalue is a literals
Y = X #Here Rvalue is a variable or object
Z = X + Y #Here Rvalue is an expression.
Multiple Assignments
Python allows to assign multiple values to multiple variables. All variables and values are separated with comma, and evaluated from left to right and assigned in same order.
Example-
a = b = c = 20 # Assigning same value to multiple variable
a, b, c = 20, 40, 50 # Assigning different values to different variable
a, c = a+b, b+c # First expression will be evaluated then values assigned to the variable
b = 50
b, b = b+2, b+5 # b, b = 50+2, 50+5 => b,b, = 52, 55
print(b) # 55
Note: Suppose you have written multiple values separated by comma in rhs of assignment operator, but did not written multiple variable names in lhs, then Python create a tuple internally,
u = 4, 5 # will not raise an error, it will create a tuple
type(u) # class ‘tuple’
print(u) # shows result (4,5)
Variable Definition
Assigning a value to the variable in Python, is called Variable Definition or Variable assignment.
print(q) # Error : name ‘q’ is not defined
q = 153.36 # Variable ‘q’ is defined here
print(q) #153.36
Dynamic Typing:
As we know that, In Python, the type of variable depend upon the value assigned. Python allows, a variable pointing to a value of a certain type, can be made to point to value of different type. This is called Dynamic Typing.
Example :
d = 35.65 # ‘d’ starts referring to an float value.
print(type(d)) # <class ‘float’>
d = ‘Anjeev’ # ‘d’ starts referring to a string value.
print(type(d)) # <class ‘str’>
type(variable or object or literal) : type( ) is a built-in-method in python, which returns the type of variable or object or literal.
e.g.
a = ‘hello’
type(a)
<class ‘str’>
b = 5 + 6j
type(b)
<class ‘complex’>
Internal Component of Variable:
In Python, a variable / object has three main components:-
(a) Identity of the Variable / Object
(b) Type of the Variable / Object
(c) Value of the Variable / Object
(a) Identity of the variable/Object
Can be checked by using the method id(variable/object name)
This method return the address pointed by variable or address where value stored.
Example :
x = 25
print(id(x))
1254698
(b) Type of a Variable / Object
Type means, data type of variable In Python, each value is an object has a data type associated with it. Data type of a variable tell us about the data contained by variable and allowed associated operations on it.
type(variable or object) – type() is a built-in function in Python, which returns the data type of the variable or object.
(c) Value of Variable / Object
Variable is a means to name a value and used in programs to access & manipulate value. To assign or give a value to variable, we can use assignment operator (=). In Python, this is called building of a variable. For
Example
amt = 2586.36 # building a variable amt, which value is 2586.36
maths = 98 # building a variable maths, which value is 98
science = 75 # building a variable science, which value is 75
name =’Anjeev Singh’ #building a variable name, which value is ‘Anjeev Singh’
print(amt) # Printing value of a variable amt
2586.36 # Value of variable ‘amt’
totoal = math + science #building a variable total, by using math and science
print(total) #printing value of variable ‘total’
173