-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlist_comp.py
More file actions
32 lines (21 loc) · 708 Bytes
/
list_comp.py
File metadata and controls
32 lines (21 loc) · 708 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
#####
# LIST COMPREHENSION
# Constructing new arrays from an iterable(lists, ranges, etc.)
#####
lst = [x for x in range(1, 6)]
print(lst)
lst2 = list(map(lambda x: x * 10, [i for i in range(1, 4)]))
print(lst2)
lst3 = list(range(1, 11))
print(lst3)
lst4 = list(map(lambda i: i * 2, range(1, 11)))
print(lst4)
### Square the values of even numbers between 1-100 and just print the odd ones, i.e [1, 4, 3, 16,...].
play = [x**2 if x % 2 == 0 else x for x in range(1, 101)]
print(play)
play2 = list(map(lambda i: i**2 if i % 2 == 0 else i, range(1, 101)))
print(play2)
work = [i for i in range(2, 100)]
print(work)
user_input = [int(input("Enter a grade: ")) for i in range(1, 6)]
print(user_input)