1️⃣ Check if a Number is Positive, Negative, or Zero
num = float(input("Enter a number: "))if num > 0: print("Positive number")elif num == 0: print("Zero")else: print("Negative number")
2️⃣ Check if a Number is Odd or Even
num = int(input("Enter a number: "))if num % 2 == 0: print("Even number")else: print("Odd number")
3️⃣ Check Leap Year
year = int(input("Enter a year: "))if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0): print("Leap Year")else: print("Not a Leap Year")
4️⃣ Check Prime Number
num = int(input("Enter a number: "))if num > 1: for i in range(2, num): if num % i == 0: print("Not Prime") break else: print("Prime Number")else: print("Not Prime")
5️⃣ Print All Prime Numbers in an Interval
lower = int(input("Enter lower range: "))upper = int(input("Enter upper range: "))for num in range(lower, upper + 1): if num > 1: for i in range(2, num): if num % i == 0: break else: print(num)
6️⃣ Find Factorial of a Number
num = int(input("Enter a number: "))factorial = 1if num < 0: print("No factorial for negative numbers")else: for i in range(1, num + 1): factorial *= i print("Factorial:", factorial)
7️⃣ Display Multiplication Table
num = int(input("Enter a number: "))for i in range(1, 11): print(f"{num} x {i} = {num * i}")
num = int(input("Enter a number: "))sum = 0temp = numn = len(str(num))while temp > 0: digit = temp % 10 sum += digit ** n temp //= 10if num == sum: print("Armstrong Number")else: print("Not Armstrong")
🔟 Find Armstrong Numbers in an Interval
lower = int(input("Enter lower range: "))upper = int(input("Enter upper range: "))for num in range(lower, upper + 1): n = len(str(num)) sum = 0 temp = num while temp > 0: digit = temp % 10 sum += digit ** n temp //= 10 if num == sum: print(num)
1️⃣1️⃣ Sum of Natural Numbers
n = int(input("Enter a number: "))sum = 0for i in range(1, n + 1): sum += iprint("Sum:", sum)
📙 Operations on Strings, Lists, Tuples, Arrays
✅ Create Lists/Tuples/Arrays and Access Elements
lst = [10, 20, 30, 40]tpl = (1, 2, 3, 4)print(lst[2]) # List indexprint(tpl[-1]) # Tuple negative index
def lcm(x, y): if x > y: greater = x else: greater = y while True: if (greater % x == 0) and (greater % y == 0): return greater greater += 1n1 = int(input("Enter first number: "))n2 = int(input("Enter second number: "))print("LCM:", lcm(n1, n2))
1️⃣3️⃣ Find HCF
def hcf(x, y): while(y): x, y = y, x % y return xn1 = int(input("Enter first number: "))n2 = int(input("Enter second number: "))print("HCF:", hcf(n1, n2))
1️⃣4️⃣ Convert Decimal to Binary, Octal, Hexadecimal
num = int(input("Enter a number: "))print("Binary:", bin(num))print("Octal:", oct(num))print("Hexadecimal:", hex(num))
1️⃣5️⃣ Find ASCII Value
ch = input("Enter a character: ")print("ASCII value:", ord(ch))
def recur_fibo(n): if n <= 1: return n else: return(recur_fibo(n-1) + recur_fibo(n-2))n_terms = int(input("How many terms? "))for i in range(n_terms): print(recur_fibo(i))
1️⃣9️⃣ Factorial Using Recursion
def recur_factorial(n): if n == 1: return 1 else: return n * recur_factorial(n - 1)num = int(input("Enter a number: "))print("Factorial:", recur_factorial(num))
2️⃣0️⃣ Get Class Name of Instance
class MyClass: passobj = MyClass()print(type(obj).__name__)
2️⃣1️⃣ Differentiate between type() and isinstance()
a = 5print(type(a))print(isinstance(a, int))
📕 File Operations & Exception Handling
✅ File Read/Write/Search
# Writewith open("test.txt", "w") as f: f.write("Hello, World!")# Readwith open("test.txt", "r") as f: print(f.read())# Searchwith open("test.txt", "r") as f: content = f.read() if "World" in content: print("Found")
✅ Exception Handling Example
try: num = int(input("Enter number: ")) print(10 / num)except ZeroDivisionError: print("Cannot divide by zero!")except ValueError: print("Invalid input!")finally: print("Done.")