summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPrabhu Ramachandran2018-06-07 01:30:23 +0530
committerPrabhu Ramachandran2018-06-07 01:30:23 +0530
commitc05978bd1d5464988300eead1c1d8af98f732a42 (patch)
treeb6173edee2d8a0b66e695ef3718308403e44e875
parent3170893245a922fce640a64f0c3bc3f6009ae1e3 (diff)
downloadpython-workshops-c05978bd1d5464988300eead1c1d8af98f732a42.tar.gz
python-workshops-c05978bd1d5464988300eead1c1d8af98f732a42.tar.bz2
python-workshops-c05978bd1d5464988300eead1c1d8af98f732a42.zip
Fixing some mistakes in the slides.
-rw-r--r--advanced_python/16_generators.ipyml44
-rw-r--r--advanced_python/17_list_comprehensions.ipyml76
-rw-r--r--advanced_python/18_with_statement.ipyml39
3 files changed, 92 insertions, 67 deletions
diff --git a/advanced_python/16_generators.ipyml b/advanced_python/16_generators.ipyml
index b30162d..bf6e427 100644
--- a/advanced_python/16_generators.ipyml
+++ b/advanced_python/16_generators.ipyml
@@ -12,7 +12,6 @@ cells:
slideshow:
slide_type: slide
-
- markdown: |
## Background
@@ -95,7 +94,7 @@ cells:
next(c)
- code: |
- for i in counter(4):
+ for i in counter(5):
print(i)
metadata:
@@ -150,7 +149,6 @@ cells:
for i in range(n):
yield 2*i + 1
-
- markdown: |
## Exercise: Fibonacci generator
@@ -162,7 +160,7 @@ cells:
slide_type: slide
- code: |
- for x in fib(5):
+ for x in fib(10):
print(x)
- markdown: |
@@ -176,13 +174,10 @@ cells:
def fib(n):
a, b = 0, 1
yield 0
- for i in range(n-1)
+ for i in range(n-1):
yield b
a, b = b, a + b
-
-# --------------- Part 2 --------------
-
- markdown: |
## Creating iterable objects
@@ -230,7 +225,21 @@ cells:
self.animals = list(animals)
def __iter__(self):
- return self.animals
+ return iter(self.animals)
+
+- code: |
+ class Animal(object):
+ def __init__(self, name):
+ self.name = name
+ def greet(self):
+ print(self.name, "says hi!")
+
+ class Mammal(Animal):
+ pass
+
+ metadata:
+ slideshow:
+ slide_type: slide
- code: |
c = Animal('crow')
@@ -247,8 +256,8 @@ cells:
- markdown: |
## Observations
- - Just returns `self.animals`
- - `self.animals` is iterable as it is a list
+ - Just returns `iter(self.animals)`
+ - `self.animals` is a `list` and calling `iter` makes it an iterator
metadata:
slideshow:
@@ -283,7 +292,7 @@ cells:
slide_type: subslide
- code: |
- r = SillyRange(5)
+ r = SimpleRange(5)
for i in r:
print(i)
@@ -291,7 +300,6 @@ cells:
slideshow:
slide_type: subslide
-
- markdown: |
## Observations
@@ -304,7 +312,6 @@ cells:
slideshow:
slide_type: slide
-
- markdown: |
## Summary
@@ -317,9 +324,8 @@ cells:
slideshow:
slide_type: slide
-
- markdown: |
- ## Exercise: Fibonacci generator object
+ ## Exercise: Fibonacci object
Create a class that when instantiated with an argument `n` can be used as a
sequence of the first `n` numbers of the Fibonacci sequence, call this
@@ -357,7 +363,7 @@ cells:
return old
- code: |
- f = Fib(5)
+ f = Fib(10)
for i in f:
print(i)
@@ -365,10 +371,10 @@ cells:
slideshow:
slide_type: slide
-
# The lines below here may be deleted if you do not need them.
# ---------------------------------------------------------------------------
metadata:
+ celltoolbar: Slideshow
kernelspec:
display_name: Python 3
language: python
@@ -382,7 +388,7 @@ metadata:
name: python
nbconvert_exporter: python
pygments_lexer: ipython3
- version: 3.6.0
+ version: 3.5.2
rise:
scroll: true
transition: none
diff --git a/advanced_python/17_list_comprehensions.ipyml b/advanced_python/17_list_comprehensions.ipyml
index cb5434f..d560097 100644
--- a/advanced_python/17_list_comprehensions.ipyml
+++ b/advanced_python/17_list_comprehensions.ipyml
@@ -2,7 +2,7 @@ cells:
- markdown: |
# Advanced Python: List/dict comprehensions, generator expressions
-
+
### Prabhu Ramachandran
### The FOSSEE Python group &
### Department of Aerospace Engineering
@@ -14,7 +14,7 @@ cells:
- markdown: |
## Introduction
-
+
- A convenient way to quickly create lists
- Lets say we have the following
@@ -27,9 +27,11 @@ cells:
for i in range(5):
sqr.append(i*i)
+
- markdown: |
Is done easily with the following
+
- code: |
sqr = [i*i for i in range(5)]
sqr
@@ -38,10 +40,9 @@ cells:
slideshow:
slide_type: fragment
-
- markdown: |
## More examples
-
+
- Concise, readable, and quick
metadata:
@@ -51,20 +52,24 @@ cells:
- code: |
[2*x + 1 for x in range(5)]
+
- code: |
data = [-1, -2, 3, 4]
[abs(x) for x in data]
+
- code: |
names = ['Amar', 'Akbar', 'Antony']
[x.upper() for x in names]
+
- code: |
[(len(x), x) for x in names]
+
- markdown: |
## Filtering elements
-
+
- Filter elements using `if` clauses
metadata:
@@ -76,6 +81,7 @@ cells:
positive = [x for x in data if x > 0]
positive
+
- code: |
not_in_data = [x for x in range(-2, 5) if x not in data]
not_in_data
@@ -83,7 +89,7 @@ cells:
- markdown: |
## More complex comprehensions
-
+
- One can have many `for` and `if` clauses
metadata:
@@ -93,19 +99,26 @@ cells:
- code: |
[(i, j) for i in range(3) for j in range(2)]
+
- code: |
[(i, j) for i in range(3) for j in range(2) if i > j]
+
+
- code: |
[(i, j) for i in range(4) if i%2 for j in range(4) if i > j]
+
- code: |
[(i, j) for i in range(4) if i%2 for j in range(4) if (i > j or j%2)]
+
+
- code: |
[(i, j) for i in range(4) if i%2 for j in range(4) if i > j if j%2]
+
- markdown: |
## Observations
-
+
- Powerful and convenient.
- Can supply multiple `for` and `if` clauses
- Always create lists
@@ -119,7 +132,7 @@ cells:
- markdown: |
## Generator expressions
-
+
- Like list comprehensions but:
- do not create lists
- and create a generator
@@ -131,10 +144,11 @@ cells:
slide_type: slide
- code: |
- (i*i for i in range(5))
+ gexp = (i*i for i in range(5))
+
- code: |
- sum(i*i for i in range(5))
+ sum(gexp)
metadata:
slideshow:
@@ -150,7 +164,7 @@ cells:
- markdown: |
## More examples
-
+
- `zip` is a handy function
metadata:
@@ -162,21 +176,26 @@ cells:
b = [-1.5, -1.0, -0.5]
zip(a, b)
+
- code: |
list(zip(a, b))
+
- code: |
list(zip('hello', 'world'))
+
- code: |
list(zip(range(5), 'hello'))
+
- code: |
sum(x*y for x, y in zip(a, b))
+
- markdown: |
## Dictionary comprehensions
-
+
- Similar to list comprehensions
- Creates a dictionary easily
@@ -187,12 +206,14 @@ cells:
- code: |
{x: len(x) for x in ('cat', 'dog', 'mouse')}
+
- code: |
{x: x%2 == 0 for x in range(0, 5)}
+
- markdown: |
## Summary
-
+
- List comprehensions
- Generator expressions
- Dictionary comprehensions
@@ -204,7 +225,7 @@ cells:
- markdown: |
## Exercise: even number list comprehension
-
+
Write a simple list comprehension that generates the first 5 even
numbers as a list.
@@ -221,7 +242,7 @@ cells:
- 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$.
@@ -231,7 +252,7 @@ cells:
- code: |
from math import sin, pi
- [sin(i*2*pi) for i in range(0, 10) if sin(i*2*pi) > 0]
+ [sin(i*2*pi/9) for i in range(0, 10) if sin(i*2*pi/9) > 0]
metadata:
slideshow:
@@ -239,7 +260,7 @@ cells:
- code: |
# or
- r = [sin(i*2*pi) for i in range(0, 10)]
+ r = [sin(i*2*pi/9) for i in range(0, 10)]
[x for x in r if x > 0]
metadata:
@@ -248,7 +269,7 @@ cells:
- 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.
@@ -266,7 +287,7 @@ cells:
- 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.
@@ -276,7 +297,7 @@ cells:
- code: |
word = 'hello'
- set([x + y for x in word for y in word])
+ set(x + y for x in word for y in word)
metadata:
slideshow:
@@ -292,7 +313,7 @@ cells:
- 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.
@@ -309,7 +330,7 @@ cells:
- 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.
@@ -326,7 +347,7 @@ cells:
- 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'.
@@ -336,7 +357,7 @@ cells:
slide_type: slide
- code: |
- {x: i for i, x in enumerate('mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun') }
+ {x: i for i, x in enumerate(('mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun'))}
metadata:
slideshow:
@@ -352,7 +373,7 @@ cells:
- 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$.
@@ -362,7 +383,7 @@ cells:
- code: |
from math import sin, pi
- max(sin(i*2*pi) for i in range(0, 100))
+ max(sin(i*2*pi/99) for i in range(0, 100))
metadata:
slideshow:
@@ -384,9 +405,10 @@ metadata:
name: python
nbconvert_exporter: python
pygments_lexer: ipython3
- version: 3.6.0
+ version: 3.5.2
rise:
scroll: true
transition: none
nbformat: 4
nbformat_minor: 2
+
diff --git a/advanced_python/18_with_statement.ipyml b/advanced_python/18_with_statement.ipyml
index b231d39..57ccc38 100644
--- a/advanced_python/18_with_statement.ipyml
+++ b/advanced_python/18_with_statement.ipyml
@@ -2,7 +2,7 @@ cells:
- markdown: |
# Advanced Python: `with` statements
-
+
### Prabhu Ramachandran
### The FOSSEE Python group &
### Department of Aerospace Engineering
@@ -14,7 +14,7 @@ cells:
- markdown: |
## Introduction
-
+
- `with` statement is used as follows
metadata:
@@ -26,19 +26,17 @@ cells:
for line in f:
print(line, end='')
- id: 0
- code: |
f.closed
- id: 1
metadata:
slideshow:
slide_type: fragment
- markdown: |
## Observations
-
+
- Creates a context
- `f` is automatically closed
- Works even if there was an exception!
@@ -49,7 +47,7 @@ cells:
- markdown: |
## Use cases
-
+
- Use it with locks (for threaded computing)
- Database connections
- File objects
@@ -60,7 +58,7 @@ cells:
- markdown: |
## Creating your own
-
+
- Relatively easy to do
metadata:
@@ -69,7 +67,7 @@ cells:
- code: |
from contextlib import contextmanager
-
+
@contextmanager
def context():
print("setup")
@@ -78,23 +76,23 @@ cells:
finally:
print("done")
+
- code: |
with context():
print("do something")
+
- markdown: |
## A more complex example
-
metadata:
slideshow:
slide_type: slide
-
- code: |
import os
from contextlib import contextmanager
-
+
@contextmanager
def tempfile(filename):
f = open(filename, 'w')
@@ -104,6 +102,11 @@ cells:
f.close()
os.remove(filename)
+
+- code: |
+ %ls junk.txt
+
+
- code: |
with tempfile('junk.txt') as f:
f.write('hello world\n')
@@ -114,8 +117,8 @@ cells:
slide_type: slide
- markdown: |
- ## Comments
-
+ ## Summary
+
- Useful feature
- Can define an object supporting this
- Overload, `__enter__` and `__exit__`
@@ -125,11 +128,9 @@ cells:
slideshow:
slide_type: slide
-
-
- markdown: |
## More information
-
+
- https://www.python.org/dev/peps/pep-0343
- [With statement](https://docs.python.org/3/reference/datamodel.html#with-statement-context-managers)
- [Context manager types](https://docs.python.org/3/library/stdtypes.html#typecontextmanager)
@@ -154,14 +155,10 @@ metadata:
name: python
nbconvert_exporter: python
pygments_lexer: ipython3
- version: 3.6.0
+ version: 3.5.2
rise:
scroll: true
transition: none
nbformat: 4
nbformat_minor: 2
-# ---------------------------------------------------------------------------
-data:
- [{execution_count: null, outputs: []}, {execution_count: null, outputs: []}, {execution_count: null,
- outputs: []}, {execution_count: null, outputs: []}, {execution_count: null, outputs: []}]