Class 11 — Informatics Practices (IP): Script Mode Python
Programs (Explained & Detailed)
Generated on: 2025-10-12 13:44:41
1. Basic Input and Output
1. Print name, class, and school
Explanation: Prints fixed student details.
print('Name: Student')
print('Class: 11')
print('School: Your School Name')
Sample Output:
Name: Student
Class: 11
School: Your School Name
2. Two numbers: sum, difference, product, quotient
Explanation: Takes two numbers from input and shows arithmetic results.
a = float(input('Enter first number: '))
b = float(input('Enter second number: '))
print('Sum =', a+b)
print('Difference =', a-b)
print('Product =', a*b)
if b!=0:
print('Quotient =', a/b)
else:
print('Cannot divide by zero')
Sample Output:
Enter first number: 10
Enter second number: 5
Sum = 15.0
Difference = 5.0
Product = 50.0
Quotient = 2.0
3. Area and perimeter of rectangle
Explanation: Inputs length and breadth, computes area and perimeter.
l = float(input('Length: '))
b = float(input('Breadth: '))
area = l*b
peri = 2*(l+b)
print('Area =', area)
print('Perimeter =', peri)
Sample Output:
Length: 5
Breadth: 3
Area = 15.0
Perimeter = 16.0
4. Area and circumference of circle
Explanation: Uses [Link] for accuracy.
import math
r = float(input('Radius: '))
area = [Link] * r * r
circ = 2 * [Link] * r
print('Area =', round(area,2))
print('Circumference =', round(circ,2))
Sample Output:
Radius: 3
Area = 28.27
Circumference = 18.85
5. Simple and Compound Interest
Explanation: Compute simple and compound interest for given principal, rate, time.
P = float(input('Principal: '))
r = float(input('Rate (annual %): '))/100
t = float(input('Time (years): '))
SI = P * r * t
CI = P * ((1+r)**t - 1)
print('Simple Interest =', round(SI,2))
print('Compound Interest =', round(CI,2))
Sample Output:
Principal: 1000
Rate (annual %): 5
Time (years): 2
Simple Interest = 100.0
Compound Interest = 102.5
6. Temperature conversion (C <-> F)
Explanation: Convert between Celsius and Fahrenheit.
c = float(input('Celsius: '))
f = (c * 9/5) + 32
print(f'{c} °C = {round(f,2)} °F')
# To convert F->C: c = (f-32)*5/9
Sample Output:
Celsius: 25
25.0 °C = 77.0 °F
7. Swap two numbers using a third variable
Explanation: Classic swap using temporary variable.
a = input('a: ')
b = input('b: ')
print('Before swap:', a, b)
temp = a
a = b
b = temp
print('After swap:', a, b)
Sample Output:
a: 2
b: 3
Before swap: 2 3
After swap: 3 2
8. Swap two numbers without third variable
Explanation: Swap using tuple unpacking in Python.
a = input('a: ')
b = input('b: ')
print('Before swap:', a, b)
a, b = b, a
print('After swap:', a, b)
Sample Output:
a: 2
b: 3
Before swap: 2 3
After swap: 3 2
2. Conditional Statements (if, elif, else)
1. Even or Odd
Explanation: Checks parity using modulo operator.
n = int(input('Enter number: '))
if n % 2 == 0:
print('Even')
else:
print('Odd')
Sample Output:
Enter number: 7
Odd
2. Positive, Negative or Zero
Explanation: Simple sign check.
n = float(input('Enter number: '))
if n>0:
print('Positive')
elif n<0:
print('Negative')
else:
print('Zero')
Sample Output:
Enter number: -3
Negative
3. Greatest of three numbers
Explanation: Uses comparisons to find maximum.
a = float(input('a: '))
b = float(input('b: '))
c = float(input('c: '))
if a>=b and a>=c:
print('Greatest is', a)
elif b>=a and b>=c:
print('Greatest is', b)
else:
print('Greatest is', c)
Sample Output:
a: 5
b: 9
c: 3
Greatest is 9.0
4. Leap year check
Explanation: Uses Gregorian rule: divisible by 4 and (not by 100 unless by 400).
y = int(input('Year: '))
if (y % 400 == 0) or (y % 4 == 0 and y % 100 != 0):
print('Leap year')
else:
print('Not a leap year')
Sample Output:
Year: 2024
Leap year
5. Vowel or Consonant
Explanation: Checks if a character is vowel. Handles uppercase/lowercase.
ch = input('Enter a character: ')
if [Link]() in 'aeiou' and len(ch)==1:
print('Vowel')
else:
print('Consonant or invalid input')
Sample Output:
Enter a character: a
Vowel
6. Roots of quadratic equation
Explanation: Computes discriminant and real/complex roots.
import math
a = float(input('a: '))
b = float(input('b: '))
c = float(input('c: '))
d = b*b - 4*a*c
if d>0:
r1 = (-b + [Link](d))/(2*a)
r2 = (-b - [Link](d))/(2*a)
print('Two real roots:', r1, r2)
elif d==0:
r = -b/(2*a)
print('One real root:', r)
else:
real = -b/(2*a)
imag = [Link](-d)/(2*a)
print(f'Complex roots: {real}+{imag}j and {real}-{imag}j')
Sample Output:
a:1
b: -3
c: 2
Two real roots: 2.0 1.0
7. Calculate grade from marks
Explanation: Maps percentage to grade bands.
marks = float(input('Marks out of 100: '))
if marks>=90:
print('Grade A+')
elif marks>=80:
print('Grade A')
elif marks>=70:
print('Grade B')
elif marks>=60:
print('Grade C')
else:
print('Grade D')
Sample Output:
Marks out of 100: 85
Grade A
8. Divisible by 5 and 11
Explanation: Checks divisibility by both 5 and 11.
n = int(input('Enter number: '))
if n%5==0 and n%11==0:
print('Divisible by 5 and 11')
else:
print('Not divisible by both')
Sample Output:
Enter number: 55
Divisible by 5 and 11
3. Looping (for, while)
1. Print first 10 natural numbers
Explanation: Simple for-loop from 1 to 10.
for i in range(1,11):
print(i, end=' ')
print()
Sample Output:
1 2 3 4 5 6 7 8 9 10
2. Multiplication table
Explanation: Table for given number.
n = int(input('Number: '))
for i in range(1,11):
print(f'{n} x {i} = {n*i}')
Sample Output:
Number: 7
7 x 1 = 7
7 x 2 = 14
...
3. Sum of first N natural numbers
Explanation: Compute using loop or formula.
n = int(input('N: '))
s = sum(range(1,n+1))
print('Sum =', s)
Sample Output:
N: 5
Sum = 15
4. Factorial
Explanation: Compute factorial using loop.
n = int(input('n: '))
f = 1
for i in range(1, n+1):
f *= i
print(f'Factorial = {f}')
Sample Output:
n: 5
Factorial = 120
5. Fibonacci series up to N terms
Explanation: Generates Fibonacci sequence iteratively.
n = int(input('terms: '))
a, b = 0, 1
for _ in range(n):
print(a, end=' ')
a, b = b, a+b
print()
Sample Output:
terms: 6
0 1 1 2 3 5
6. Reverse a number
Explanation: Reverses digits using arithmetic.
n = int(input('Number: '))
rev = 0
temp = abs(n)
while temp>0:
rev = rev*10 + temp%10
temp//=10
if n<0:
rev = -rev
print('Reversed =', rev)
Sample Output:
Number: 1234
Reversed = 4321
7. Palindrome number
Explanation: Check if number equals its reverse.
n = int(input('Number: '))
rev = 0
temp = abs(n)
while temp>0:
rev = rev*10 + temp%10
temp//=10
if n==rev or n==-rev:
print('Palindrome')
else:
print('Not palindrome')
Sample Output:
Number: 121
Palindrome
8. Armstrong number
Explanation: Checks Armstrong for 3-digit by sum of cubes of digits; generalized approach used.
n = int(input('Number: '))
s = 0
order = len(str(abs(n)))
temp = abs(n)
while temp>0:
d = temp%10
s += d**order
temp//=10
if s==abs(n):
print('Armstrong')
else:
print('Not Armstrong')
Sample Output:
Number: 153
Armstrong
9. Sum of digits
Explanation: Adds digits using loop.
n = int(input('Number: '))
sumd = 0
temp = abs(n)
while temp>0:
sumd += temp%10
temp//=10
print('Sum of digits =', sumd)
Sample Output:
Number: 123
Sum of digits = 6
10. Prime numbers 1 to 100
Explanation: Simple sieve-like check; efficient enough for 100.
for n in range(2,101):
isprime = True
for i in range(2,int(n**0.5)+1):
if n%i==0:
isprime=False
break
if isprime:
print(n, end=' ')
print()
Sample Output:
2 3 5 7 11 13 17 19 23 29 ... 97
4. String Handling
1. Count vowels, consonants, digits, spaces
Explanation: Iterates characters and classifies them.
s = input('Enter string: ')
v= c = d = sp = 0
for ch in s:
if [Link]():
if [Link]() in 'aeiou': v+=1
else: c+=1
elif [Link](): d+=1
elif [Link](): sp+=1
print('Vowels:',v,'Consonants:',c,'Digits:',d,'Spaces:',sp)
Sample Output:
Enter string: Hello 123
Vowels: 2 Consonants: 3 Digits: 3 Spaces: 1
2. String palindrome
Explanation: Checks string against its reverse (ignores spaces/case).
s = input('Enter string: ').lower().replace(' ','')
if s == s[::-1]:
print('Palindrome')
else:
print('Not palindrome')
Sample Output:
Enter string: Madam
Palindrome
3. Uppercase & lowercase
Explanation: Demonstrates string methods.
s = input('Enter: ')
print('Upper:', [Link]())
print('Lower:', [Link]())
Sample Output:
Enter: Hello
Upper: HELLO
Lower: hello
4. Count words
Explanation: Splits on whitespace to count words.
s = input('Enter sentence: ').strip()
words = [Link]()
print('Word count =', len(words))
Sample Output:
Enter sentence: This is a test
Word count = 4
5. Replace word
Explanation: Uses replace method (non-regex).
s = input('Enter string: ')
old = input('Old word: ')
new = input('New word: ')
print('Result:', [Link](old,new))
Sample Output:
Enter string: I like apples
Old word: apples
New word: mango
Result: I like mango
6. Frequency of each character
Explanation: Uses dictionary to count occurrences.
s = input('Enter string: ')
freq = {}
for ch in s:
freq[ch] = [Link](ch,0) + 1
for k,v in [Link]():
print(repr(k),':',v)
Sample Output:
Enter string: aaaab
'a' : 4
'b' : 1
7. Reverse string using slicing
Explanation: Pythonic reversal with [::-1].
s = input('Enter string: ')
print('Reversed:', s[::-1])
Sample Output:
Enter string: ABC
Reversed: CBA
5. List Programs
1. Sum and average of N numbers in list
Explanation: Collects inputs into list, computes sum and average.
n = int(input('How many numbers: '))
arr = [float(input()) for _ in range(n)]
print('Sum =', sum(arr))
print('Average =', sum(arr)/n if n else 0)
Sample Output:
How many numbers: 3
1
2
3
Sum = 6.0
Average = 2.0
2. Max and min in list
Explanation: Uses built-in functions.
arr = list(map(int, input('Enter numbers separated by space: ').split()))
print('Max =', max(arr))
print('Min =', min(arr))
Sample Output:
Enter numbers separated by space: 4 7 1
Max = 7
Min = 1
3. Sort ascending & descending
Explanation: Demonstrates sort() and reversed list.
arr = list(map(int, input().split()))
[Link]()
print('Ascending:', arr)
print('Descending:', arr[::-1])
Sample Output:
4 1 3
Ascending: [1,3,4]
Descending: [4,3,1]
4. Count occurrences of element
Explanation: Uses [Link]().
arr = list(map(int, input().split()))
x = int(input('Element to count: '))
print('Occurrences =', [Link](x))
Sample Output:
1 2 2 3
Element to count: 2
Occurrences = 2
5. Print even and odd separately
Explanation: Partitions list into evens & odds.
arr = list(map(int, input().split()))
even = [x for x in arr if x%2==0]
odd = [x for x in arr if x%2!=0]
print('Even:', even)
print('Odd:', odd)
Sample Output:
1 2 3 4
Even: [2,4]
Odd: [1,3]
6. Merge two lists
Explanation: Concatenates lists.
a = list(map(int, input('List1: ').split()))
b = list(map(int, input('List2: ').split()))
print('Merged:', a + b)
Sample Output:
List1: 1 2
List2: 3 4
Merged: [1,2,3,4]
7. Remove duplicates from list
Explanation: Creates list of unique preserving order.
arr = list(map(int, input().split()))
seen = set()
uniq = []
for x in arr:
if x not in seen:
[Link](x)
[Link](x)
print('Unique:', uniq)
Sample Output:
1 2 2 3
Unique: [1,2,3]
6. Tuples
1. Create and display tuple
Explanation: Simple tuple creation.
t = (1, 'a', 3.5)
print(t)
for x in t:
print(x)
Sample Output:
(1, 'a', 3.5)
1
a
3.5
2. Max and min in tuple
Explanation: Works for numeric tuple.
t = tuple(map(int, input('Enter numbers: ').split()))
print('Max =', max(t))
print('Min =', min(t))
Sample Output:
Enter numbers: 4 7 1
Max = 7
Min = 1
3. Count occurrences in tuple
Explanation: Uses [Link]().
t = tuple(map(int, input().split()))
x = int(input('Element: '))
print('Count =', [Link](x))
Sample Output:
1 2 2 3
Element: 2
Count = 2
4. Convert list to tuple and vice versa
Explanation: Demonstrates conversions.
lst = [1,2,3]
t = tuple(lst)
print(t)
print(list(t))
Sample Output:
[1,2,3]
(1,2,3)
7. Dictionaries
1. Create student roll->marks dictionary
Explanation: Shows basic dict operations.
d = {1:85, 2:90, 3:78}
for k,v in [Link]():
print('Roll',k,'Marks',v)
Sample Output:
Roll 1 Marks 85
Roll 2 Marks 90
Roll 3 Marks 78
2. Add, update, delete elements
Explanation: Demonstrates assignment and pop().
d = {}
d['r1'] = 80
print(d)
d['r1'] = 85
print('Updated', d)
[Link]('r1')
print('After delete', d)
Sample Output:
{'r1': 80}
Updated {'r1': 85}
After delete {}
3. Key with highest value
Explanation: Find key for maximum value using max with key parameter.
d = {'a':50,'b':78,'c':45}
max_key = max(d, key=[Link])
print('Top student:', max_key, d[max_key])
Sample Output:
Top student: b 78
4. Display keys and values separately
Explanation: Uses keys() and values().
d = {'a':1,'b':2}
print('Keys:', list([Link]()))
print('Values:', list([Link]()))
Sample Output:
Keys: ['a','b']
Values: [1,2]
5. Word frequency in sentence
Explanation: Counts words using dict.
s = input('Enter sentence: ')
words = [Link]()
freq = {}
for w in words:
freq[w] = [Link](w,0)+1
print(freq)
Sample Output:
Enter sentence: hi hi hello
{'hi':2,'hello':1}
8. Functions
1. Factorial function
Explanation: Defines function and returns factorial.
def factorial(n):
f=1
for i in range(1,n+1):
f*=i
return f
print(factorial(5))
Sample Output:
120
2. Prime check function
Explanation: Returns True/False.
def is_prime(n):
if n<2: return False
for i in range(2,int(n**0.5)+1):
if n%i==0: return False
return True
print(is_prime(17))
Sample Output:
True
3. Sum of digits function
Explanation: Returns sum of digits.
def sum_digits(n):
return sum(int(d) for d in str(abs(n)))
print(sum_digits(123))
Sample Output:
6
4. Palindrome string function
Explanation: Checks palindrome for string.
def is_pal(s):
t = [Link](' ','').lower()
return t==t[::-1]
print(is_pal('Nitin'))
Sample Output:
True
5. Area functions menu-driven
Explanation: Menu to compute area of circle/rectangle/triangle.
import math
print('[Link] [Link] [Link]')
ch = int(input())
if ch==1:
r=float(input('r:'))
print('Area:', [Link]*r*r)
elif ch==2:
l=float(input('l:')); b=float(input('b:'))
print('Area:', l*b)
elif ch==3:
b=float(input('base:')); h=float(input('height:'))
print('Area:', 0.5*b*h)
else:
print('Invalid')
Sample Output:
1
r:3
Area: 28.274333882308138
6. Max of three numbers function
Explanation: Returns maximum using comparisons.
def max3(a,b,c):
return max(a,b,c)
print(max3(4,9,2))
Sample Output:
9
9. Random and Math Library
1. Generate random numbers 1-100
Explanation: Uses [Link].
import random
print([Link](1,100))
Sample Output:
42
2. Roll a dice
Explanation: Simulates dice roll 1-6.
import random
print('Dice shows', [Link](1,6))
Sample Output:
Dice shows 4
3. Square root, power, trig (math)
Explanation: Demonstrate math functions.
import math
print('sqrt(16)=', [Link](16))
print('2^5=', [Link](2,5))
print('sin(0)=', [Link](0))
Sample Output:
sqrt(16)= 4.0
2^5= 32.0
sin(0)= 0.0
4. GCD using [Link]
Explanation: Uses built-in gcd.
import math
print([Link](12,18))
Sample Output:
6
10. File Handling (Text Files)
1. Write text to file
Explanation: Opens file in write mode and writes text.
with open('[Link]','w') as f:
[Link]('Hello\nThis is a file')
print('Written')
Sample Output:
Written
2. Read contents of file
Explanation: Reads entire file and prints content.
with open('[Link]') as f:
print([Link]())
Sample Output:
Hello
This is a file
3. Count lines, words, chars
Explanation: Reads file and counts lines, words, characters.
with open('[Link]') as f:
data = [Link]()
lines = [Link]()
words = [Link]()
print('Lines=', len(lines))
print('Words=', len(words))
print('Chars=', len(data))
Sample Output:
Lines=2
Words=4
Chars=24
4. Copy contents from one file to another
Explanation: Reads source and writes to destination.
with open('[Link]') as f:
data = [Link]()
with open('[Link]','w') as g:
[Link](data)
print('Copied')
Sample Output:
Copied
5. Append content to existing file
Explanation: Opens file in append mode.
with open('[Link]','a') as f:
[Link]('\nAppended line')
print('Appended')
Sample Output:
Appended
6. Count vowels in file
Explanation: Counts vowels by reading file content.
with open('[Link]') as f:
data = [Link]().lower()
vowels = sum(1 for ch in data if ch in 'aeiou')
print('Vowels =', vowels)
Sample Output:
Vowels = 7
11. Miscellaneous / Logical Programs
1. Perfect number check
Explanation: Sum proper divisors and compare.
n = int(input('n: '))
s = 0
for i in range(1, n//2+1):
if n%i==0: s += i
if s==n:
print('Perfect')
else:
print('Not perfect')
Sample Output:
n: 6
Perfect
2. Print all factors of a number
Explanation: Iterates to find factors.
n = int(input('n: '))
for i in range(1, n+1):
if n%i==0: print(i, end=' ')
print()
Sample Output:
n: 12
1 2 3 4 6 12
3. Binary <-> Decimal conversion
Explanation: Converts using int() and bin().
dec = int(input('Decimal: '))
print('Binary =', bin(dec)[2:])
# To convert binary to decimal: int('1010',2)
Sample Output:
Decimal: 10
Binary = 1010
4. LCM of two numbers
Explanation: Computes LCM via gcd.
import math
a = int(input('a: '))
b = int(input('b: '))
print('LCM =', abs(a*b)//[Link](a,b))
Sample Output:
a: 12
b: 18
LCM = 36
5. Menu-driven basic calculator
Explanation: Performs + - * / based on choice.
a = float(input('a: '))
b = float(input('b: '))
ch = input('Enter + - * /: ')
if ch=='+': print(a+b)
elif ch=='-': print(a-b)
elif ch=='*': print(a*b)
elif ch=='/': print(a/b if b!=0 else 'Inf')
else: print('Invalid')
Sample Output:
a: 5
b: 2
+
7.0