forked from jmbharathram/executeoncommand
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path7_list_comprehension.py
More file actions
46 lines (25 loc) · 781 Bytes
/
7_list_comprehension.py
File metadata and controls
46 lines (25 loc) · 781 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
40
41
42
43
44
45
46
#[expression for item in iterable_object]
#Find the cubes of the even numbers between 0 and 10
#Using for loop
even_cubes = []
for num in range(0,11,2):
even_cubes.append(num**3)
print("Even Cubes:", even_cubes)
#Using Map
def calculate_cube(num):
return num**3
result = map(calculate_cube,range(0,11,2))
print(list(result))
#List comprehension
even_cubes = [ num**3 for num in range(0,11,2) ]
print("Even Cubes:", even_cubes)
#Using a function instead of expression
def calculate_power(num, power):
return num**power
result = [ calculate_power(num,3) for num in range(0,11,2) ]
print(result)
#Using if condition
def calculate_power(num, power):
return num**power
result = [ calculate_power(num,3) for num in range(0,11) if num % 2 == 0 ]
print(result)