-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquestions.py
More file actions
134 lines (111 loc) · 3.67 KB
/
Copy pathquestions.py
File metadata and controls
134 lines (111 loc) · 3.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# # write a program to sort numbers
# def sort():
# numbers = [1,5,6,9,4,5,3,2]
# numbers.sort()
# return numbers
# print(sort())
# # Write a program to find the largest number in a list
# def largestNumber():
# list = []
# for i in range(6):
# number = int(input("Enter Number"))
# list.append(number)
# largest = max(list)
# print("The largest number is:", largest)
# largestNumber()
# # Write a program to find the second largest number in a list
# def secondLargestNumber():
# list = []
# for i in range(6):
# number = int(input("Enter Number: "))
# list.append(number)
# list.sort()
# secondLargestNumber = list[-2]
# print("Second largest number is: ", secondLargestNumber)
# secondLargestNumber()
# # Write a program to find the smallest number in a list
# def smallestNumber():
# list = []
# for i in range(6):
# number = int(input("Enter Number: "))
# list.append(number)
# smallest = min(list)
# print("The smallest number is:", smallest)
# smallestNumber()
# # Write a program to find the sum of all numbers in a list
# def sumOfList():
# list = []
# for i in range(5):
# number = int(input("Enter NuMber"))
# list.append(number)
# sumOfList = sum(list)
# print("The sum of all numbers in the list is:", sumOfList)
# sumOfList()
# # EXCEPTION HANDLING
# # Write a program to handle division by zero exception
# try:
# num1 = int(input("Enter first number: "))
# num2 = int(input("Enter second number: "))
# result = num1 / num2
# print("The result is:", result)
# except ZeroDivisionError:
# print("Divisible by Zero is undefined")
# finally:
# print("in finally block")
# # Write a program to handle invalid input exception
# try:
# num1 , num2 = int(input("Enter numbers with gaps or commas"))
# result = num1 / num2
# print("The result is:", result)
# except ZeroDivisionError:
# print("Divisible by Zero is undefined")
# except ValueError:
# print("Invalid input! Please enter numeric values only.")
# except:
# print("Some other exception occurred")
# else:
# print("No exception occurred")
# finally:
# print("in finally block")
# Dictionary Operations
# Write a program to add a key-value pair to a dictionary
# def addKeyValuePair():
# my_dict = {}
# key = input("Enter key: ")
# value = input("Enter value: ")
# my_dict[key] = value
# print("Updated dictionary:", my_dict)
# addKeyValuePair()
# def addKeyValuePair(key, value):
# my_dict = {}
# my_dict[key] = value
# print("Updated dictionary:", my_dict)
# addKeyValuePair("name", "Alice")
# def addKeyValuePair(key, value):
# my_dict = {}
# my_dict[key] = value
# print("Updated dictionary:", my_dict)
# addKeyValuePair("name", "Alice")
# def makeDic():
# try:
# dict = {}
# diclength = int(input("Enter Dictionary length from 1-10"))
# if diclength <= 0 or diclength > 10:
# raise ValueError("value must be greater than zero and less than equals to 10")
# except ValueError as ve:
# print("Invalid input:", ve)
# else:
# for i in range(diclength):
# key, value = input("Enter key value separted by gaps or commas").split()
# dict[key] = value
# print("The dictionary is:", dict)
# makeDic()
# Write a program to remove a key-value pair from a dictionary
# def removeDicItem():
# dict = {"name": "hasnain", "age":18, "bodycount": 0}
# print(dict)
# item = input("Enter item you want to remove")
# if item in dict:
# del dict[item]
# print("Updates Dictionary", dict)
# removeDicItem()