Components of a program
Variables and assignments
Creating a variable
Creating variable was just that
simply only?
In python create a variable just
assign to its name the value of appropriate type.
For example – to create a variable
namely student to hold student’s name and variable age to hold the student’s
age, you just need to write somewhat similar to what is shown below :
student = ‘jack’
age = 16
Python will internally create labels
referring to these values shown below:
student → variable for name
age → variable for age of student
some more variables –
trainno = 12345
balance=3,45,635
roll_no
= 105
IMPORTANT
– Variables are not storage container in python
But
Python variables are not created in the form most other programming languages
do.
Lvalues and Rvalues
Lvalue – values which are on the lhs (left hand side) of an assignment.
Rvalue – expressions that can come on the rhs (right hand side) of an assignment.
e.g., you can say that –
a = 20
b = 10
e.g., you cannot say that
20 = a
10 = b
The literals or the expressions that
evaluate a value cannot come on lhs of an assignment hence they are rvalues but
variables name come on left hand side of an assignment, they are lvalues. lvalues
can come on left hand side as well as right hand side.
Multiple assignments
Python is very versatile with
assignments.
Different ways of using assignments –
1 – Assigning same value to multiple
variables
You can assign same values to
multiple variables in a single statement,
e.g., a=b=c=10
a=b=c=10
it will assign value 10 to all three
variables a,b,c that is, all three labels a,b,c will refer to
same location with value 10
2 – Assigning multiple values to
multiple variables in a single statement,
you can even assign multiple values to
multiple variable in single line
e.g., x,y,z=10,20,30
it will assign the value order wise,
i.e., first variable given first value, second variable the second value and so
on.
e.g., -
x,y = 25,50
print(x,y)
it will print result as –
25 50
COMPONENTS OF PYTHON PROGAM
Reviewed by Learn Python
on
November 02, 2019
Rating:
No comments: