0% found this document useful (0 votes)
2 views4 pages

Python Termwork 20 Programs

Uploaded by

satyamk95087
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

Python Termwork 20 Programs

Uploaded by

satyamk95087
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

SBTE Bihar - Python Programming Termwork (20

Programs with Output)

Program 1: Display Message and Data Types


Code:
print("Welcome to Python Programming") x = 10 y = 3.14 name = "SBTE Bihar"
print("Integer:", x) print("Float:", y) print("String:", name) print("Type of x:",
type(x)) print("Type of y:", type(y)) print("Type of name:", type(name))
Output:
Output: Welcome to Python Programming Integer: 10 Float: 3.14 String: SBTE Bihar
Type of x: Type of y: Type of name:

Program 2: Arithmetic Operations


Code:
a = 12 b = 5 print("Addition:", a + b) print("Subtraction:", a - b)
print("Multiplication:", a * b) print("Division:", a / b) print("Floor Division:", a
// b) print("Modulus:", a % b) print("Power:", a ** b)
Output:
Output: Addition: 17 Subtraction: 7 Multiplication: 60 Division: 2.4 Floor Division:
2 Modulus: 2 Power: 248832

Program 3: Simple Calculator


Code:
a = float(input("Enter first number: ")) b = float(input("Enter second number: "))
op = input("Enter operator (+, -, *, /): ") if op == '+': print("Result:", a + b)
elif op == '-': print("Result:", a - b) elif op == '*': print("Result:", a * b) elif
op == '/': print("Result:", a / b) else: print("Invalid Operator")
Output:
Output Example: Enter first number: 5 Enter second number: 2 Enter operator (+, -,
*, /): * Result: 10.0

Program 4: Area and Perimeter of Circle


Code:
r = float(input("Enter radius: ")) area = 3.14 * r * r peri = 2 * 3.14 * r
print("Area:", area) print("Perimeter:", peri)
Output:
Output Example: Enter radius: 5 Area: 78.5 Perimeter: 31.4

Program 5: Grade Calculator


Code:
marks = int(input("Enter Marks: ")) if marks >= 90: print("Grade: A+") elif marks >=
75: print("Grade: A") elif marks >= 60: print("Grade: B") elif marks >= 45:
print("Grade: C") else: print("Fail")
Output:
Output Example: Enter Marks: 78 Grade: A

Program 6: Factorial using for loop


Code:
num = int(input("Enter a number: ")) fact = 1 for i in range(1, num+1): fact *= i
print("Factorial:", fact)
Output:
Output Example: Enter a number: 5 Factorial: 120

Program 7: Prime Number Checker


Code:
num = int(input("Enter a number: ")) if num > 1: for i in range(2, int(num/2)+1): if
num % i == 0: print(num, "is not prime") break else: print(num, "is prime") else:
print(num, "is not prime")
Output:
Output Example: Enter a number: 7 7 is prime

Program 8: Even and Odd Separation


Code:
numbers = [10, 21, 4, 45, 66, 93, 11] even = [] odd = [] for num in numbers: if num %
2 == 0: [Link](num) else: [Link](num) print("Even Numbers:", even)
print("Odd Numbers:", odd)
Output:
Output: Even Numbers: [10, 4, 66] Odd Numbers: [21, 45, 93, 11]

Program 9: String Operations


Code:
text = "Python Programming" print("Original:", text) print("Uppercase:",
[Link]()) print("Lowercase:", [Link]()) print("Slice (0–6):", text[0:6])
print("Replace:", [Link]("Python", "SBTE"))
Output:
Output: Original: Python Programming Uppercase: PYTHON PROGRAMMING Lowercase: python
programming Slice (0–6): Python Replace: SBTE Programming

Program 10: Count Vowels and Consonants


Code:
string = "Diploma in Computer Science" vowels = "aeiouAEIOU" v = c = 0 for ch in
string: if [Link](): if ch in vowels: v += 1 else: c += 1 print("Vowels:", v)
print("Consonants:", c)
Output:
Output: Vowels: 10 Consonants: 15

Program 11: List Operations


Code:
fruits = ['apple', 'banana', 'cherry'] [Link]('mango')
[Link]('banana') print(fruits) print('cherry' in fruits) print('banana' in
fruits)
Output:
Output: ['apple', 'cherry', 'mango'] True False

Program 12: Tuple Immutability


Code:
t = (1, 2, 3, 4) print("Tuple:", t) print("First Element:", t[0]) # t[0] = 10 # This
will cause an error print("Tuples are immutable!")
Output:
Output: Tuple: (1, 2, 3, 4) First Element: 1 Tuples are immutable!
Program 13: Set Operations
Code:
A = {1, 2, 3, 4} B = {3, 4, 5, 6} print("Union:", A | B) print("Intersection:", A &
B) print("Difference:", A - B)
Output:
Output: Union: {1, 2, 3, 4, 5, 6} Intersection: {3, 4} Difference: {1, 2}

Program 14: Dictionary Operations


Code:
student = {'name': 'Ravi', 'age': 20, 'course': 'Python'} print(student)
student['age'] = 21 student['grade'] = 'A' print(student) print("Keys:",
[Link]()) print("Values:", [Link]())
Output:
Output: {'name': 'Ravi', 'age': 20, 'course': 'Python'} {'name': 'Ravi', 'age': 21,
'course': 'Python', 'grade': 'A'} Keys: dict_keys(['name', 'age', 'course',
'grade']) Values: dict_values(['Ravi', 21, 'Python', 'A'])

Program 15: Functions Example


Code:
def greet(name="User"): print("Hello,", name) greet("Ravi") greet()
Output:
Output: Hello, Ravi Hello, User

Program 16: Recursive Fibonacci


Code:
def fib(n): if n <= 1: return n else: return fib(n-1) + fib(n-2) n = 6 for i in
range(n): print(fib(i), end=' ')
Output:
Output: 0 1 1 2 3 5

Program 17: Lambda with map/filter/reduce


Code:
from functools import reduce numbers = [1, 2, 3, 4, 5] doubles = list(map(lambda x:
x*2, numbers)) evens = list(filter(lambda x: x%2==0, numbers)) product =
reduce(lambda x, y: x*y, numbers) print("Doubles:", doubles) print("Evens:", evens)
print("Product:", product)
Output:
Output: Doubles: [2, 4, 6, 8, 10] Evens: [2, 4] Product: 120

Program 18: Math and Random Module


Code:
import math, random print("Square root of 25:", [Link](25)) print("Random number
(1–10):", [Link](1, 10))
Output:
Output Example: Square root of 25: 5.0 Random number (1–10): 7

Program 19: NumPy Array Operations


Code:
import numpy as np arr = [Link]([1, 2, 3, 4, 5]) print("Array:", arr)
print("Sum:", [Link](arr)) print("Mean:", [Link](arr)) print("Add 10:", arr + 10)
Output:
Output: Array: [1 2 3 4 5] Sum: 15 Mean: 3.0 Add 10: [11 12 13 14 15]

Program 20: NumPy Matrix Multiplication


Code:
import numpy as np A = [Link]([[1, 2, 3], [4, 5, 6]]) B = [Link]([[1, 2], [3,
4], [5, 6]]) C = [Link](A, B) print("Matrix A:\n", A) print("Matrix B:\n", B)
print("A x B =\n", C)
Output:
Output: Matrix A: [[1 2 3] [4 5 6]] Matrix B: [[1 2] [3 4] [5 6]] A x B = [[22 28]
[49 64]]

You might also like