1.
Python Program to Find the Largest Among Three Numbers
# Python program to find the largest number among the three input
numbers
# change the values of num1, num2 and num3
# for a different result
num1 = 10
num2 = 14
num3 = 12
# uncomment following lines to take three numbers from user
#num1 = float(input("Enter first number: "))
#num2 = float(input("Enter second number: "))
#num3 = float(input("Enter third number: "))
if (num1 >= num2) and (num1 >= num3):
largest = num1
elif (num2 >= num1) and (num2 >= num3):
largest = num2
else:
largest = num3
print("The largest number is", largest)
Output
The largest number is 14
2. Write a program to display all prime numbers within an interval
# Python program to display all the prime numbers within an interval
lower = 900
upper = 1000
print("Prime numbers between", lower, "and", upper, "are:")
for num in range(lower, upper + 1):
# all prime numbers are greater than 1
if num > 1:
for i in range(2, num):
if (num % i) == 0:
break
else:
print(num)
Run Code
Output
Prime numbers between 900 and 1000 are:
907
911
919
929
937
941
947
953
967
971
977
983
991
997
3. Write a program to swap two numbers without using a temporary variable.
# Python program to swap two variables
x = 5
y = 10
# To take inputs from the user
#x = input('Enter value of x: ')
#y = input('Enter value of y: ')
# create a temporary variable and swap the values
temp = x
x = y
y = temp
print('The value of x after swapping: {}'.format(x))
print('The value of y after swapping: {}'.format(y))
Output
The value of x after swapping: 10
The value of y after swapping: 5
1. 4. Demonstrate the following Operators in Python with suitable examples.
i) Arithmetic Operators ii) Relational Operators iii) Assignment Operators
iv) Logical Operators v) Bit wise Operators vi) Ternary Operator
vii) Membership Operators viii) Identity Operators
Example 1: Arithmetic Operators in Python
a = 7
b = 2
# addition
print ('Sum: ', a + b)
# subtraction
print ('Subtraction: ', a - b)
# multiplication
print ('Multiplication: ', a * b)
# division
print ('Division: ', a / b)
# floor division
print ('Floor Division: ', a // b)
# modulo
print ('Modulo: ', a % b)
# a to the power b
print ('Power: ', a ** b)
Run Code
Output
Sum: 9
Subtraction: 5
Multiplication: 14
Division: 3.5
Floor Division: 3
Modulo: 1
Power: 49
2. Python Assignment Operators
# assign 5 to x
x = 5
Example 2: Assignment Operators
# assign 10 to a
a = 10
# assign 5 to b
b = 5
# assign the sum of a and b to a
a += b # a = a + b
print(a)
# Output: 15
3. Python Relational Operators
a = 5
b = 2
print (a > b) # True
Example 3: Comparison Operators
a = 5
b = 2
# equal to operator
print('a == b =', a == b)
# not equal to operator
print('a != b =', a != b)
# greater than operator
print('a > b =', a > b)
# less than operator
print('a < b =', a < b)
# greater than or equal to operator
print('a >= b =', a >= b)
# less than or equal to operator
print('a <= b =', a <= b)
Output
a == b = False
a != b = True
a > b = True
a < b = False
a >= b = True
a <= b = False
4. Python Logical Operators
a = 5
b = 6
print((a > 2) and (b >= 6)) # True
Example 4: Logical Operators
# logical AND
print(True and True) # True
print(True and False) # False
# logical OR
print(True or False) # True
# logical NOT
print(not True) # False
5. Python Bitwise operators
x = 10 //0000 1010 in binary
y = 4 //0000 0100 in binary
print(x & y ) //output=0(0000 0000)
print(x | y )//output=14(0000 1110)
print(~x )//output=-11 ( 1111 0101 )
print(x ^ y )//output= 14 ( 0000 1110 )
print(x >> 2)//output= 2 ( 0000 0010 )
7.Membership operators
message = 'Hello world'
dict1 = {1:'a', 2:'b'}
# check if 'H' is present in message string
print('H' in message) # prints True
# check if 'hello' is present in message string
print('hello' not in message) # prints True
# check if '1' key is present in dict1
print(1 in dict1) # prints True
# check if 'a' key is present in dict1
print('a' in dict1) # prints False
Output
True
True
True
False
Membership operators
message = 'Hello world'
dict1 = {1:'a', 2:'b'}
# check if 'H' is present in message string
print('H' in message) # prints True
# check if 'hello' is present in message string
print('hello' not in message) # prints True
# check if '1' key is present in dict1
print(1 in dict1) # prints True
# check if 'a' key is present in dict1
print('a' in dict1) # prints False
Example 4: Identity operators in Python
x1 = 5
y1 = 5
x2 = 'Hello'
y2 = 'Hello'
x3 = [1,2,3]
y3 = [1,2,3]
print(x1 is not y1) # prints False
print(x2 is y2) # prints True
print(x3 is y3) # prints False