CBSE 2026
Important CS Programs
Master your practical exams with our curated list of essential Python and SQL programs for Class 11. Click on any question to view the code, copy it, or run it instantly.
Python 3
# Accept input from user
name = input("Enter your name: ")
# Display output
print("Welcome to Python Programming,", name)
Python 3
# Explicit type conversion from string to int
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
result = num1 + num2
print("The sum is:", result)
Python 3
p = float(input("Enter Principal amount: "))
r = float(input("Enter Rate of interest: "))
t = float(input("Enter Time in years: "))
# Formula for Simple Interest
si = (p * r * t) / 100
print("Simple Interest:", si)
Python 3
l = float(input("Enter Length: "))
b = float(input("Enter Breadth: "))
area = l * b
print("Area of Rectangle:", area)
Python 3
a = 10
b = 20
print(f"Before: a={a}, b={b}")
temp = a
a = b
b = temp
print(f"After: a={a}, b={b}")
Python 3
a = 5
b = 10
# Tuple unpacking method
a, b = b, a
print("Swapped values:", a, b)
Python 3
c = float(input("Enter temp in Celsius: "))
# Operator precedence: multiplication/division happens before addition
f = (c * 9/5) + 32
print("Temperature in Fahrenheit:", f)
Python 3
# This code will fail to run
# print("Hello World" <-- Missing closing parenthesis
print("To fix: Ensure all parentheses are closed.")
Python 3
try:
num = 10
den = 0
print(num / den)
except ZeroDivisionError:
print("Runtime Error: Cannot divide by zero.")
Python 3
days = int(input("Enter number of days: "))
seconds = days * 24 * 60 * 60
print(f"{days} days = {seconds} seconds")
Python 3
num = int(input("Enter a number: "))
if num % 2 == 0:
print("Even Number")
else:
print("Odd Number")
Python 3
x = int(input("First: "))
y = int(input("Second: "))
z = int(input("Third: "))
if x >= y and x >= z:
print("Largest is:", x)
elif y >= x and y >= z:
print("Largest is:", y)
else:
print("Largest is:", z)
Python 3
age = int(input("Enter your age: "))
if age >= 18:
print("Eligible to vote.")
else:
print("Not eligible to vote.")
Python 3
year = int(input("Enter year: "))
# Leap year logic: Divisible by 400 OR (Divisible by 4 AND NOT by 100)
if (year % 400 == 0) or (year % 4 == 0 and year % 100 != 0):
print("Leap Year")
else:
print("Not a Leap Year")
Python 3
ch = input("Enter a character: ")
if ch.isupper():
print("Uppercase Letter")
elif ch.islower():
print("Lowercase Letter")
elif ch.isdigit():
print("Digit")
else:
print("Special Character")
Python 3
marks = float(input("Enter marks: "))
if marks >= 90:
print("Grade A")
elif marks >= 75:
print("Grade B")
elif marks >= 50:
print("Grade C")
else:
print("Grade F")
Python 3
op = input("Enter operator (+, -, *, /): ")
a = float(input("Num 1: "))
b = float(input("Num 2: "))
if op == '+':
print(a + b)
elif op == '-':
print(a - b)
elif op == '*':
print(a * b)
elif op == '/':
if b != 0:
print(a / b)
else:
print("Cannot divide by zero")
else:
print("Invalid operator")
Python 3
n = int(input("Enter number: "))
if n < 0:
print("Absolute value:", -n)
else:
print("Absolute value:", n)
Python 3
n = int(input("Enter number: "))
if n % 5 == 0 and n % 11 == 0:
print("Divisible by both")
else:
print("Not divisible by both")
Python 3
a = int(input("Angle 1: "))
b = int(input("Angle 2: "))
c = int(input("Angle 3: "))
# Sum of angles must be 180
if a + b + c == 180:
print("Valid Triangle")
else:
print("Invalid Triangle")
Python 3
for i in range(1, 11):
print(i, end=" ")
Python 3
n = int(input("Enter N: "))
sum_val = 0
i = 1
while i <= n:
sum_val += i
i += 1
print("Sum:", sum_val)
Python 3
num = int(input("Enter number: "))
fact = 1
for i in range(1, num + 1):
fact *= i
print("Factorial:", fact)
Python 3
n = int(input("Enter number: "))
for i in range(1, 11):
print(f"{n} x {i} = {n*i}")
Python 3
n = int(input("How many terms? "))
a, b = 0, 1
print("Fibonacci sequence:")
for i in range(n):
print(a, end=" ")
a, b = b, a + b
Python 3
num = int(input("Enter number: "))
is_prime = True
if num > 1:
for i in range(2, int(num/2)+1):
if num % i == 0:
is_prime = False
break
else:
is_prime = False
if is_prime:
print("Prime Number")
else:
print("Not Prime")
Python 3
num = int(input("Enter number: "))
rev = 0
while num > 0:
digit = num % 10
rev = rev * 10 + digit
num //= 10
print("Reversed:", rev)
Python 3
num = int(input("Enter number: "))
total = 0
while num > 0:
total += num % 10
num //= 10
print("Sum of digits:", total)
Python 3
num = int(input("Enter number: "))
temp = num
sum_cubes = 0
while temp > 0:
digit = temp % 10
sum_cubes += digit ** 3
temp //= 10
if num == sum_cubes:
print("Armstrong Number")
else:
print("Not Armstrong")
Python 3
num = int(input("Enter number: "))
original = num
rev = 0
while num > 0:
rev = rev * 10 + (num % 10)
num //= 10
if original == rev:
print("Palindrome Number")
else:
print("Not Palindrome")
Python 3
rows = 5
for i in range(1, rows+1):
for j in range(i):
print("*", end="")
print() # Newline
Python 3
rows = 5
for i in range(rows, 0, -1):
for j in range(i):
print("*", end="")
print()
Python 3
rows = 4
for i in range(1, rows + 1):
# Print leading spaces
print(" " * (rows - i), end="")
# Print numbers
for j in range(1, i + 1):
print(j, end=" ")
print()
Python 3
n = int(input("Enter N: "))
s = 0.0
for i in range(1, n+1):
s += 1/i
print("Sum of series:", s)
Python 3
x = int(input("Enter x: "))
n = int(input("Enter n: "))
s = 0
for i in range(1, n+1):
s += x**i
print("Result:", s)
Python 3
for i in range(1, 11):
if i == 5:
continue # Skip this iteration
print(i, end=" ")
Python 3
target = 5
for i in range(1, 11):
if i == target:
print("Found", target)
break # Exit loop completely
Python 3
a = int(input("Num 1: "))
b = int(input("Num 2: "))
while b:
a, b = b, a % b
print("GCD is:", a)
Python 3
x = int(input("Num 1: "))
y = int(input("Num 2: "))
if x > y:
greater = x
else:
greater = y
while True:
if (greater % x == 0) and (greater % y == 0):
lcm = greater
break
greater += 1
print("LCM is:", lcm)
Python 3
count = 0
while True:
ch = input("Enter char (or 'quit' to exit): ")
if ch == 'quit':
break
if ch in 'aeiouAEIOU':
count += 1
print("Total vowels entered:", count)
Python 3
s = "Python"
for char in s:
print(char)
Python 3
text = input("Enter string: ")
rev_text = text[::-1]
print("Reversed:", rev_text)
Python 3
s = input("Enter string: ")
# Compare with reverse
if s == s[::-1]:
print("Palindrome")
else:
print("Not Palindrome")
Python 3
s = input("Enter string: ").lower()
v = 0
c = 0
for char in s:
if char.isalpha():
if char in "aeiou":
v += 1
else:
c += 1
print(f"Vowels: {v}, Consonants: {c}")
Python 3
text = "Hello World"
print(text.upper())
print(text.lower())
Python 3
s = "python programming is fun"
print("Capitalize:", s.capitalize()) # First char only
print("Title:", s.title()) # First char of every word
Python 3
s = "Computer Science"
print("Found 'Sci' at:", s.find("Sci"))
print("Index of 'o':", s.index("o"))
Python 3
s = "Python3"
print("Is Alpha?", s.isalpha())
print("Is Digit?", s.isdigit())
print("Is Alnum?", s.isalnum())
Python 3
s = "banana"
print("Number of 'a':", s.count("a"))
Python 3
text = "I like Java"
new_text = text.replace("Java", "Python")
print(new_text)
Python 3
s = "Learn Python Today"
words = s.split() # Splits by space
print("List:", words)
joined = "-".join(words)
print("Joined:", joined)
Python 3
s = " hello "
print(f"Original: '{s}'")
print(f"Stripped: '{s.strip()}'")
print(f"LStrip: '{s.lstrip()}'")
Python 3
filename = "report.pdf"
if filename.endswith(".pdf"):
print("It is a PDF file.")
if filename.startswith("report"):
print("It is a report.")
Python 3
s = "user@gmail.com"
parts = s.partition("@")
print(parts) # ('user', '@', 'gmail.com')
Python 3
s = "ABCDEFGHI"
print(s[::2]) # Step of 2
Python 3
fruits = ["Apple", "Banana", "Cherry"]
print(fruits)
print("First:", fruits[0])
print("Last:", fruits[-1])
Python 3
nums = [10, 20, 30, 40]
sum_val = 0
for n in nums:
sum_val += n
print("Sum:", sum_val)
Python 3
l = [1, 2]
l.append(3)
l.extend([4, 5])
print(l)
Python 3
l = [10, 30]
l.insert(1, 20) # Insert 20 at index 1
print(l)
l.remove(30) # Remove value 30
print(l)
Python 3
l = [10, 20, 30]
val = l.pop(1) # Remove index 1
print("Popped:", val)
l.clear()
print("List after clear:", l)
Python 3
nums = [5, 12, 1, 88, 4]
print("Max:", max(nums))
print("Min:", min(nums))
Python 3
data = [10, 50, 30, 70, 80, 20]
x = int(input("Search for: "))
found = False
for i in range(len(data)):
if data[i] == x:
print("Found at index", i)
found = True
break
if not found:
print("Not found")
Python 3
l = [1, 2, 1, 3, 1, 4]
print("1 appears:", l.count(1), "times")
Python 3
nums = [5, 2, 9, 1]
nums.sort()
print("Ascending:", nums)
nums.sort(reverse=True)
print("Descending:", nums)
Python 3
data = [10, 20, 30, 40, 50]
avg = sum(data) / len(data)
print("Mean:", avg)
Python 3
sq = [x**2 for x in range(1, 6)]
print(sq)
Python 3
matrix = [[1, 2], [3, 4]]
print("Element at 1,0:", matrix[1][0])
Python 3
nums = [1, 2, 3, 4, 5, 6]
evens = []
for n in nums:
if n % 2 == 0:
evens.append(n)
print("Evens:", evens)
Python 3
l = [1, 2, 3]
l.reverse()
print("Reversed:", l)
Python 3
l = [10, 20, 4, 45, 99]
l.sort()
print("Second largest:", l[-2])
Python 3
t = (1, 2, 3)
t_single = (1,) # Comma needed for single element
print(t, type(t))
print(t_single, type(t_single))
Python 3
t = (1, 2, 3)
try:
t[0] = 10
except TypeError:
print("Error: Tuples are immutable")
Python 3
t1 = (1, 2)
t2 = (3, 4)
print("Add:", t1 + t2)
print("Repeat:", t1 * 3)
Python 3
point = (10, 20)
x, y = point
print(f"X: {x}, Y: {y}")
Python 3
t = (10, 50, 20)
print("Length:", len(t))
print("Max:", max(t))
print("Min:", min(t))
Python 3
t = (0, 1, 2, 3, 4, 5)
print(t[2:5])
Python 3
t = ('a', 'b', 'c')
if 'b' in t:
print("Found b")
Python 3
l = [1, 2, 3]
t = tuple(l)
print("Converted to tuple:", t)
Python 3
t = (10, 20, 30, 20)
print("Index of 20:", t.index(20)) # Finds first occurrence
Python 3
t = (1, 2, 1, 1, 3)
print("Count of 1:", t.count(1))
Python 3
d = {"name": "Alice", "age": 16}
print("Name:", d["name"])
Python 3
d = {"a": 1}
d["b"] = 2 # Add new
d["a"] = 100 # Modify existing
print(d)
Python 3
d = {"Apple": 100, "Mango": 80}
for k in d:
print(k, "->", d[k])
Python 3
d = {"x": 10, "y": 20}
print(d.keys())
print(d.values())
print(d.items())
Python 3
s = "hello"
d = {}
for char in s:
if char in d:
d[char] += 1
else:
d[char] = 1
print(d)
Python 3
d = {"a": 1}
print(d.get("b", "Not Found"))
Python 3
emps = {"John": 50000, "Doe": 60000}
name = input("Enter name: ")
print("Salary:", emps.get(name, "Unknown Employee"))
Python 3
d = {"a": 1, "b": 2, "c": 3}
del d["a"]
val = d.pop("b")
print("Deleted value:", val)
print("Dict now:", d)
Python 3
keys = ['a', 'b', 'c']
d = dict.fromkeys(keys, 0)
print(d)
Python 3
d1 = {"a": 1}
d2 = {"b": 2}
d1.update(d2)
print("Updated d1:", d1)
Python 3
import math
r = 5
area = math.pi * r * r
print("Area:", area)
Python 3
import math
print("Square root of 16:", math.sqrt(16))
print("2 to power 3:", math.pow(2, 3))
Python 3
import math
n = 4.7
print("Ceil:", math.ceil(n)) # 5
print("Floor:", math.floor(n)) # 4
Python 3
import math
# math.sin takes radians
print("Sin 90 deg:", math.sin(math.radians(90)))
Python 3
import random
# Random integer between 1 and 6 inclusive
print("Dice roll:", random.randint(1, 6))
Python 3
import random
# Random even number between 10 and 20
print(random.randrange(10, 21, 2))
Python 3
import random
print("Random float:", random.random())
Python 3
import statistics
data = [10, 20, 30, 40, 50]
print("Mean:", statistics.mean(data))
Python 3
import statistics
data = [1, 5, 2, 9, 3]
# Median sorts list and picks middle
print("Median:", statistics.median(data))
Python 3
import statistics
data = [1, 2, 2, 3, 4, 2]
print("Mode:", statistics.mode(data))