summaryrefslogtreecommitdiff
path: root/advanced_python
diff options
context:
space:
mode:
authorPrabhu Ramachandran2018-05-19 14:00:04 +0530
committerPrabhu Ramachandran2018-05-19 14:00:04 +0530
commit89de5dd28d80d20ff41a70594d930d5adaf3f57e (patch)
tree0903ec28aaa303235a4ff19c79938f2d10561dc2 /advanced_python
parent93e8e112a67dd1122fd7aaffdb044a202fe80107 (diff)
downloadpython-workshops-89de5dd28d80d20ff41a70594d930d5adaf3f57e.tar.gz
python-workshops-89de5dd28d80d20ff41a70594d930d5adaf3f57e.tar.bz2
python-workshops-89de5dd28d80d20ff41a70594d930d5adaf3f57e.zip
Add several exercises for the list comprehensions.
Diffstat (limited to 'advanced_python')
-rw-r--r--advanced_python/17_list_comprehensions.ipyml164
1 files changed, 164 insertions, 0 deletions
diff --git a/advanced_python/17_list_comprehensions.ipyml b/advanced_python/17_list_comprehensions.ipyml
index a300331..cb5434f 100644
--- a/advanced_python/17_list_comprehensions.ipyml
+++ b/advanced_python/17_list_comprehensions.ipyml
@@ -202,7 +202,171 @@ cells:
slideshow:
slide_type: slide
+- markdown: |
+ ## Exercise: even number list comprehension
+
+ Write a simple list comprehension that generates the first 5 even
+ numbers as a list.
+
+ metadata:
+ slideshow:
+ slide_type: slide
+
+- code: |
+ [2*i for i in range(1, 6)]
+
+ metadata:
+ slideshow:
+ slide_type: fragment
+
+- markdown: |
+ ## Exercise: positive `sin` values
+
+ Write a list comprehension that generates only positive values of the
+ `math.sin` when applied to the first 10 values between $0$ to $2 \pi$.
+
+ metadata:
+ slideshow:
+ slide_type: slide
+
+- code: |
+ from math import sin, pi
+ [sin(i*2*pi) for i in range(0, 10) if sin(i*2*pi) > 0]
+
+ metadata:
+ slideshow:
+ slide_type: fragment
+
+- code: |
+ # or
+ r = [sin(i*2*pi) for i in range(0, 10)]
+ [x for x in r if x > 0]
+
+ metadata:
+ slideshow:
+ slide_type: fragment
+
+- markdown: |
+ ## Exercise: two letter permutations of 'hello'
+
+ Using a list comprehension, write all possible 2 letter permutations of
+ the word 'hello', do not worry about repetitions.
+
+ metadata:
+ slideshow:
+ slide_type: slide
+
+- code: |
+ word = 'hello'
+ [x + y for x in word for y in word]
+
+ metadata:
+ slideshow:
+ slide_type: fragment
+
+- markdown: |
+ ## Exercise: unique two letter permutations of 'hello'
+
+ Write all possible 2 letter permutations of the word 'hello', this time
+ show only the unique ones.
+
+ metadata:
+ slideshow:
+ slide_type: slide
+
+- code: |
+ word = 'hello'
+ set([x + y for x in word for y in word])
+
+ metadata:
+ slideshow:
+ slide_type: fragment
+
+- code: |
+ # Or better to use a generator expression
+ set(x + y for x in word for y in word)
+
+ metadata:
+ slideshow:
+ slide_type: fragment
+
+- markdown: |
+ ## Exercise: list of tuples
+
+ Using a list comprehension, create a list of tuples of the form `(x, x*x,
+ x*x*x)` for the first 5 integers starting at 1.
+
+ metadata:
+ slideshow:
+ slide_type: slide
+
+- code: |
+ [(x, x*x, x*x*x) for x in range(1, 6)]
+
+ metadata:
+ slideshow:
+ slide_type: fragment
+
+- markdown: |
+ ## Exercise: simple dictionary comprehension
+
+ Create a dictionary with the keys being the lowercase characters of the
+ English alphabet and the value being an empty list.
+
+ metadata:
+ slideshow:
+ slide_type: slide
+
+- code: |
+ {x: [] for x in 'abcdefghijklmnopqrstuvwxyz'}
+
+ metadata:
+ slideshow:
+ slide_type: fragment
+
+- markdown: |
+ ## Exercise: another simple dict comprehension
+
+ Create a dictionary with the keys being the lowercase words (only the
+ first three characters) denoting the day of the week and the value being
+ an integer corresponding to each with 0 being 'mon' and 6 'sun'.
+
+ metadata:
+ slideshow:
+ slide_type: slide
+
+- code: |
+ {x: i for i, x in enumerate('mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun') }
+
+ metadata:
+ slideshow:
+ slide_type: fragment
+
+- code: |
+ # Or
+ {x: i for i, x in enumerate('mon tue wed thu fri sat sun'.split()) }
+
+ metadata:
+ slideshow:
+ slide_type: fragment
+
+- markdown: |
+ ## Exercise: max `sin` generator expression
+
+ Using a generator expression find the maximum value of `math.sin` when
+ applied to the first 100 values between $0$ to $2 \pi$.
+ metadata:
+ slideshow:
+ slide_type: slide
+
+- code: |
+ from math import sin, pi
+ max(sin(i*2*pi) for i in range(0, 100))
+
+ metadata:
+ slideshow:
+ slide_type: fragment
# The lines below here may be deleted if you do not need them.
# ---------------------------------------------------------------------------