Professional Documents
Culture Documents
Ques – 1) Write a program that will determine whether a point lies inside the circle, on the
circle or outside the circle for given the coordinates(x,y) of the center of a circle and its
radius. (Use sort () and pow () functions)
Ans -import math
def point_position(x, y, center_x, center_y, radius):
distance = math.sqrt(pow(x - center_x, 2) + pow(y - center_y, 2))
Output:
Enter a: 8
Enter b: 5
Enter c: 9
The solution are (-0.3125-1.0135796712641785j) and (-0.3125+1.0135796712641785j)
c
Ques – 3) Write a python program to check whether a triangle is isosceles, equilateral, scalene or
right angled triangle for the given three sides of a triangle
Ans - # Display a message prompting the user to input lengths of the sides of a triangle
# Request input from the user for the length of side 'x' and convert it to an integer
x = int(input("x: "))
# Request input from the user for the length of side 'y' and convert it to an integer
y = int(input("y: "))
# Request input from the user for the length of side 'z' and convert it to an integer
z = int(input("z: "))
# Check conditions to determine the type of triangle based on the lengths of its sides
if x == y == z:
print("Equilateral triangle")
# If at least two sides are equal, display that it's an isosceles triangle
elif x == y or y == z or z == x:
print("Isosceles triangle")
# If all sides have different lengths, display that it's a scalene triangle
else:
print("Scalene triangle")
output – x : 6
y:8
z : 12
scalene trinagle
Ques – 4 ) Design a function named Print Unique () in python which will return a list of
unique elements in sorted order
Ans - # function to get unique values
def unique(list1):
# driver code
list1 = [10, 20, 10, 30, 40, 40]
print("the unique values from 1st list is")
unique(list1)
list2 = [1, 2, 1, 1, 3, 4, 3, 3, 5]
print("\nthe unique values from 2nd list is")
unique(list2)
Output : the unique value from 1st list is
10 20 30 40
the unique value from list 2nd ids
1 2 3 4 5
Ques -5) Develop a python script to count the number of alphabets, digits and special characters in
a string
Ans - # Python program to Count Alphabets Digits and Special Characters in a String
for i in range(len(string)):
if(string[i].isalpha()):
alphabets = alphabets + 1
elif(string[i].isdigit()):
digits = digits + 1
else:
special = special + 1
Ques – 6) Develop a Python program that reads a string from the keyboard and creates a
dictionary containing frequency of each character occurring in the string
Ans - # Define a function named char_frequency that takes one argument, str1.
def char_frequency(str1):
dict = {}
for n in str1:
keys = dict.keys()
if n in keys:
dict[n] += 1
else:
dict[n] = 1
# Return the dictionary containing the frequency of each character in the input string.
return dict
# Call the char_frequency function with the argument 'google.com' and print the result.
print(char_frequency('google.com'))
Sample Output:
Ques – 7) Write a program that creates and uses a Time class to perform various time
arithmetic operations
Ques 8 )Write a program that implements a Matrix Class and perform addition,
multiplication, and transpose operations on 3*3matrices
Ans - # Python program to perform matrix operations ,matrix addition, matrix subtraction,
matrix multiplication – addition
Ques -9) Write a program that merges lines alternatively from two files and write the results
to new file. If one file has less number of lines than the other, then remaining lines from the
larger file should be simply copied into target file.
Ans - data = data2 = "";
# Merging 2 files
# To add the data of file2
# from next line
data += "\n"
data += data2
Ques -10 ) Write a program to read the file and print a list of all blood donors whose age is
below 25 and blood type is 3
Ans - def read_file(file_path):
with open(file_path, 'r') as file:
lines = file.readlines()
return lines
def filter_donors(lines):
donors = []
for line in lines[1:]: # Skipping the header line
name, age, blood_type = line.strip().split(', ')
age = int(age)
blood_type = int(blood_type)
if age < 25 and blood_type == 3:
donors.append(name)
return donors
def main():
file_path = 'path/to/your/file.txt' # Replace with the actual path to your file
lines = read_file(file_path)
donors = filter_donors(lines)
if donors:
print("List of blood donors below 25 years old with blood type 3:")
for donor in donors:
print(donor)
else:
print("No matching donors found.")
if __name__ == "__main__":
main()
Output – blood donor
Age – 25
Group - 3