Skip to content

Commit 8b802c5

Browse files
committed
ch03
1 parent 583ec6a commit 8b802c5

34 files changed

+332
-0
lines changed

ch03/any.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
items = [0, None, 0.0, True, 0, 7] # True and 7 evaluate to True
2+
3+
found = False # this is called "flag"
4+
for item in items:
5+
print('scanning item', item)
6+
if item:
7+
found = True # we update the flag
8+
break
9+
10+
if found: # we inspect the flag
11+
print('At least one item evaluates to True')
12+
else:
13+
print('All items evaluate to False')

ch03/binary.2.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
n = 39
2+
remainders = []
3+
while n > 0:
4+
n, remainder = divmod(n, 2)
5+
remainders.append(remainder)
6+
7+
remainders.reverse()
8+
print(remainders)

ch03/binary.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""
2+
6 / 2 = 3 (remainder: 0)
3+
3 / 2 = 1 (remainder: 1)
4+
1 / 2 = 0 (remainder: 1)
5+
List of remainders: 0, 1, 1.
6+
Reversed is 1, 1, 0, which is also the binary repres. of 6: 110
7+
"""
8+
9+
n = 39
10+
remainders = []
11+
while n > 0:
12+
remainder = n % 2 # remainder of division by 2
13+
remainders.append(remainder) # we keep track of remainders
14+
n //= 2 # we divide n by 2
15+
16+
remainders.reverse()
17+
print(remainders)

ch03/compress.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from itertools import compress
2+
data = range(10)
3+
even_selector = [1, 0] * 10
4+
odd_selector = [0, 1] * 10
5+
6+
even_numbers = list(compress(data, even_selector))
7+
odd_numbers = list(compress(data, odd_selector))
8+
9+
print(odd_selector)
10+
print(list(data))
11+
print(even_numbers)
12+
print(odd_numbers)

ch03/conditional.1.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
late = True
2+
if late:
3+
print('I need to call my manager!')

ch03/conditional.2.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
late = False
2+
if late:
3+
print('I need to call my manager!') #1
4+
else:
5+
print('no need to call my manager...') #2

ch03/coupons.dict.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
customers = [
2+
dict(id=1, total=200, coupon_code='F20'), # F20: fixed, £20
3+
dict(id=2, total=150, coupon_code='P30'), # P30: percent, 30%
4+
dict(id=3, total=100, coupon_code='P50'), # P50: percent, 50%
5+
dict(id=4, total=110, coupon_code='F15'), # F15: fixed, £15
6+
]
7+
discounts = {
8+
'F20': (0.0, 20.0), # each value is (percent, fixed)
9+
'P30': (0.3, 0.0),
10+
'P50': (0.5, 0.0),
11+
'F15': (0.0, 15.0),
12+
}
13+
for customer in customers:
14+
code = customer['coupon_code']
15+
percent, fixed = discounts.get(code, (0.0, 0.0))
16+
customer['discount'] = percent * customer['total'] + fixed
17+
18+
for customer in customers:
19+
print(customer['id'], customer['total'], customer['discount'])

ch03/coupons.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
customers = [
2+
dict(id=1, total=200, coupon_code='F20'), # F20: fixed, £20
3+
dict(id=2, total=150, coupon_code='P30'), # P30: percent, 30%
4+
dict(id=3, total=100, coupon_code='P50'), # P50: percent, 50%
5+
dict(id=4, total=110, coupon_code='F15'), # F15: fixed, £15
6+
]
7+
for customer in customers:
8+
code = customer['coupon_code']
9+
if code == 'F20':
10+
customer['discount'] = 20.0
11+
elif code == 'F15':
12+
customer['discount'] = 15.0
13+
elif code == 'P30':
14+
customer['discount'] = customer['total'] * 0.3
15+
elif code == 'P50':
16+
customer['discount'] = customer['total'] * 0.5
17+
else:
18+
customer['discount'] = 0.0
19+
20+
for customer in customers:
21+
print(customer['id'], customer['total'], customer['discount'])

ch03/discount.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from datetime import date, timedelta
2+
3+
today = date.today()
4+
tomorrow = today + timedelta(days=1) # today + 1 day is tomorrow
5+
products = [
6+
{'sku': '1', 'expiration_date': today, 'price': 100.0},
7+
{'sku': '2', 'expiration_date': tomorrow, 'price': 50},
8+
{'sku': '3', 'expiration_date': today, 'price': 20},
9+
]
10+
11+
for product in products:
12+
if product['expiration_date'] != today:
13+
continue
14+
product['price'] *= 0.8 # equivalent to applying 20% discount
15+
print(
16+
'Price for sku', product['sku'],
17+
'is now', product['price'])

ch03/errorsalert.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# The send_email function is defined here to enable you to play with
2+
# code, but of course it doesn't send an actual email.
3+
def send_email(*a):
4+
print (*a)
5+
6+
alert_system = 'console' # other value can be 'email'
7+
error_severity = 'critical' # other values: 'medium' or 'low'
8+
error_message = 'OMG! Something terrible happened!'
9+
10+
if alert_system == 'console':
11+
print(error_message) #1
12+
elif alert_system == 'email':
13+
if error_severity == 'critical':
14+
send_email('[email protected]', error_message) #2
15+
elif error_severity == 'medium':
16+
send_email('[email protected]', error_message) #3
17+
else:
18+
send_email('[email protected]', error_message) #4

0 commit comments

Comments
 (0)