-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomprehensionss.py
More file actions
39 lines (26 loc) · 931 Bytes
/
comprehensionss.py
File metadata and controls
39 lines (26 loc) · 931 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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#print list containing numbers divisible by 3
# ls=[i for i in range(100) if i%3==0]
# print(ls)
#Dict comprehension
# dict1={i:f"item{i}" for i in range(100)}
# dict1={value:key for key,value in dict1.items()} # it will get reversed
# print(dict1)
#Set comprehension
# dress={dress for dress in ["dress1","dress2","dress1","dress2","dress1","dress2"]}
# print(dress)
#generator type comprehension
# evens=(i for i in range(100) if i%2==0)
# print(type(evens)) # It is a generator type as it is YIELDING values
# # print(evens)
# # print(evens.__next__())
# for item in evens:
# print(item)
# little exercise
a=int(input("Enter the number of items you want\n"))
b=int(input("Enter 1 for list, 2 for dict, 3 for set\n"))
list1=[i for i in range(a) if b==1]
print(list1)
dict1={i:f"item{i}" for i in range(a) if b==2}
print(dict1)
set1={i for i in range(a) if b==3}
print(set1)