Q1.
Object-Oriented Concepts
Class – A blueprint for creating objects.
Object – An instance of a class.
Encapsulation – Wrapping variables and methods together, restricting direct access.
Inheritance – One class can use properties/methods of another.
Polymorphism – Same function behaves differently in different contexts.
Features of OOP: Modularity, Reusability, Encapsulation, Inheritance, Polymorphism, Abstraction.
Q2. Features of Python
1. Simple and easy to learn
2. Open-source and free
3. Interpreted language
4. Platform independent
5. Dynamically typed
6. Rich library support
7. Supports OOP & functional programming
8. Extensible and embeddable
9. Automatic memory management
10. High-level language
Q3. Python Datatypes
Numeric: int, float, complex
Sequence: list, tuple, range
String: str
Mapping: dict
Set types: set, frozenset
Boolean: bool
Example:
x = 10 # int
y = 3.14 # float
z = "Hello" # str
a = [1,2,3] # list
b = (4,5,6) # tuple
c = {"id":101,"name":"John"} # dict
Q4. Variable
Variable is a name given to memory location storing data.
Declaration: Just assign a value.
Example:
x = 100
name = "Alice"
Q5. Output Formatting
print("Name:", "Alice")
print("My name is {}".format("Alice"))
age = 20
print(f"I am {age} years old")
Q6. Simple Interest Program
p = float(input("Enter principal: "))
r = float(input("Enter rate: "))
t = float(input("Enter time: "))
si = (p * r * t) / 100
if r > 10:
print("High Interest Rate")
else:
print("Simple Interest:", si)
Q7. Celsius to Fahrenheit
c = float(input("Enter temperature in Celsius: "))
f = (c * 9/5) + 32
if c < 0:
print("Below Freezing Point")
elif c > 100:
print("Above Boiling Point")
else:
print("Fahrenheit:", f)
Q8. Even or Odd
n = int(input("Enter a number: "))
if n % 2 == 0:
print("Even")
else:
print("Odd")
Q9. Positive, Negative or Zero
n = int(input("Enter a number: "))
if n > 0:
print("Positive")
elif n < 0:
print("Negative")
else:
print("Zero")
Q10. Largest of Three
a = int(input("Enter first: "))
b = int(input("Enter second: "))
c = int(input("Enter third: "))
print("Largest is:", max(a,b,c))
Q11. Day of the Week
d = int(input("Enter day number (1-7): "))
days = ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"]
if 1 <= d <= 7:
print(days[d-1])
else:
print("Invalid")
Q12. Employee Salary
name = input("Enter name: ")
emp_id = input("Enter emp-id: ")
dept = input("Enter department: ")
basic = float(input("Enter basic pay: "))
hra = 0.1 * basic
da = 0.3 * basic
salary = basic + hra + da
print("Name:", name)
print("Emp-ID:", emp_id)
print("Dept:", dept)
print("Salary:", salary)
Q13. Vowel or Consonant
ch = input("Enter a letter: ").lower()
if ch in ['a','e','i','o','u']:
print("Vowel")
else:
print("Consonant")
Q14. 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 Leap Year")
Q15. Calculator
a = float(input("Enter first: "))
b = float(input("Enter second: "))
op = input("Enter operator (+,-,*,/): ")
if op == '+': print(a+b)
elif op == '-': print(a-b)
elif op == '*': print(a*b)
elif op == '/': print(a/b)
else: print("Invalid operator")
Q16. Grade
s = int(input("Enter score: "))
if s >= 90: print("A")
elif s >= 80: print("B")
elif s >= 70: print("C")
elif s >= 60: print("D")
else: print("F")
Q17. Divisible by 5 and 11
n = int(input("Enter number: "))
if n % 5 == 0 and n % 11 == 0:
print("Divisible by both 5 and 11")
else:
print("Not divisible by both")
Q18. Number Game
n = int(input("Enter number: "))
if n == 99: print("CONGRATULATIONS")
elif n < 99: print("AIM HIGHER NUMBER")
else: print("AIM LOWER NUMBER")
Q19. Traffic Lights
signal = input("Enter signal: ").lower()
if signal == "green": print("Car can go")
elif signal == "red": print("Car must stop")
elif signal == "yellow": print("Car must wait")
else: print("Unrecognized signal")
Q20. Student Marks
m1 = int(input("Enter mark1: "))
m2 = int(input("Enter mark2: "))
m3 = int(input("Enter mark3: "))
m4 = int(input("Enter mark4: "))
total = m1+m2+m3+m4
agg = total/4
print("Total:", total, "Aggregate:", agg)
if agg >= 75: print("Distinction")
elif agg >= 60: print("First Class")
elif agg >= 50: print("Second Class")
elif agg >= 40: print("Pass")
else: print("Fail")
Q21. Prime Number
n = int(input("Enter number: "))
if n > 1:
for i in range(2,n):
if n % i == 0:
print("Not Prime")
break
else:
print("Prime")
else:
print("Not Prime")
Q22. Triangle Type
a = int(input("Enter side1: "))
b = int(input("Enter side2: "))
c = int(input("Enter side3: "))
if a==b==c: print("Equilateral")
elif a==b or b==c or a==c: print("Isosceles")
else: print("Scalene")
Q23. BMI
w = float(input("Enter weight (kg): "))
h = float(input("Enter height (m): "))
bmi = w / (h*h)
print("BMI:", bmi)
if bmi < 18.5: print("Underweight")
elif bmi < 24.9: print("Normal weight")
elif bmi < 29.9: print("Overweight")
else: print("Obesity")
Q24. Electricity Bill
units = int(input("Enter units: "))
bill = 0
if units <= 100:
bill = units * 0.5
elif units <= 200:
bill = 100*0.5 + (units-100)*0.75
else:
bill = 100*0.5 + 100*0.75 + (units-200)*1.2
print("Bill = $", bill)
Q25. Discount
amt = float(input("Enter purchase amount: "))
if amt > 1000:
discount = 0.1 * amt
elif amt >= 500:
discount = 0.05 * amt
else:
discount = 0
final = amt - discount
print("Final Amount:", final)
Q26. Age Eligibility
age = int(input("Enter age: "))
if age < 16: print("Too young for a driver’s license")
elif age < 18: print("Eligible for learner’s permit")
elif age >= 18: print("Eligible for driver’s license")
if age >= 21: print("Eligible to vote")
Q27. Shopping Cart
p1 = float(input("Price of item1: "))
q1 = int(input("Qty of item1: "))
p2 = float(input("Price of item2: "))
q2 = int(input("Qty of item2: "))
p3 = float(input("Price of item3: "))
q3 = int(input("Qty of item3: "))
subtotal = (p1*q1)+(p2*q2)+(p3*q3)
discount = 0
if subtotal > 100:
discount = 0.1 * subtotal
after_discount = subtotal - discount
tax = 0.08 * after_discount
total = after_discount + tax
print("Subtotal:", subtotal)
print("Discount:", discount)
print("Tax:", tax)
print("Final Total:", total)