Python Solutions for Computer Science Practical File
1. Sphere Area and Volume
import math
r = float(input("Enter the radius of the sphere: "))
area = math.pi * r**2
volume = (4/3) * math.pi * r**3
print(f"Area: {area:.2f}")
print(f"Volume: {volume:.2f}")
2. Factorial of a Number
import math
num = int(input("Enter a number: "))
print(f"Factorial: {math.factorial(num)}")
3. Palindrome Number
num = input("Enter a number: ")
if num == num[::-1]:
print("Palindrome")
else:
print("Not a Palindrome")
4. Prime Number Check
num = int(input("Enter a number: "))
if num > 1:
for i in range(2, int(num**0.5) + 1):
if num % i == 0:
print("Not a Prime Number")
break
else:
print("Prime Number")
else:
print("Not a Prime Number")
5. Pattern 1
n = 5
for i in range(1, n + 1):
print(" ".join(map(str, range(1, i + 1))))
6. Pattern 2
n = 5
count = 1
for i in range(1, n + 1):
print(" ".join(str(count + j) for j in range(i)))
count += i
7. String Statistics
s = input("Enter a string: ")
uppercase = sum(1 for c in s if c.isupper())
lowercase = sum(1 for c in s if c.islower())
alphabets = sum(1 for c in s if c.isalpha())
digits = sum(1 for c in s if c.isdigit())
symbols = len(s) - (alphabets + digits)
print(f"Uppercase: {uppercase}, Lowercase: {lowercase}, Alphabets: {alphabets}, Digits:
{digits}, Symbols: {symbols}")
8. Character Location in String
s = input("Enter a string: ")
c = input("Enter a character: ")
indices = [i for i, char in enumerate(s) if char == c]
if indices:
print(f"Character found at indices: {indices}")
else:
print("Character not found")
9. Remove All Occurrences of an Element from List
lst = list(map(int, input("Enter list elements: ").split()))
el = int(input("Enter element to remove: "))
lst = [x for x in lst if x != el]
print("Updated list:", lst)
10. List Slices and Calculations
lst = list(range(1, 21))
slice1 = lst[5:16:2]
slice2 = lst[5:16:4]
print(f"Sum of first slice: {sum(slice1)}")
print(f"Average of second slice: {sum(slice2) / len(slice2)}")
11. Maximum Element from Two Lists
lst1 = list(map(int, input("Enter elements of first list: ").split()))
lst2 = list(map(int, input("Enter elements of second list: ").split()))
max1 = max(lst1)
max2 = max(lst2)
if max1 > max2:
print(f"Max Element: {max1}, Index: {lst1.index(max1)} in List 1")
else:
print(f"Max Element: {max2}, Index: {lst2.index(max2)} in List 2")
12. Tuple Search
n = int(input("Enter number of students: "))
students = tuple(input("Enter student name: ") for _ in range(n))
name = input("Enter name to search: ")
print("Present" if name in students else "Not Present")
13. Check for Duplicates in Tuple
tup = tuple(input("Enter elements: ").split())
print("Duplicates Found" if len(tup) != len(set(tup)) else "No Duplicates")
14. Dictionary of Students with Marks Above 75
n = int(input("Enter number of students: "))
students = {input("Enter roll number: "): (input("Enter name: "), int(input("Enter
marks: "))) for _ in range(n)}
print("Students with marks above 75:")
for roll, (name, marks) in students.items():
if marks > 75:
print(name)
15. Check if Value Exists in Dictionary
d = {'a': 10, 'b': 20, 'c': 30}
value = int(input("Enter value to check: "))
for k, v in d.items():
if v == value:
print(f"Value found for key: {k}")
break
else:
print("Value not found")