Python Lab Manual (22616) Practical 3 Answers

 Python Lab Manual (22616)Answers

Practical 3: Write Simple Python Program Using Operators  Arithmetic Operators ,Logical operators , Bitwise Operators 


 *Practical  Related Questions.

1)Mention the use of //, **, % operator in Python.
Ans=>  1) // - This is Arithmetic Operator  Use for Floor Division.
             2)**- This is Arithmetic Operator  Use for Calculate First Operand Power to Second Operand .
             3) %- This is Arithmetic Operator  it returns the reminder of division.

2)Describe ternary operator in Python
Ans=> 1) Ternary operators are also known as conditional expressions are operators that evaluate something based on a condition being true or false .
          2) Syntax : [on_true] if [expression] else [on_false] 
false
3) Describe about different Logical operators in Python with appropriate examples.
Ans => 1) Logical Operators: Logical operators perform Logical AND, Logical OR and Logical 
NOT operations. For logical operators following condition are applied.
             2)For AND operator – It returns TRUE if both the operands (right side and left side)  are true
             3) For OR operator- It returns TRUE if either of the operand (right side or left side) is true
             4) For NOT operator- returns TRUE if operand is false
             
a = True
b = False
  
print(a and b)

print(a or b)
  
print(not a)

Output :
 
  False
  True
  False

4) Describe about different Arithmetic operators in Python with appropriate 
examples.
Ans=>
OperationOperator
Example in Python Shell
Addition: Sum of two operands+
>>> x = 5; y = 6
>>> x + y
11
>>> import operator
>>> operator.add(5,6)
11
Subtraction: Left operand minus right operand-
>>> x = 10; y = 5
>>> x - y
5
>>> import operator
>>> operator.sub(10, 5)
5
Multiplication*
>>> x = 5; y = 6
>>> x * y
30
>>> import operator
>>> operator.mul(5,6)
30
Exponentiation: Left operand raised to the power of right**
>>> x = 2; y = 3
>>> x ** y
8
>>> import operator
>>> operator.pow(2, 3)
8
Division/
>>> x = 6; y = 3
>>> x / y
2
>>> import operator
>>> operator.truediv(6, 3)
2
Floor division: equivilant to math.floor(a/b)//
>>> x = 6; y = 5
>>> x // y
1
>>> import operator
>>> operator.floordiv(6,5)
1
Modulus: Reminder of a/b
%
>>> x = 11; y = 3
>>> x % y
12
>>> import operator
>>> operator.mod(11, 3)

5) Describe about different Bitwise operators in Python with appropriate examples.
Ans=>
           1) &(AND) : Set each bit to 1 if both bits are 1
           2)  | (OR)   : Set each bit to 1 if one of the two bits are 1
           3) ^ (XOR): Set each bit to 1 if one bit is 1
           4) ~ (NOT) : Inverts all bit
           5)<< (LEFT SHIFT) : The left operand's value is moved
           6) >> (Right shift): The left operand's value is moved right by the number of bits specified by the right operand


*Exercise 
1) Write a program to convert U.S. dollars to Indian rupees.
Ans=> 

us=float(input("enter us dollar="))
rs=us*75
print("indian Currency =",rs,"rupees")

  
2. Write a program to convert bits to Megabytes, Gigabytes and Terabytes
Ans=> 

bits = int(input("Enter the Bits : "))

byte = bits/8
megabyte = byte/1000000
gigabyte = megabyte/1000
terabyte = gigabyte/1000

print("MB = ", megabyte)
print("GB = ", gigabyte)
print("TB = ", terabyte)


3. Write a program to find the square root of a number
Ans=> 



number = int(input("Enter a number : "))
sqrt = number ** 0.5
print("Square root = ",sqrt)


4. Write a program to find the area of Rectangle
Ans=> 


l = float(input("Enter the length of Rectangle : "))
b = float(input("Enter the breadth of Rectangle : "))
Area = l*b
print("Area of Rectangle : ",Area)


5. Write a program to calculate area and perimeter of the square
Ans=> 

side = int(input("Enter the side : "))

Area = side*side
Perimeter = 4*side

print("Area of square : ",Area)
print("Perimeter of square : ",Perimeter)


6. Write a program to calculate surface volume and area of a cylinder.
Ans=> 

PI = 3.14

r = float(input("Enter Radius : "))
h = float(input("Enter Height : "))

surface_area = 2 * PI * r * ( r + h )
volume = PI * r * r * h

print("Surface Area = ",surface_area)
print("Volume = ",volume)



7. Write a program to swap the value of two variables
Ans=>
x = input("Enter first number : ")
y = input("Enter second number : ")

temp = x
x = y
y = temp

print("After swapping first number : ",x)
print("After swapping second number : ",y)



Post a Comment

0 Comments