Class 12 Python Practical File
Below is a collection of 35 Python programs along with their sample outputs.
1. Python Program to Print "Hello, World!"
print("Hello, World!")
Output:
Hello, World!
2. Program to Add Two Numbers
a = 10
b=5
print(a + b)
Output:
15
3. Area of a Circle
r=5
area = 3.14 * r * r
print(area)
Output:
78.5
4. Check Even or Odd
num = 7
if num % 2 == 0:
print("Even")
else:
print("Odd")
Output:
Odd
5. Factorial of a Number
n=5
fact = 1
for i in range(1, n + 1):
fact *= i
print(fact)
Output:
120
6. Fibonacci Series (First 10 Terms)
a, b = 0, 1
for _ in range(10):
print(a)
a, b = b, a + b
Output:
0 1 1 2 3 5 8 13 21 34
7. Largest of Three Numbers
a, b, c = 10, 25, 15
print(max(a, b, c))
Output:
25
8. Sum of Digits
n = 1234
s=0
for digit in str(n):
s += int(digit)
print(s)
Output:
10
9. Reverse a Number
n = 1234
rev = int(str(n)[::-1])
print(rev)
Output:
4321
10. Check Palindrome Number
n = 121
if str(n) == str(n)[::-1]:
print("Palindrome")
else:
print("Not Palindrome")
Output:
Palindrome
11. Check Prime Number
n = 11
flag = True
for i in range(2, n):
if n % i == 0:
flag = False
break
print("Prime" if flag else "Not Prime")
Output:
Prime
12. Print Multiplication Table
n=5
for i in range(1, 11):
print(n * i)
Output:
5 10 15 20 25 30 35 40 45 50
13. Count Vowels in a String
s = "computer"
vowels = "aeiou"
count = sum(1 for ch in s if ch in vowels)
print(count)
Output:
14. String Reverse Program
s = "Python"
print(s[::-1])
Output:
nohtyP
15. Simple Calculator
a, b = 10, 5
print(a + b)
print(a - b)
print(a * b)
print(a / b)
Output:
15
5
50
2.0
16. List Sorting
lst = [5, 2, 9, 1]
[Link]()
print(lst)
Output:
[1, 2, 5, 9]
17. Linear Search
arr = [10, 20, 30, 40]
x = 30
found = False
for i in arr:
if i == x:
found = True
print(found)
Output:
True
18. Binary Search
arr = [10, 20, 30, 40]
l, u, x = 0, len(arr)-1, 30
found = False
while l <= u:
mid = (l + u) // 2
if arr[mid] == x:
found = True
break
elif arr[mid] < x:
l = mid + 1
else:
u = mid - 1
print(found)
Output:
True
19. Sum of List Elements
lst = [1, 2, 3, 4]
print(sum(lst))
Output:
10
20. Find Largest in List
lst = [3, 8, 2, 10]
print(max(lst))
Output:
10
21. Dictionary Example
student = {"name": "Amit", "marks": 90}
print(student)
Output:
{'name': 'Amit', 'marks': 90}
22. Count Characters in String
s = "banana"
print(len(s))
Output:
23. File Write Program
f = open("[Link]", "w")
[Link]("Hello File!")
[Link]()
Output:
File created
24. File Read Program
f = open("[Link]", "r")
print([Link]())
[Link]()
Output:
Hello File!
25. Program Using Function
def add(a, b):
return a + b
print(add(2, 3))
Output:
26. Lambda Function Example
square = lambda x: x * x
print(square(5))
Output:
25
27. Recursion Example (Factorial)
def fact(n):
if n == 1:
return 1
return n * fact(n-1)
print(fact(5))
Output:
120
28. Tuple Example
t = (1, 2, 3)
print(t)
Output:
(1, 2, 3)
29. Set Example
s = {1, 2, 3, 2}
print(s)
Output:
{1, 2, 3}
30. Pattern Printing
for i in range(1, 6):
print("*" * i)
Output:
**
***
****
*****
31. Square of Each Element in List
lst = [1, 2, 3]
print([x*x for x in lst])
Output:
[1, 4, 9]
32. Convert Celsius to Fahrenheit
c = 20
f = (c * 9/5) + 32
print(f)
Output:
68.0
33. Swap Two Numbers
a, b = 5, 10
a, b = b, a
print(a, b)
Output:
10 5
34. GCD of Two Numbers
import math
print([Link](12, 18))
Output:
35. Basic Class Example
class Student:
def __init__(self, name):
[Link] = name
s = Student("Amit")
print([Link])
Output:
Amit