To write a Python program that calculates the area of a circle based on the radius provided by the user. This program uses a class named cse and a method mech to perform the calculation.
- Get user input: Take the radius of the circle as input from the user.
- Define the class: Create a class named
cse. - Define the method: Inside the class, define the method
mechto calculate the area of the circle using the formula:
Area = pi *r^2 - Execute the program: Create an object of the class and call the method with the radius value.
import math
class cse:
def mech(self, radius):
area = math.pi * radius ** 2
print(f"Area of circle: {area:.2f}")
r = float(input())
obj = cse()
obj.mech(r)
Thus the program executed successfully.
To write a Python program that merges two dictionaries and combines their key-value pairs.
- Define two dictionaries dict1 and dict2 with some key-value pairs.
- Define a function merge() that merges the two dictionaries using the ** unpacking operator.
- The merged result will combine keys from both dictionaries. If a key exists in both, the value from dict2 will overwrite that from dict1.
- Call the merge() function and print the merged dictionary.
dict1={'Ten': 10,'Twenty': 20,'Thirty': 30} dict2={'Thirty': 30,'Fourty': 40,'Fifty': 50} def merge (dict1,dict2): res={**dict1 , **dict2} return res dict3=merge(dict1,dict2) print(dict3)
thus,the program has been executed successfully.
This Python program demonstrates how to sort a dictionary:
- Alphabetically by keys
- Alphabetically by values
To write a Python program that sorts a dictionary's:
- Keys in alphabetical order
- Values in alphabetical order
- Start the program.
- Define a dictionary with key-value pairs.
- Sort by Keys:
- Use sorted(dictionary.items())
- Convert the result to a dictionary using dict()
- Sort by Values:
- Use sorted(dictionary.items(), key=lambda item: item[1])
- Convert the result to a dictionary using dict()
- Display the original and sorted dictionaries.
- End the program.
def dictionairy():
key_value ={}
key_value[2] = 56
key_value[1] = 2
key_value[5] = 12
key_value[4] = 24
key_value[6] = 18
key_value[3] = 323
s=sorted(key_value.items(),key=lambda x:x[1])
print ("Keys and Values sorted in alphabetical order by the value")
print(s)
def main():
dictionairy()
The program successfully sorts the dictionary by its keys and values in alphabetical order and prints both sorted versions along with the original dictionary.
To write a Python program that handles an IndexError when trying to access an element beyond the available range of a list.
- Define a list
list1with some integer elements. - Use a try-except block:
- In the
tryblock, attempt to access an index that is out of range (e.g.,list1[5]). - In the
exceptblock, catch the error and print a custom message"You're out of list range".
- In the
- Print the result based on whether the index access succeeds or fails.
try:
# Taking 3 elements input from the user
L = []
for i in range(3):
item = ['laptop','mobile','pen']
L.append(item)
# Trying to access index 4
print(L[4])
except IndexError:
print("check index range")
Thus the program executed successfully.
To write a Python program that counts the number of lines in a text file story.txt that do not start with the alphabet 'T'.
- Open the file story.txt in read mode.
- Initialize a counter count to zero.
- Iterate through each line of the file:
- Check if the first character of the line is not 'T'.
- If the line does not start with 'T', increment the count by 1.
- After processing all lines, print the count value, which represents the number of lines that do not start with 'T'.
def returnSum(myDict):
final=0
for i in myDict.values():
final+=i
return final
#driver functions
myDict = {'a': 100, 'b': 200, 'c': 300}
print("Sum :",returnSum(myDict))
Thus,the program has been executed successfully.




