XI-IP-Python Fundamentals-Module - III-Jun-14-2021
XI-IP-Python Fundamentals-Module - III-Jun-14-2021
Variable:
A names labels, whose values can be used and processed during program run, are
called variables. Also called as symbolic variables.
Marks=90
Creating a variable
Python assign value of desired type to them, for example:
To create a numeric variable assign numeric value etc.
Student=’ABC’
Rno=12
Python internally create labels referring to these values.
bal=2345.56
train_no=’A-N098’
Variable are not storage containers in Python. Most programming languages create
variables as storage containers.
age=9
age=20
Value 9 is assigned to variable age and then value 20 is assigned to it.
Page 1 of 9
Thus variable in python does not have fixed locations unlike other programming
language. The location they refer to changes every time their values change.
The literal or the expressions that evaluate a value can’t come on lhs of an assignment
hence they are rvalues but variable names can come on lhs of an assignment , they are
lvalues.
Multiple assignment:
1. Assigning same value to multiple variables
a=b=c=10
Page 2 of 9
Assigning same value to multiple variables in a single statement.
It will assign the values orderwise. This style of assigning value is very compact
and useful.
For example:
x,y=25,50
print(x,y)
It will print the result as:
25 50
50 25
While assigning values through multiple assignments, Python first evaluates the RHS
and then LHS.
For Example:
a, b, c = 5,10, 7 # statement 1
b, c, a=a+1, b+2, c-1 # statement 2
print(a,b,c) #statement 3
Y=15
Output: 30 50
a,a=5,15
Variable Definition:
Variable is a name given to a memory location. A variable can consider as a
container which holds value.
Python is a type infer language (automatic detection of the type of an expression in a
formal language) that means you don't need to specify the datatype of variable.
Python automatically get variable datatype depending upon the value assigned to
the variable.
Assigning Values To Variable:
name = ‘python' # String Data Type
sum = None # a variable without value
a = 23 # Integer
b = 6.2 # Float
sum = a + b
print(sum)
Note: A variable in python is defined only when you assign some value to it. Using an
undefined variable in an expression /statement causes an error called NameError.-
Name <’variable’> not defined
Like:
print(x)
x=20
print(x)
To correct the above code, we can type:
Page 4 of 9
X=0
print(x)
X=20
print(x)
Dynamic typing
X = 25 # integer type
print(x)
X = “python” # x variable data type change to string on just next line
print(x)
Dynamic typing is different from static typing ,in static typing a data type is attached
with a variable when it is defined first and it is fixed. That is data type of a variable can
not be changed in static typing whereas there is no such restriction in dynamic typing .
float x;
If you want to determine the type of a variable and what type of value does it point to?
Use type(<<var.name>> )
For Example:
a=20
type(a)
<class ‘int’>
a=20.5
type(b)
<class ‘float’>
Page 5 of 9
a=’hello’
<class’str’>
Output:
Enter your age ? 10
age
‘10’
type(age)
str
age+1
will give an error ,because you can not add .
Reading numbers:
age=(“enter age?”)
age=int( age)
enter age? 16
age+ 2
18
Check by typing:
type(age)
int
marks=float (marks)
Page 6 of 9
Possible Errors when reading numeric values
age=int(input(“enter your age”))
17.5
If explicitly give an end argument with a print( ) function then the print( ) print the line
and end with the string specified with the end argument.
For Example:
print(“ Class XI IP”, end=’#’)
print(“Delhi Public School”)
Q.
name1=’ABCD’
print(“Hello’”, end=’ ‘)
print(name)
print(“hope you are liking python”)
Output:
Hello ABCD
hope you are liking python
In python any statement can break by putting a \ at the end and pressing enter key.
print(“hello”,\
end=’ ‘) The backslash at the end means that
the statement is still continuing
Assignment Questions:
1. What are variables? How are they important for a program?
Page 7 of 9
2.What do you understand by undefined variable in Python?
7."Comments are useful and easy way to enhance readability and understandability of a
program.” Elaborate with examples.
8.Write a program that display a Quote, but display the main line only when the user
presses enter key .
9.Write a program to read today `s date from user. Then display how many days are
left in the current month .
13.Write the program with a maximum three line of code and that assign first five
multiples of number to 5 variable and then` print them .
14.Write the python program that accept radius of a circle and print its area .
15.Write the python program that accept marks in 5 subjects and output average marks.
16.Write a short program that asks for your height in centimeter and then converts your
height in feet and inches.(1 foot = 12 inches , 1 inch = 2.54 cm).
20.Write a program to input a number and print its first five multiples .
21.Write a program to read details like name class age of student and then print the
details firstly in same line and then in separate line .
Make sure to have two blank lines in these two different types of prints .
Page 9 of 9