Program 1: Swap Two Variables (Python)
Question: Write a Python program to swap the values of two variables.
x = 100
y = 200
temp = x
x = y
y = temp
print("The value of x after swapping: {}".format(x))
print("The value of y after swapping: {}".format(y))
Program 2: Circulate the Values of N Variables (Python)
Question: Write a Python program to circulate (rotate) the values of N integers.
no_of_terms = int(input("Enter number of values: "))
list1 = []
for val in range(no_of_terms):
ele = int(input("Enter integer: "))
list1.append(ele)
print("Circulating the elements of list:", list1)
for val in range(no_of_terms):
ele = list1.pop(0)
list1.append(ele)
print(list1)
Program 3: Calculate Distance Between Two Points (Python)
Question: Write a Python program to calculate the distance between two points using the distance formula.
import math
p1 = [4, 0]
p2 = [6, 6]
distance = math.sqrt(((p1[0] - p2[0]) ** 2) + ((p1[1] - p2[1]) ** 2))
print(distance)
Program 4: Print Fibonacci Number Series (Python)
Question: Write a Python program to print the Fibonacci number series using recursion.
def Fibonacci(number):
if number == 0:
return 0
elif number == 1:
return 1
else:
return Fibonacci(number - 2) + Fibonacci(number - 1)
number = int(input("Enter the Range number: "))
for n in range(number):
print(Fibonacci(n))
Program 5: Print Number Patterns (Python)
Question: Write a Python program to print a symmetric number pyramid pattern.
N = int(input("Enter the value: "))
for i in range(1, N + 1):
for k in range(N, i, -1):
print(" ", end="")
for j in range(1, i + 1):
print(j, end="")
for j in range(i - 1, 0, -1):
print(j, end="")
print()
Program 6: Print Number Pyramid (Python)
Question: Write a Python program to print a right-aligned pyramid pattern using stars.
n = int(input("Enter number of lines: "))
for i in range(1, n + 1):
print(" " * (n - i) + "*" * i)
Program 7: Find All Indexes of an Element in a List (Python)
Question: Write a Python program to find all index positions of the element "Book" in a list.
my_list = ['Book', 'Stock Register', 'Library Management System', 'Book',
'Book Return Register', 'Book Availability', 'Book']
all_indexes = []
for i in range(len(my_list)):
if my_list[i] == 'Book':
all_indexes.append(i)
print("Original list:", my_list)
print("Indexes for element 'Book':", all_indexes)
Program 8: Count Occurrence of an Element in a Tuple (Python)
Question: Write a Python program to create a tuple of car components and count how many times "Seat" appears.
thistuple = ("Seat", "Gear", "Horn", "RearMirror", "Seat",
"Headlight", "Accelerator")
x = thistuple.count("Seat")
print(x)
Program 9: Modify a Tuple by Converting to List (Python)
Question: Write a Python program to replace "Sand" with "M-Sand" in a tuple of construction materials.
x = ("Cement", "Sand", "Bricks")
y = list(x)
y[1] = "M-Sand"
x = tuple(y)
print(x)
Program 10: Repeat Tuple Elements (Python)
Question: Write a Python program to repeat the elements of a tuple containing building materials.
building = ("Water", "R-Sand", "M Sand", "Bricks", "Tiles")
mytuple = building * 2
print(mytuple)
Program 12: Implement Dictionary for State Names (Python)
Question: Create a dictionary that stores states and their languages.
thisdict = {"Tamilnadu": "Tamil", "Andra": "Telugu",
"Karnataka": "Kannada", "Kerala": "Malayalam"}
print(thisdict)
Program 13: Implement Dictionary for Colours (Python)
Question: Write a Python program to copy a dictionary containing colour and vehicle details.
thisdict = {"Colour": "White", "Number": 1234,
"Rear mirror": 2, "Wheel Count": 4}
mydict = thisdict.copy()
print(mydict)
Program 14: Implement Dictionary for Civil Structure Elements (Python)
Question: Write a Python program to find common elements between two sets.
x = {"Sand", "Cement", "Bricks"}
y = {"M-Sand", "Cement", "Bricks"}
x.intersection_update(y)
print(x)
Program 15: Factorial of a Number (Python)
Question: Write a Python program to find the factorial of a given number.
num = int(input("Enter a number: "))
factorial = 1
if num < 0:
print("Factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
for i in range(1, num + 1):
factorial *= i
print("The factorial of", num, "is", factorial)
Program 16: Largest Number in a List (Python)
Question: Write a Python program to find the largest number in a list.
def max_num_in_list(list):
max_value = list[0]
for a in list:
if a > max_value:
max_value = a
return max_value
print(max_num_in_list([7, 20, -8, -25]))
Program 17: Reverse a String (Python)
Question: Write a Python program to reverse a string using slicing.
txt = "AllTheBest"[::-1]
print(txt)
Program 18: Check Palindrome in a String (Python)
Question: Write a Python program to check if a string is a palindrome.
def isPalindrome(s):
return s == s[::-1]
s = "madam"
ans = isPalindrome(s)
if ans:
print("Yes")
else:
print("No")
Program 21: Copy Contents From One File to Another (Python)
Question: Write a Python program to copy the contents of one file into another.
with open("myfile.txt", "r") as firstfile:
with open("myfile1.txt", "a") as secondfile:
for line in firstfile:
secondfile.write(line)
Program 22: Count Words in a String (Python)
Question: Write a Python program to count the number of words in a string using split().
test_string = "Narasu’s Sarathy Institute of Technology"
print("The original string is: " + test_string)
res = len(test_string.split())
print("The number of words in string are: " + str(res))
Program 23: Print the Longest Word in a Sentence (Python)
Question: Write a Python program to print the longest word from a given sentence.
def largestWord(words):
words = sorted(words, key=len)
print(words[-1])
if __name__ == "__main__":
s = "spread positivity and care yourself"
l = s.split()
largestWord(l)
Program 24: Handle Divide by Zero Error (Python)
Question: Write a Python program to handle divide-by-zero and invalid input errors.
try:
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
result = num1 / num2
print(result)
except ValueError:
print("Invalid input. Please enter integers only...")
except ZeroDivisionError as e:
print(e)
Program 25: Check Voter Age Validity (Python)
Question: Write a Python program to check whether a person is eligible to vote.
def main():
try:
age = int(input("Enter your age: "))
if age >= 18:
print("Eligible to vote")
else:
print("Not eligible to vote")
except:
print("Invalid number")
main()
Program 26: Student Mark Range Validation (Python)
Question: Write a Python program to validate a student's mark and print the appropriate grade.
try:
marks = float(input("Enter your marks in Computer Science: "))
if marks > 90:
print("Grade: O")
elif marks >= 80 and marks < 90:
print("Grade: A+")
elif marks >= 70 and marks < 80:
print("Grade: A")
elif marks >= 60 and marks < 70:
print("Grade: B+")
elif marks >= 50 and marks < 60:
print("Grade: B")
elif marks >= 40 and marks < 50:
print("Grade: C")
else:
print("Grade: F")
if marks < 0:
raise ValueError("That is a negative number!")
except ValueError as e:
print(e)