From e7fb56d245dc5fa0ef656da7c8bd65723c660b75 Mon Sep 17 00:00:00 2001 From: wxmlong2012 <44708161+wxmlong2012@users.noreply.github.com> Date: Sat, 16 Jul 2022 22:18:23 -0400 Subject: [PATCH] Add files via upload --- ch1_data_model.py | 52 +++++++++++++++++++ ch2_sequence.py | 124 ++++++++++++++++++++++++++++++++++++++++++++++ ch3_dictionary.py | 33 ++++++++++++ 3 files changed, 209 insertions(+) create mode 100644 ch1_data_model.py create mode 100644 ch2_sequence.py create mode 100644 ch3_dictionary.py diff --git a/ch1_data_model.py b/ch1_data_model.py new file mode 100644 index 0000000..1bc6e22 --- /dev/null +++ b/ch1_data_model.py @@ -0,0 +1,52 @@ +import collections +from random import choice + +Card = collections.namedtuple('Card', ['rank', 'suit']) + +class FrenchDeck: + ranks = [str(n) for n in range(2, 11)] + list('JQKA') + suits = 'spades diamonds clubs hearts'.split() + + def __init__(self): + self._cards = [Card(rank, suit) for suit in self.suits + for rank in self.ranks] + + def __len__(self): + return len(self._cards) + + def __getitem__(self, position): + return self._cards[position] + + +deck = FrenchDeck() +choice(deck) +set() + + + + + +import math + +class Vector: + + def __init__(self, x=0, y=0): + self.x = x + self.y = y + + def __repr__(self): + return f'Vector({self.x!r}, {self.y!r})' + + def __abs__(self): + return math.hypot(self.x, self.y) + + def __bool__(self): + return bool(abs(self)) + + def __add__(self, other): + x = self.x + other.x + y = self.y + other.y + return Vector(x, y) + + def __mul__(self, scalar): + return Vector(self.x * scalar, self.y * scalar) \ No newline at end of file diff --git a/ch2_sequence.py b/ch2_sequence.py new file mode 100644 index 0000000..fe13990 --- /dev/null +++ b/ch2_sequence.py @@ -0,0 +1,124 @@ +x = "ABC" +codes = [ord(x) for x in x] + +[last := ord(c) for c in x] +last +c +symbols = "$CFGD" +[c for c in map(ord, symbols)] +list(filter(lambda x: x > 67, map(ord, symbols))) + +colors = ['black', 'white'] +sizes = ['S', 'M', 'L'] +tshirts = [(color, size) for color in colors + for size in sizes] +tshirts + +# ---------- Generator expression --------------- +# Generator expression saves memory, it won't need to create the whole list +# Here the number is created one by one to feed the tuple, the whole list is not created +symbol_tuple = tuple(ord(symbol) for symbol in symbols) +import array +array.array("I", (ord(symbol) for symbol in symbols)) + +# same as above here the whole list is never created, the generator expression feeds the for loop producing one item at a time. +for tshirt in (f"{c} {s}" for c in colors for s in sizes): + print(tshirt) + +# --------- unpack tuple ------------- +traveler_ids = [('USA', '31195855'), ('BRA', 'CE342567'), ('ESP', 'XDA205856')] +# The % formatting operator understands tuples and treats each item as a separate field. +for passport in sorted(traveler_ids): + print("%s/%s" % passport) + +for country, _ in traveler_ids: + print(country) + +hash("1234") +hash("12345") +t1 = (1,2,3) +t2 = (4,5,6) +t1*=2 + +l1 = [1,2,3] +l1.reverse() + +tshirts.sort(key = lambda x: x[0]) +tshirts.sort(key = lambda x: x[1]) +tshirts + +# Using * to Grab Excess Items +a, b, *rest = range(5) +a, *rest, c, d = range(10) + +# Unpacking with * in Function Calls and Sequence Literals +def func(a, b, c, d, *rest): + return a, b, c, d, rest + + +func(*[1,2], 3, *range(4,7)) + +# * can be used to define list, tuple, set +*range(3), 4 +*[2,3], 4 +[*range(3), 4] +{*range(3), 4, *(5,6,7)} + +metro_areas = [ + ('Tokyo', 'JP', 36.933, (35.689722, 139.691667)), + ('Delhi NCR', 'IN', 21.935, (28.613889, 77.208889)), + ('Mexico City', 'MX', 20.142, (19.433333, -99.133333)), + ('New York-Newark', 'US', 20.104, (40.808611, -74.020386)), + ('São Paulo', 'BR', 19.649, (-23.547778, -46.635833)), +] + +def main(): + print(f'{"":15} | {"latitude":>9} | {"longitude":>9}') + for name, _, _, (lat, lon) in metro_areas: + if lon <= 0: + print(f'{name:15} | {lat:9.4f} | {lon:9.4f}') + +if __name__ == '__main__': + main() + + +print(f'{2000:*20} | {"latitude":>9} | {"longtitude":>9}') + +a = 0.12355476 +b = 12.23459 +print(f"percent = {a:.2%}, two digit = {b:.2f}") +print(f"percent = {a:.2%}, two digit = {b:4.3f}") +print(f"percent = {a:.2%}, two digit = {'b'!s}") + + +invoice = """ +0.....6.................................40........52...55........ +1909 Pimoroni PiBrella $17.50 3 $52.50 +1489 6mm Tactile Switch x20 $4.95 2 $9.90 +1510 Panavise Jr. - PV-201 $28.00 1 $28.00 +1601 PiTFT Mini Kit 320x240 $34.95 1 $34.95 +""" + +line_items = invoice.split("\n")[2:] +unit_price = slice(40, 52) +description = slice(6, 40) +for item in line_items: + print(item[description], item[unit_price]) + +l = list(range(10)) +l[2:5] = [10,20,30,40] +l + +my_list = [[]] * 3 +my_list + +t = (1, 2, [30, 40]) +t[2] += [50, 60] +t + + + + + + + diff --git a/ch3_dictionary.py b/ch3_dictionary.py new file mode 100644 index 0000000..143d3ec --- /dev/null +++ b/ch3_dictionary.py @@ -0,0 +1,33 @@ + + +dial_codes = [ + (880, 'Bangladesh'), + (55, 'Brazil'), + (86, 'China'), + (91, 'India'), + (62, 'Indonesia'), + (81, 'Japan'), + (234, 'Nigeria'), + (92, 'Pakistan'), + (7, 'Russia'), + (1, 'United States'), +] + +country_dial = {country: dial for dial, country in dial_codes} +print(country_dial) + +# Sorting country_dial by name, reversing the pairs again, uppercasing values, and filtering items with code < 70 +{code: country.upper() for country, code in sorted(country_dial.items()) + if code < 70} + + +test_dict = {'a': 0, **{'x': 1}, 'y': 2, **{'z': 3, 'x': 4}} + + +# dict.get(key, default) can be used to avoid KeyError +test_dict.get("b", []) +# setdefault(k, default) +test_dict.setdefault("b", []).append((10,20)) + + +