Python Programs - Day 2 (With Output)
(b) Relational Operators
1. Largest among three numbers
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))
if a > b:
if a > c:
print("Largest number is:", a)
else:
print("Largest number is:", c)
else:
if b > c:
print("Largest number is:", b)
else:
print("Largest number is:", c)
Sample Output:
Enter first number: 10
Enter second number: 25
Enter third number: 15
Largest number is: 25
2. Check Odd or Even
num = int(input("Enter a number: "))
if num % 2 == 0:
print("Even number")
else:
print("Odd number")
Sample Output:
Enter a number: 7
Odd number
(c) Assignment Operators
1. Simple Interest
p = float(input("Enter Principal: "))
r = float(input("Enter Rate: "))
t = float(input("Enter Time: "))
si = p
si *= r
si *= t
si /= 100
print("Simple Interest =", si)
Sample Output:
Enter Principal: 1000
Enter Rate: 5
Enter Time: 2
Simple Interest = 100.0
2. Profit/Loss
cp = float(input("Enter Cost Price: "))
sp = float(input("Enter Selling Price: "))
if sp > cp:
profit = sp - cp
percent = (profit / cp) * 100
print("Profit =", profit)
print("Profit % =", percent)
else:
loss = cp - sp
percent = (loss / cp) * 100
print("Loss =", loss)
print("Loss % =", percent)
Sample Output:
Enter Cost Price: 500
Enter Selling Price: 600
Profit = 100
Profit % = 20.0
(d) Logical Operators
1. Leap Year
year = int(input("Enter year: "))
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print("Leap Year")
else:
print("Not a Leap Year")
Sample Output:
Enter year: 2024
Leap Year
2. Collinear Points
x1 = int(input("Enter x1: "))
y1 = int(input("Enter y1: "))
x2 = int(input("Enter x2: "))
y2 = int(input("Enter y2: "))
x3 = int(input("Enter x3: "))
y3 = int(input("Enter y3: "))
if (y2 - y1) * (x3 - x2) == (y3 - y2) * (x2 - x1):
print("Points are collinear")
else:
print("Points are not collinear")
Sample Output:
Enter x1: 1
Enter y1: 2
Enter x2: 2
Enter y2: 4
Enter x3: 3
Enter y3: 6
Points are collinear
(e) Bitwise Operator
Even/Odd using Bitwise AND
num = int(input("Enter a number: "))
if num & 1:
print("Odd number")
else:
print("Even number")
Sample Output:
Enter a number: 8
Even number
(f) Ternary Operator
1. Maximum of two numbers
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
max_num = a if a > b else b
print("Maximum number is:", max_num)
Sample Output:
Enter first number: 12
Enter second number: 20
Maximum number is: 20
2. Even/Odd using Ternary
num = int(input("Enter a number: "))
result = "Even" if num % 2 == 0 else "Odd"
print(result)
Sample Output:
Enter a number: 9
Odd