forked from sagarsalgar29/python_baseline
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomprehension.py
More file actions
24 lines (18 loc) · 834 Bytes
/
comprehension.py
File metadata and controls
24 lines (18 loc) · 834 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
def main():
try:
squared_numbers = [x ** 2 for x in range(1, 6)]
square_dict = {x: x ** 2 for x in range(1, 6)}
even_squares_set = {x ** 2 for x in range(1, 11) if x % 2 == 0}
square_generator = (x ** 2 for x in range(1, 6))
print("List Comprehension (Squared Numbers):", squared_numbers)
print("Dictionary Comprehension (Square Dictionary):", square_dict)
print("Set Comprehension (Even Squares Set):", even_squares_set)
print("Generator Comprehension (Square Generator):", list(square_generator))
except ValueError as ve:
print(f"ValueError: {ve}")
except ZeroDivisionError as zde:
print(f"ZeroDivisionError: {zde}")
except Exception as e:
print(f"An unexpected error occurred: {e}")
if __name__ == "__main__":
main()