<jupyter_start><jupyter_text>Example 2-1. Build a list of Unicode codepoints from a string<jupyter_code>symbols = '$@#%^&*'
codes = []
for symbol in symbols:
    codes.append(ord(symbol))

codes<jupyter_output><empty_output><jupyter_text>Example 2-2. Build a list of Unicode codepoints from a string, using a listcomp<jupyter_code>symbols = '$@#%^&*'
codes = [ord(symbol) for symbol in symbols]

codes<jupyter_output><empty_output><jupyter_text>Listcomps Versus map and filter Example 2-3. The same list built by a listcomp and a map/filter composition<jupyter_code>symbols = '$¢£¥€¤'
beyond_ascii = [ord(s) for s in symbols if ord(s) > 127]
beyond_ascii
symbols = '$¢£¥€¤'
beyond_ascii = list(filter(lambda c: c > 127, map(ord, symbols)))
beyond_ascii<jupyter_output><empty_output><jupyter_text>Example 2-4. Cartesian product using a list comprehension<jupyter_code>colors = ['red', 'black', 'green', 'yellow']
sizes = ['xs', 's', 'm', 'l', 'xl' , 'xxl']

tshirts = [(color, size) for color in colors for size in sizes]
tshirts
tshirts = [(color, size) for size in sizes
                         for color in colors]

tshirts<jupyter_output><empty_output><jupyter_text>Example 2-5 shows basic usage of genexps to build a tuple and an array.<jupyter_code>import collections
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) -> None:
        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]<jupyter_output><empty_output><jupyter_text>Example 2-5. Initializing a tuple and an array from a generator expression<jupyter_code>import array

symbols = '$¢£¥€¤'
tuple(ord(symbol) for symbol in symbols)

array.array('I', (ord(symbol) for symbol in symbols))<jupyter_output><empty_output><jupyter_text>Example 2-6. Cartesian product in a generator expression<jupyter_code>colors = ['black', 'white']
sizes = ['S', 'M', 'L']
for tshirt in (f'{c} {s}' for c in colors for s in sizes):
    print(tshirt)
    
lax_coordinates = (33.9425, -118.408056)
city, year, pop, chg, area = ('Ardabil', 2020, 32_450, 0.66, 8014)
travelers_ids = [('AZER', '510608921'), 
                 ('TURK', 'TR964585'),
                 ('SAKHA', 'SKH86954758'),
                 ]

for passport in sorted(travelers_ids):
    print('%s/%s' % passport)
for country, _ in travelers_ids:
    print(country)<jupyter_output>AZER
TURK
SAKHA<jupyter_text>Unpacking with * in function calls and sequence literals<jupyter_code>def fun(a, b, c, d, *rest):
    return a, b, c, d, rest

fun(*[1, 2], 3, *range(4,7))
*range(4), 4<jupyter_output><empty_output><jupyter_text>Example 2-8. Unpacking nested tuples to access the longitude<jupyter_code>metro_areas = [
('Ardabil', 'IR', 36.933, (35.689722, 139.691667)),
('Tabriz', 'IR', 21.935, (28.613889, 77.208889)),
('Istanbul', 'TR', 20.142, (19.433333, -99.133333)),
('Baku', 'AZ', 20.104, (40.808611, -74.020386)),
('Gharabagh', 'AZ', 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()<jupyter_output>|  latitude | longitude
Istanbul        |   19.4333 |  -99.1333
Baku            |   40.8086 |  -74.0204
Gharabagh       |  -23.5478 |  -46.6358
{'fraction_non_alphanumeric': 0.15837228485015123, 'fraction_numerical': 0.06571350013747594, 'mean_word_length': 3.949659863945578, 'pattern_counts': {'":': 3, '<': 36, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '642783', 'license': 'MIT', 'n_tokens_mistral': 1522, 'n_tokens_neox': 1349, 'n_words': 348, 'path': 'chapter 2-Part II. Data Structures/example 2-1-to-8-List Comprehensions and Readability.ipynb', 'repo_name': 'Jalilnkh/KartalOl-Fluent-Python-2022', 'size': '3637'}