From 6fb8c436ee7d9f0636625384ebb129faeb38bc51 Mon Sep 17 00:00:00 2001 From: jrafaaael <70046023+jrafaaael@users.noreply.github.com> Date: Thu, 6 Oct 2022 17:00:46 -0400 Subject: [PATCH] feat: add list comprehension tutorial (#131) * add list comprehension tutorial * Update README.md Co-authored-by: Sarah Abderemane * Update lists/comprehension.py Co-authored-by: Sarah Abderemane * Update lists/comprehension.py Co-authored-by: Sarah Abderemane * Update lists/comprehension.py Co-authored-by: Sarah Abderemane * Update lists/comprehension.py Co-authored-by: Sarah Abderemane * fix challenge steps * rename file for consistency * fix trailing space Co-authored-by: Sarah Abderemane --- README.md | 1 + lists/list-comprehension.py | 53 +++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 lists/list-comprehension.py diff --git a/README.md b/README.md index 8c38ad0..6529e41 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,7 @@ Each file contains: |How to alter a list | [alterning-lists.py](lists/altering-lists.py) | |How to declare a list | [declaring-lists.py](lists/declaring-lists.py) | |How to index a list | [indexing-lists.py](lists/indexing-lists.py) | +|List comprehension | [list-comprehension.py](lists/list-comprehension.py) | | **Conditions** | |How to make conditions | [if-else.py](conditions/if-else.py) | | **Loops** | diff --git a/lists/list-comprehension.py b/lists/list-comprehension.py new file mode 100644 index 0000000..e0ef4f1 --- /dev/null +++ b/lists/list-comprehension.py @@ -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 +# ------------------------------------------------------------------------------------