Python Assignment 1
Python Assignment 1
ii. Subtraction(-) - Subtracts right hand operand from left hand operand.
Example : a – b = 10
v. Modulus(%) - Divides left hand operand by right hand operand and returns
remainder
Example : a % b = 0
vii. Floor Division(//) - The division of operands where the result is the quotient
in which the digits after the decimal point are removed. But if one of the
operands is negative, the result is floored, i.e., rounded away from zero
(towards negative infinity).
Example : 9//2 = 4 and 9.0//2.0 = 4.0, -11//3 = -4, -11.0//3 = -4.0
1. c) What are the role of indentation in python? Provide example to support your answer.
Ans : Many a times it is required to treat more than one statements in a program as a block.
Different programming languages use different techniques to define scope and extent of
block of statements in constructs like class, function, conditional and loop. In C and C++ for
example, statements inside curly brackets are treated as a block. Python uses uniform
indentation to mark block of statements.
Before beginning of block symbol : is used. First and subsequent statements in block are
written by leaving additional (but uniform) whitespace (called indent) . In order to signal end
of block, the whitespace is deducted.
For Example :
if(age==18):
elif(age>18):
print(‘allowed to drive')
else:
area = length*width
print("The area is",area)
Output :
2.b) Write a python program to swap two numbers without temporary variable.
Ans :
A = int(input("Enter value for A : "))
B = int(input("Enter value for B : "))
print("Before swaping : ",A,B)
A = A + B
B = A - B
A = A - B
print("After swaping : ",A,B)
Output :
2.c) Explain if-elif condition. When do we use it? Explain with example.
Ans :
The elif is short for else if. It allows us to check for multiple expressions.If the
condition for if is False, it checks the condition of the next elif block and so on.If all
the conditions are False, the body of else is executed.Only one block among the
several if...elif...else blocks is executed according to the condition.The if block can
have only one else block. But it can have multiple elif blocks.
The elif statement enables us to check multiple conditions and execute the specific
block of statements depending upon the true condition among them. We can have
any number of elif statements in our program depending upon our need. However,
using elif is optional.The elif statement works like an if-else-if ladder statement in C.
It must be succeeded by an if statement.
Example :
a=int(input("enter first number\n"))
b=int(input("enter second number\n"))
c=int(input("enter third number\n"))
if(a>b and a>c):
print(a,"is greatest")
elif(b>a and b>c):
print(b,"is greatest")
else:
print(c,"is greatest")
Output