The document contains a series of Python programming exercises including finding the largest of three numbers, displaying prime numbers within an interval, swapping two numbers without a temporary variable, adding and multiplying complex numbers, and printing a multiplication table. It also demonstrates various operators in Python such as arithmetic, relational, assignment, logical, bitwise, ternary, membership, and identity operators with examples. Each program is presented with user input prompts and expected outputs.
Download as TXT, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
13 views
Week1-Python
The document contains a series of Python programming exercises including finding the largest of three numbers, displaying prime numbers within an interval, swapping two numbers without a temporary variable, adding and multiplying complex numbers, and printing a multiplication table. It also demonstrates various operators in Python such as arithmetic, relational, assignment, logical, bitwise, ternary, membership, and identity operators with examples. Each program is presented with user input prompts and expected outputs.
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3
1. Write a program to find the largest element among three Numbers.
a=int(input ("Enter the a Value: "))
b=int(input ("Enter the b Value: ")) c=int(input ("Enter the c Value: "))
if a >= b and a >= c:
print("a is big",a) elif b >= a and b >= c: print("b is big",b) else: print("c is big",c)
2. Write a Program to display all prime numbers within an interval
s = int(input ("Enter the start Value: "))
e = int(input ("Enter the end Value: ")) for number in range (s, e+1): if number > 1: for i in range (2, number): if (number % i) == 0: break else: print (number)
3. Write a program to swap two numbers without using a temporary variable.
a=int(input("Enter value of first variable: "))
b=int(input("Enter value of second variable: ")) print("before swapping a is:",a," and b is:",b) a=a+b b=a-b a=a-b print("after swapping a is:",a," and b is:",b)
4. Write a program to add and multiply complex numbers
a=complex(input("Enter value of first variable: "))
b=complex(input("Enter value of second variable: ")) add=a+b mul=a*b print("addition of two complex numbers are",add) print("Multiplication of two complex numbers are",mul)
5. Write a program to print multiplication table of a given number
n=int(input("Enter a TableNumber: "))
e=int(input("Enter a TableNumber Ending Number: ")) for i in range(1, e+1): print(n, 'x', i, '=', n*i)
6. Demonstrate the following Operators in Python with suitable examples
i) Arithmetic Operators a=int(input ("Enter the a Value: ")) b=int(input ("Enter the b Value: ")) print ('Sum: ', a + b) print ('Subtraction: ', a - b) print ('Multiplication: ', a * b) print ('Division: ', a / b) print ('Floor Division: ', a // b) print ('Modulo: ', a % b) print ('Power: ', a ** b) ii) Relational Operators a=int(input ("Enter the a Value: ")) b=int(input ("Enter the b Value: ")) print(a == b) print(a != b) print(a < b) print(a <= b) print(a > b) print(a >= b)
iii) Assignment Operatorsiv
a=int(input ("Enter the a Value: ")) a+=1 print(a) a -=3 print(a) a *= 4 print(a) a /= 3 print(a) a %= 10 print(a) a **= 10 print(a)
iv) Logical Operators
a=int(input ("Enter the a Value: "))
b=int(input ("Enter the b Value: ")) print((a > b) and (b >= a)) print((a > b) or (b >= a)) print( not (a > b))
v) Bit wise Operators
x = int(input("Enter the value of x: ")) y = int(input("Enter the value of y: ")) print("x & y =", x & y) print("x | y =", x | y) print("~y =", ~ y) print("x ^ y =", x ^ y) print("x >> 1 =", x >> 3) print("y >> 1 =", y >> 3) print("x << 1 =", x << 1) print("y << 1 =", y << 1)
vi) Ternary Operator
x = int(input("Enter the value of x: ")) # Taking user input for x y = int(input("Enter the value of y: ")) # Taking user input for y print("x" if x> y else "y")
vii) Membership Operators
print(25 in {0, 15, 25})
print('z' not in 'Hello') viii) Identity Operators
a = [1, 2, 3, 4, 5] b = [1, 2, 3, 4, 5] c = a
# Comparing and printing return values
a = [1, 2, 3, 4, 5] b = [1, 2, 3, 4, 5] c = a
# Comparing and printing return values
print(a is c) print(a is b) print(a is not c) print(a is not b)