-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add list comprehension tutorial (#131)
* add list comprehension tutorial * Update README.md Co-authored-by: Sarah Abderemane <[email protected]> * Update lists/comprehension.py Co-authored-by: Sarah Abderemane <[email protected]> * Update lists/comprehension.py Co-authored-by: Sarah Abderemane <[email protected]> * Update lists/comprehension.py Co-authored-by: Sarah Abderemane <[email protected]> * Update lists/comprehension.py Co-authored-by: Sarah Abderemane <[email protected]> * fix challenge steps * rename file for consistency * fix trailing space Co-authored-by: Sarah Abderemane <[email protected]>
- Loading branch information
1 parent
87ae8ec
commit 6fb8c43
Showing
2 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# ------------------------------------------------------------------------------------ | ||
# Tutorial: List comprehension | ||
# ------------------------------------------------------------------------------------ | ||
# It's a Python feature to iterate and create a list from some other list in a | ||
# shorter and declarative way than loops. | ||
|
||
|
||
# In general there is a very common for loop structure that looks like this: | ||
for_loop_squares = [] | ||
|
||
for i in range(10): | ||
for_loop_squares.append(i * i) | ||
|
||
print(for_loop_squares) # [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] | ||
|
||
|
||
# With list comprehension you can do the exact same thing in one line. | ||
# the syntax is: | ||
# new_list = [expression for member in iterable] | ||
comprehension_squares = [x * x for x in range(10)] | ||
|
||
print(comprehension_squares) # [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] | ||
|
||
|
||
# You can even add conditionals to the list comprehension, the condition is place at the end of the list comprehension, just like that: | ||
# new_list = [expression for member in iterable if condition] | ||
comprehension_even_squares_numbers = [x * x for x in range(10) if x % 2 == 0] | ||
|
||
print(comprehension_even_squares_numbers) # [0, 4, 16, 36, 64] | ||
|
||
|
||
# But if you can do if/else logic, you need to place the condition before the for loop just like that: | ||
comprehension_even_squares_numbers_and_odd_numbers_is_zero = [x * x if x % 2 == 0 else 0 for x in range(10)] | ||
|
||
print(comprehension_even_squares_numbers_and_odd_numbers_is_zero) # [0, 0, 4, 0, 16, 0, 36, 0, 64, 0] | ||
|
||
|
||
# We can summarize the 3 formats of list comprehension. The basic format of a list comprehension is like: | ||
# new_list = [expression for member in iterable] | ||
|
||
# With only one condition | ||
# new_list = [expression for member in iterable if condition] | ||
|
||
# With if/else logic: | ||
# new_list = [expression if condition else other_expression for x in sequence] | ||
|
||
|
||
# ------------------------------------------------------------------------------------ | ||
# Challenge: FIZZBUZZ | ||
# Create a list comprehesion that returns the numbers from 0 to 100 but: | ||
# 1 -. if the number is divisible by 3 and 5, append to the list the word "FizzBuzz" | ||
# 2 -. else, append to the list the number | ||
# ------------------------------------------------------------------------------------ |