0% found this document useful (0 votes)
10 views

XI-IP-Python Fundamentals-Module - III-Jun-14-2021

This document summarizes key concepts about variables and input/output in Python from Module III. It discusses defining variables, assigning values, data types, dynamic typing, taking user input, and basic output statements like print. Examples are provided to illustrate concepts like multiple assignment, evaluating expressions, and type conversion when reading numeric values. Assignment questions at the end assess understanding of variables, input/output, and simple programs.

Uploaded by

Raju Upadhyay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

XI-IP-Python Fundamentals-Module - III-Jun-14-2021

This document summarizes key concepts about variables and input/output in Python from Module III. It discusses defining variables, assigning values, data types, dynamic typing, taking user input, and basic output statements like print. Examples are provided to illustrate concepts like multiple assignment, evaluating expressions, and type conversion when reading numeric values. Assignment questions at the end assess understanding of variables, input/output, and simple programs.

Uploaded by

Raju Upadhyay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Chapter 2 -Python Fundamentals-Module-III

Module III- Variables and Assignments, Simple Input and Output

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.

Lvalues and Rvalues


lvalue: Expression that comes on the lhs(left hand side) of an assignment. Lvalues are
the objects to which you can assign values.
rvalue: are the literals and expressions that are assigned to lvalues. Rvalues comes on
the rhs(right hand side) of an assignment.
a=10
b=20
but this is wrong:
100=a
20=b

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.

2. Assigning multiple values to multiple variables


x,y,z=10,20,30

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

If you want to swap values of x and y:


x,y=y,x
print(x,y)
Now the result will be:

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

Statement 1 assigns 5,10, 7 to a, b, c respectively.


Statemnt 2 will first evaluate RHS as a+1, b+2, c-1.
5+1, 10+2, 7-1=6, 12, 6
Then it will make the statement:
b,c,a=6,12,6

Thus b=6 , c=12 and a=6

Statemnt 3 will print :


6,6,12

Find out the output:


p,q=3 ,5
q, r=p-2, p+2
print(p,q,r)
Page 3 of 9
Example to evaluate from RHS and LHS
x=10
y,y=x + 2, x + 5
will evaluate to following(after the RHS of = operator)
y, y= 12, 15
Firstly assign first RHS to first LHS
Y=12

Y=15

Find out the output:


x,x= 20, 30
y, y= x+ 10, x+ 20
print(x,y)

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

A variable pointing to a value of a certain type, can be made to point to a value /


object of different type. This is called Dynamic Typing.
Data type of a variable depend/change upon the value assigned to a variable on each
next statement.

X = 25 # integer type
print(x)
X = “python” # x variable data type change to string on just next line
print(x)

Now programmer should be aware that not to write like this:

CAUTION WITH DYNAMIC TYPING

Y = X / 5 # error !! String cannot be divided.

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’>

Simple Input and Output statement:


To get input from user interactively you can use built in function input ( ).
Syntax is:
<variable to hold the value>=input(<prompt to be displayed>)

Nam1=input(“what is your name”)

Note: The input () function always return a value of string type.


So:
Name=input(“Enter your name:- “)
age=input(“Enter your age ? ”)

Output:
Enter your age ? 10
age
‘10’
type(age)
str

age+1
will give an error ,because you can not add .

This type of error is called TypeError.

Reading numbers:
age=(“enter age?”)
age=int( age)
enter age? 16

age+ 2
18
Check by typing:
type(age)
int

marks=float (marks)

per=float(“enter your percentage”)

Page 6 of 9
Possible Errors when reading numeric values
age=int(input(“enter your age”))
17.5

Output through print statement


print(“I\’m”, 12+5,”years old”)

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”)

Will print the output as:


Class XI IP #Delhi Public School

Find out the output:


a,b= 20,30
print(“a =”,a, end=’ ‘)
print(“b =”,b)
Output will be:
a=20 b=30 the space between 20 and b is because end=’ ‘

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?

3.What is Dynamic Typing feature of Python ?

4.What would the following code do :


X= Y= 7 ?

5.What is the error in following code:


X, Y = 7?

6. Following variable definition is creating problem X = 0281 , find reasons.

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 .

10.Write a program that generates the following out put:


5
10
9
11.Assign value 5 to a variable using assignment operator (=) multiply it with 2 to
generate 10 and subtract 1 to generate 9.

12.Modify the above program so as to print output as 5@10@9.

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).

17.Write a program to read a number n and print n2 , n3 and n4 .

18.Write a program to find the area of triangle .


Page 8 of 9
19.Write a program to compute simple interest and compound interest .

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

You might also like