Skip to content

Commit 98eef7b

Browse files
author
codehouseindia
authored
Merge pull request codehouseindia#470 from tyadav4268/patch-13
Create Use_of_list-comprehension
2 parents 5ed89ee + 1ff3283 commit 98eef7b

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

Use_of_list-comprehension

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# List Comprehension is a way to shorten our code and create a list
2+
3+
# This following code creates a list of squares of numbers from 1 to n
4+
n = 10
5+
list_1 = [n**2 for n in range(1,n+1)]
6+
print(list_1) # output: [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
7+
8+
# The following code create a list of even numbers upto n
9+
list_2 = [n for n in range(n) if n%2 == 0]
10+
print(list_2) # output: [0, 2, 4, 6, 8]
11+
12+
# The following code create a list of odd numbers upto n
13+
list_3 = [n for n in range(n) if n%2]
14+
print(list_3) # output: [1, 3, 5, 7, 9]

0 commit comments

Comments
 (0)