<jupyter_start><jupyter_text>The Python Notebook 🐢 <NAME>, MSIT 👨‍💻This notebook was created from the Python Bootcamp- [Github](https://github.com/ninjakwarl/DXCPythonBootCamp)- [Jupyter Notebook](https://mybinder.org/v2/gh/ninjakwarl/DXCPythonBootCamp/main?filepath=notebooks)<jupyter_code>import this<jupyter_output>The Zen of Python, by <NAME>

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!<jupyter_text>Python Basics Math OperatorsFrom **Highest** to **Lowest** precedence:| Operators | Operation        | Example         || --------- | ---------------- | --------------- || **        | Exponent         | `2 ** 3 = 8`    || %         | Modulus/Remaider | `22 % 8 = 6`    || //        | Integer division | `22 // 8 = 2`   || /         | Division         | `22 / 8 = 2.75` || *         | Multiplication   | `3 * 3 = 9`     || -         | Subtraction      | `5 - 2 = 3`     || +         | Addition         | `2 + 2 = 4`     |Examples of expressions in the interactive shell:<jupyter_code>2 + 3 * 6
(2 + 3) * 6
2 ** 8
23 // 7
23 % 7
(5 - 1) * ((7 + 1) / (3 - 1))<jupyter_output><empty_output><jupyter_text>Data Types| Data Type              | Examples                                  || ---------------------- | ----------------------------------------- || Integers               | `-2, -1, 0, 1, 2, 3, 4, 5`                || Floating-point numbers | `-1.25, -1.0, --0.5, 0.0, 0.5, 1.0, 1.25` || Strings                | `'a', 'aa', 'aaa', 'Hello!', '11 cats'`   | String Concatenation and ReplicationString concatenation:<jupyter_code>'Alice' 'Bob'<jupyter_output><empty_output><jupyter_text>Note: Avoid `+` operator for string concatenation. Prefer string formatting.String Replication:<jupyter_code>'Alice' * 5<jupyter_output><empty_output><jupyter_text>VariablesYou can name a variable anything as long as it obeys the following three rules:1. It can be only one word.2. It can use only letters, numbers, and the underscore (`_`) character.3. It can’t begin with a number.4. Variable name starting with an underscore (`_`) are considered as "unuseful`.Example:<jupyter_code>spam = 'Hello'
_spam = 'Hello'<jupyter_output><empty_output><jupyter_text>`_spam` should not be used again in the code. CommentsInline comment:<jupyter_code># This is a comment<jupyter_output><empty_output><jupyter_text>Multiline comment:<jupyter_code># This is a
# multiline comment<jupyter_output><empty_output><jupyter_text>Code with a comment:<jupyter_code>a = 1  # initialization<jupyter_output><empty_output><jupyter_text>Please note the two spaces in front of the comment.Function docstring:<jupyter_code>def foo():
    """
    This is a function docstring
    You can also use:
    ''' Function Docstring '''
    """<jupyter_output><empty_output><jupyter_text>The print Function<jupyter_code>print('Hello world!')
a = 1
print('Hello world!', a)<jupyter_output>Hello world! 1<jupyter_text>The input FunctionExample Code:<jupyter_code>print('What is your name?')   # ask for their name
myName = input()
print('It is good to meet you, {}'.format(myName))<jupyter_output>What is your name?
It is good to meet you,<jupyter_text>The len FunctionEvaluates to the integer value of the number of characters in a string:<jupyter_code>len('hello')<jupyter_output><empty_output><jupyter_text>Note: test of emptiness of strings, lists, dictionary, etc, should **not** use len, but prefer directboolean evaluation.<jupyter_code>a = [1, 2, 3]
if a:
    print("the list is not empty!")<jupyter_output>the list is not empty!<jupyter_text>The str, int, and float FunctionsInteger to String or Float:<jupyter_code>str(29)
print('I am {} years old.'.format(str(29)))
str(-3.14)<jupyter_output><empty_output><jupyter_text>Float to Integer:<jupyter_code>int(7.7)
int(7.7) + 1<jupyter_output><empty_output><jupyter_text>Flow Control Comparison Operators| Operator | Meaning                  || -------- | ------------------------ || `==`     | Equal to                 || `!=`     | Not equal to             || `<`      | Less than                || `>`      | Greater Than             || `<=`     | Less than or Equal to    || `>=`     | Greater than or Equal to |These operators evaluate to True or False depending on the values you give them.Examples:<jupyter_code>42 == 42
40 == 42
'hello' == 'hello'
'hello' == 'Hello'
'dog' != 'cat'
42 == 42.0
42 == '42'<jupyter_output><empty_output><jupyter_text>Boolean evaluationNever use `==` or `!=` operator to evaluate boolean operation. Use the `is` or `is not` operators,or use implicit boolean evaluation.NO (even if they are valid Python):<jupyter_code>True == True
True != False<jupyter_output><empty_output><jupyter_text>YES (even if they are valid Python):<jupyter_code>True is True
True is not False<jupyter_output><empty_output><jupyter_text>These statements are equivalent:<jupyter_code>if a is True:
   pass
if a is not False:
   pass
if a:
   pass<jupyter_output><empty_output><jupyter_text>And these as well:<jupyter_code>if a is False:
   pass
if a is not True:
   pass
if not a:
   pass<jupyter_output><empty_output><jupyter_text>Boolean OperatorsThere are three Boolean operators: and, or, and not.The *and* Operator’s *Truth* Table:| Expression      | Evaluates to || --------------- | ------------ || True and True   | True         || True and False  | False        || False and True  | False        || False and False | False        |The *or* Operator’s *Truth* Table:| Expression     | Evaluates to || -------------- | ------------ || True or True   | True         || True or False  | True         || False or True  | True         || False or False | False        |The *not* Operator’s *Truth* Table:| Expression | Evaluates to || ---------- | ------------ || not True   | False        || not False  | True         | Mixing Boolean and Comparison Operators<jupyter_code>(4 < 5) and (5 < 6)
(4 < 5) and (9 < 6)
(1 == 2) or (2 == 2)<jupyter_output><empty_output><jupyter_text>You can also use multiple Boolean operators in an expression, along with the comparison operators:<jupyter_code>2 + 2 == 4 and not 2 + 2 == 5 and 2 * 2 == 2 + 2<jupyter_output><empty_output><jupyter_text>if Statements<jupyter_code>if name == 'Alice':
    print('Hi, Alice.')<jupyter_output><empty_output><jupyter_text>else Statements<jupyter_code>name = 'Bob'

if name == 'Alice':
    print('Hi, Alice.')
else:
    print('Hello, stranger.')<jupyter_output><empty_output><jupyter_text>elif Statements<jupyter_code>name = 'Bob'
age = 5

if name == 'Alice':
    print('Hi, Alice.')
elif age < 12:
    print('You are not Alice, kiddo.')
name = 'Bob'
age = 30

if name == 'Alice':
    print('Hi, Alice.')
elif age < 12:
    print('You are not Alice, kiddo.')
else:
    print('You are neither Alice nor a little kid.')<jupyter_output><empty_output><jupyter_text>while Loop Statements<jupyter_code>spam = 0

while spam < 5:
    print('Hello, world.')
    spam = spam + 1<jupyter_output><empty_output><jupyter_text>break Statements If the execution reaches a break statement, it immediately exits the while loop’s clause:<jupyter_code>while True:
    print('Please type your name.')
    name = input()
    if name == 'your name':
        break

print('Thank you!')<jupyter_output><empty_output><jupyter_text>continue StatementsWhen the program execution reaches a continue statement, the program execution immediately jumps back to the start of the loop.<jupyter_code>while True:
    print('Who are you?')
    name = input()
    if name != 'Joe':
        continue
    print('Hello, Joe. What is the password? (It is a fish.)')
    password = input()
    if password == '<PASSWORD>':
        break

print('Access granted.')<jupyter_output><empty_output><jupyter_text>for Loops and the range() Function<jupyter_code>print('My name is')
for i in range(5):
    print('<NAME> ({})'.format(str(i)))<jupyter_output><empty_output><jupyter_text>The *range()* function can also be called with three arguments. The first two arguments will be the start and stop values, and the third will be the step argument. The step is the amount that the variable is increased by after each iteration.<jupyter_code>for i in range(0, 10, 2):
   print(i)<jupyter_output><empty_output><jupyter_text>You can even use a negative number for the step argument to make the for loop count down instead of up.<jupyter_code>for i in range(5, -1, -1):
    print(i)<jupyter_output><empty_output><jupyter_text>For else statementThis allows to specify a statement to execute in case of the full loop has been executed. Onlyuseful when a `break` condition can occur in the loop:<jupyter_code>for i in [1, 2, 3, 4, 5]:
   if i == 3:
       break
else:
   print("only executed when no item of the list is equal to 3")<jupyter_output><empty_output><jupyter_text>Importing Modules<jupyter_code>import random

for i in range(5):
    print(random.randint(1, 10))
import random, sys, os, math
from random import *.<jupyter_output><empty_output><jupyter_text>Ending a Program with sys.exit<jupyter_code>import sys

while True:
    print('Type exit to exit.')
    response = input()
    if response == 'exit':
        sys.exit()
    print('You typed {}.'.format(response))<jupyter_output><empty_output><jupyter_text>Functions<jupyter_code>def hello(name):
    print('Hello {}'.format(name))<jupyter_output><empty_output><jupyter_text>Return Values and return StatementsWhen creating a function using the def statement, you can specify what the return value should be with a return statement. A return statement consists of the following:- The return keyword.- The value or expression that the function should return.<jupyter_code>import random
def getAnswer(answerNumber):
    if answerNumber == 1:
        return 'It is certain'
    elif answerNumber == 2:
        return 'It is decidedly so'
    elif answerNumber == 3:
        return 'Yes'
    elif answerNumber == 4:
        return 'Reply hazy try again'
    elif answerNumber == 5:
        return 'Ask again later'
    elif answerNumber == 6:
        return 'Concentrate and ask again'
    elif answerNumber == 7:
        return 'My reply is no'
    elif answerNumber == 8:
        return 'Outlook not so good'
    elif answerNumber == 9:
        return 'Very doubtful'

r = random.randint(1, 9)
fortune = getAnswer(r)
print(fortune)<jupyter_output><empty_output><jupyter_text>The None Value<jupyter_code>spam = print('Hello!')
spam is None<jupyter_output><empty_output><jupyter_text>Note: never compare to `None` with the `==` operator. Always use `is`. print Keyword Arguments<jupyter_code>print('Hello', end='')
print('World')
print('cats', 'dogs', 'mice')
print('cats', 'dogs', 'mice', sep=',')<jupyter_output><empty_output><jupyter_text>Local and Global Scope- Code in the global scope cannot use any local variables.- However, a local scope can access global variables.- Code in a function’s local scope cannot use variables in any other local scope.- You can use the same name for different variables if they are in different scopes. That is, there can be a local variable named spam and a global variable also named spam. The global StatementIf you need to modify a global variable from within a function, use the global statement:<jupyter_code>def spam():
    global eggs
    eggs = 'spam'

eggs = 'global'
spam()
print(eggs)<jupyter_output><empty_output><jupyter_text>There are four rules to tell whether a variable is in a local scope or global scope:1. If a variable is being used in the global scope (that is, outside of all functions), then it is always a global variable.1. If there is a global statement for that variable in a function, it is a global variable.1. Otherwise, if the variable is used in an assignment statement in the function, it is a local variable.1. But if the variable is not used in an assignment statement, it is a global variable. Exception Handling Basic exception handling<jupyter_code>def spam(divideBy):
    try:
        return 42 / divideBy
    except ZeroDivisionError as e:
        print('Error: Invalid argument: {}'.format(e))

print(spam(2))
print(spam(12))
print(spam(0))
print(spam(1))<jupyter_output><empty_output><jupyter_text>Final code in exception handlingCode inside the `finally` section is always executed, no matter if an exception has been raised ornot, and even if an exception is not caught.<jupyter_code>def spam(divideBy):
    try:
        return 42 / divideBy
    except ZeroDivisionError as e:
        print('Error: Invalid argument: {}'.format(e))
    finally:
        print("-- division finished --")

print(spam(12))
print(spam(0))<jupyter_output><empty_output><jupyter_text>Lists<jupyter_code>['cat', 'bat', 'rat', 'elephant']<jupyter_output><empty_output><jupyter_text>Getting Individual Values in a List with Indexes<jupyter_code>spam = ['cat', 'bat', 'rat', 'elephant']
spam[0]
spam[1]
spam[2]
spam[3]<jupyter_output><empty_output><jupyter_text>Negative Indexes<jupyter_code>spam = ['cat', 'bat', 'rat', 'elephant']
spam[-1]
spam[-3]
'The {} is afraid of the {}.'.format(spam[-1], spam[-3])<jupyter_output><empty_output><jupyter_text>Getting Sublists with Slices<jupyter_code>spam = ['cat', 'bat', 'rat', 'elephant']
spam[0:4]
spam[1:3]
spam[0:-1]
spam = ['cat', 'bat', 'rat', 'elephant']
spam[:2]
spam[1:]
spam[:]<jupyter_output><empty_output><jupyter_text>Getting a list Length with len<jupyter_code>spam = ['cat', 'dog', 'moose']
len(spam)<jupyter_output><empty_output><jupyter_text>Changing Values in a List with Indexes<jupyter_code>spam = ['cat', 'bat', 'rat', 'elephant']
spam[1] = 'aardvark'
spam
spam[2] = spam[1]
spam
spam[-1] = 12345
spam<jupyter_output><empty_output><jupyter_text>List Concatenation and List Replication<jupyter_code>[1, 2, 3] + ['A', 'B', 'C']
['X', 'Y', 'Z'] * 3
spam = [1, 2, 3]
spam = spam + ['A', 'B', 'C']
spam<jupyter_output><empty_output><jupyter_text>Removing Values from Lists with del Statements<jupyter_code>spam = ['cat', 'bat', 'rat', 'elephant']
del spam[2]
spam
del spam[2]
spam<jupyter_output><empty_output><jupyter_text>Using for Loops with Lists<jupyter_code>supplies = ['pens', 'staplers', 'flame-throwers', 'binders']

for i, supply in enumerate(supplies):
    print('Index {} in supplies is: {}'.format(str(i), supply))<jupyter_output><empty_output><jupyter_text>Looping Through Multiple Lists with zip<jupyter_code>name = ['Pete', 'John', 'Elizabeth']
age = [6, 23, 44]

for n, a in zip(name, age):
    print('{} is {} years old'.format(n, a))<jupyter_output><empty_output><jupyter_text>The in and not in Operators<jupyter_code>'howdy' in ['hello', 'hi', 'howdy', 'heyas']
spam = ['hello', 'hi', 'howdy', 'heyas']
False
'howdy' not in spam
'cat' not in spam<jupyter_output><empty_output><jupyter_text>The Multiple Assignment TrickThe multiple assignment trick is a shortcut that lets you assign multiple variables with the values in a list in one line of code. So instead of doing this:<jupyter_code>cat = ['fat', 'orange', 'loud']
size = cat[0]
color = cat[1]
disposition = cat[2]<jupyter_output><empty_output><jupyter_text>You could type this line of code:<jupyter_code>cat = ['fat', 'orange', 'loud']
size, color, disposition = cat<jupyter_output><empty_output><jupyter_text>The multiple assignment trick can also be used to swap the values in two variables:<jupyter_code>a, b = 'Alice', 'Bob'
a, b = b, a
print(a)
print(b)<jupyter_output><empty_output><jupyter_text>Augmented Assignment Operators| Operator    | Equivalent        || ----------- | ----------------- || `spam += 1` | `spam = spam + 1` || `spam -= 1` | `spam = spam - 1` || `spam *= 1` | `spam = spam * 1` || `spam /= 1` | `spam = spam / 1` || `spam %= 1` | `spam = spam % 1` |Examples:<jupyter_code>spam = 'Hello'
spam += ' world!'
spam
bacon = ['Zophie']
bacon *= 3
bacon<jupyter_output><empty_output><jupyter_text>Finding a Value in a List with the index Method<jupyter_code>spam = ['Zophie', 'Pooka', 'Fat-tail', 'Pooka']
spam.index('Pooka')<jupyter_output><empty_output><jupyter_text>Adding Values to Lists with append and insert**append()**:<jupyter_code>spam = ['cat', 'dog', 'bat']
spam.append('moose')
spam<jupyter_output><empty_output><jupyter_text>**insert()**:<jupyter_code>spam = ['cat', 'dog', 'bat']
spam.insert(1, 'chicken')
spam<jupyter_output><empty_output><jupyter_text>Removing Values from Lists with remove<jupyter_code>spam = ['cat', 'bat', 'rat', 'elephant']
spam.remove('bat')
spam<jupyter_output><empty_output><jupyter_text>If the value appears multiple times in the list, only the first instance of the value will be removed. Sorting the Values in a List with sort<jupyter_code>spam = [2, 5, 3.14, 1, -7]
spam.sort()
spam
spam = ['ants', 'cats', 'dogs', 'badgers', 'elephants']
spam.sort()
spam<jupyter_output><empty_output><jupyter_text>You can also pass True for the reverse keyword argument to have sort() sort the values in reverse order:<jupyter_code>spam.sort(reverse=True)
spam<jupyter_output><empty_output><jupyter_text>If you need to sort the values in regular alphabetical order, pass str. lower for the key keyword argument in the sort() method call:<jupyter_code>spam = ['a', 'z', 'A', 'Z']
spam.sort(key=str.lower)
spam<jupyter_output><empty_output><jupyter_text>You can use the built-in function `sorted` to return a new list:<jupyter_code>spam = ['ants', 'cats', 'dogs', 'badgers', 'elephants']
sorted(spam)<jupyter_output><empty_output><jupyter_text>Tuple Data Type<jupyter_code>eggs = ('hello', 42, 0.5)
eggs[0]
eggs[1:3]
len(eggs)<jupyter_output><empty_output><jupyter_text>The main way that tuples are different from lists is that tuples, like strings, are immutable. Converting Types with the list and tuple Functions<jupyter_code>tuple(['cat', 'dog', 5])
list(('cat', 'dog', 5))
list('hello')<jupyter_output><empty_output><jupyter_text>Dictionaries and Structuring DataExample Dictionary:<jupyter_code>myCat = {'size': 'fat', 'color': 'gray', 'disposition': 'loud'}<jupyter_output><empty_output><jupyter_text>The keys, values, and items Methodsvalues():<jupyter_code>spam = {'color': 'red', 'age': 42}

for v in spam.values():
    print(v)<jupyter_output><empty_output><jupyter_text>keys():<jupyter_code>for k in spam.keys():
    print(k)<jupyter_output><empty_output><jupyter_text>items():<jupyter_code>for i in spam.items():
    print(i)<jupyter_output><empty_output><jupyter_text>Using the keys(), values(), and items() methods, a for loop can iterate over the keys, values, or key-value pairs in a dictionary, respectively.<jupyter_code>spam = {'color': 'red', 'age': 42}

for k, v in spam.items():
    print('Key: {} Value: {}'.format(k, str(v)))<jupyter_output><empty_output><jupyter_text>Checking if a Key or Value Exists in a Dictionary<jupyter_code>spam = {'name': 'Zophie', 'age': 7}
'name' in spam.keys()
'Zophie' in spam.values()
# You can omit the call to keys() when checking for a key
'color' in spam
'color' not in spam
'color' in spam<jupyter_output><empty_output><jupyter_text>The get Method<jupyter_code>picnic_items = {'apples': 5, 'cups': 2}
'I am bringing {} cups.'.format(str(picnic_items.get('cups', 0)))
'I am bringing {} eggs.'.format(str(picnic_items.get('eggs', 0)))<jupyter_output><empty_output><jupyter_text>The setdefault MethodLet's consider this code:<jupyter_code>spam = {'name': 'Pooka', 'age': 5}
if 'color' not in spam:
    spam['color'] = 'black'<jupyter_output><empty_output><jupyter_text>Using `setdefault` we could make the same code more shortly:<jupyter_code>spam = {'name': 'Pooka', 'age': 5}
spam.setdefault('color', 'black')
spam
spam.setdefault('color', 'white')
spam<jupyter_output><empty_output><jupyter_text>Pretty Printing<jupyter_code>import pprint

message = 'It was a bright cold day in April, and the clocks were striking thirteen.'
count = {}

for character in message:
    count.setdefault(character, 0)
    count[character] = count[character] + 1

pprint.pprint(count)<jupyter_output><empty_output><jupyter_text>Merge two dictionaries<jupyter_code># in Python 3.5+:
x = {'a': 1, 'b': 2}
y = {'b': 3, 'c': 4}
z = {**x, **y}
z<jupyter_output><empty_output><jupyter_text>setsFrom the Python 3 [documentation](https://docs.python.org/3/tutorial/datastructures.html)> A set is an unordered collection with no duplicate elements. Basic uses include membership testing and eliminating duplicate entries. Set objects also support mathematical operations like union, intersection, difference, and symmetric difference. Initializing a setThere are two ways to create sets: using curly braces `{}` and the bult-in function `set()`<jupyter_code>s = {1, 2, 3}
s = set([1, 2, 3])<jupyter_output><empty_output><jupyter_text>When creating an empty set, be sure to not use the curly braces `{}`  or you will get an empty dictionary instead.<jupyter_code>s = {}
type(s)<jupyter_output><empty_output><jupyter_text>sets: unordered collections of unique elementsA set automatically remove all the duplicate values.<jupyter_code>s = {1, 2, 3, 2, 3, 4}
s<jupyter_output><empty_output><jupyter_text>And as an unordered data type, they can't be indexed.<jupyter_code>s = {1, 2, 3}
s(0)<jupyter_output><empty_output><jupyter_text>set add and updateUsing the `add()` method we can add a single element to the set.<jupyter_code>s = {1, 2, 3}
s.add(4)
s<jupyter_output><empty_output><jupyter_text>And with `update()`, multiple ones .<jupyter_code>s = {1, 2, 3}
s.update([2, 3, 4, 5, 6])
s  # remember, sets automatically remove duplicates<jupyter_output><empty_output><jupyter_text>set remove and discardBoth methods will remove an element from the set, but `remove()` will raise a `key error` if the value doesn't exist.<jupyter_code>s = {1, 2, 3}
s.remove(3)
s
s.remove(3)<jupyter_output><empty_output><jupyter_text>`discard()` won't raise any errors.<jupyter_code>s = {1, 2, 3}
s.discard(3)
s
s.discard(3)<jupyter_output><empty_output><jupyter_text>set union`union()` or `|` will create a new set that contains all the elements from the sets provided.<jupyter_code>s1 = {1, 2, 3}
s2 = {3, 4, 5}
s1.union(s2)  # or 's1 | s2'<jupyter_output><empty_output><jupyter_text>set intersection`intersection`  or `&`  will return a set containing only the elements that are common to all of them.<jupyter_code>s1 = {1, 2, 3}
s2 = {2, 3, 4}
s3 = {3, 4, 5}
s1.intersection(s2, s3)  # or 's1 & s2 & s3'<jupyter_output><empty_output><jupyter_text>set difference`difference` or `-` will return only the elements that are in one of the sets.<jupyter_code>s1 = {1, 2, 3}
s2 = {2, 3, 4}
s1.difference(s2)  # or 's1 - s2'<jupyter_output><empty_output><jupyter_text>set symetric_difference`symetric_difference` or `^` will return all the elements that are not common between them.<jupyter_code>s1 = {1, 2, 3}
s2 = {2, 3, 4}
s1.symmetric_difference(s2)  # or 's1 ^ s2'<jupyter_output><empty_output><jupyter_text>itertools ModuleThe *itertools* module is a collection of tools intented to be fast and use memory efficiently when handling iterators (like [lists](lists) or [dictionaries](dictionaries-and-structuring-data)).From the official [Python 3.x documentation](https://docs.python.org/3/library/itertools.html):> The module standardizes a core set of fast, memory efficient tools that are useful by themselves or in combination. Together, they form an “iterator algebra” making it possible to construct specialized tools succinctly and efficiently in pure Python.The *itertools* module comes in the standard library and must be imported.The [operator](https://docs.python.org/3/library/operator.html) module will also be used. This module is not necessary when using itertools, but needed for some of the examples below.<jupyter_code>import itertools
import operator<jupyter_output><empty_output><jupyter_text>accumulateMakes an iterator that returns the results of a function.<jupyter_code>itertools.accumulate(iterable[, func])<jupyter_output><empty_output><jupyter_text>Example:<jupyter_code>data = [1, 2, 3, 4, 5]
result = itertools.accumulate(data, operator.mul)
for each in result:
   print(each)<jupyter_output><empty_output><jupyter_text>The operator.mul takes two numbers and multiplies them:<jupyter_code>operator.mul(1, 2)
operator.mul(2, 3)
operator.mul(6, 4)
operator.mul(24, 5)<jupyter_output><empty_output><jupyter_text>Passing a function is optional:<jupyter_code>data = [5, 2, 6, 4, 5, 9, 1]
result = itertools.accumulate(data)

for each in result:
   print(each)<jupyter_output><empty_output><jupyter_text>If no function is designated the items will be summed:<jupyter_code>5
5 + 2 = 7
7 + 6 = 13
13 + 4 = 17
17 + 5 = 22
22 + 9 = 31
31 + 1 = 32<jupyter_output><empty_output><jupyter_text>combinationsTakes an iterable and a integer. This will create all the unique combination that have r members.<jupyter_code>itertools.combinations(iterable, r)<jupyter_output><empty_output><jupyter_text>Example:<jupyter_code>shapes = ['circle', 'triangle', 'square',]
result = itertools.combinations(shapes, 2)

for each in result:
   print(each)<jupyter_output><empty_output><jupyter_text>combinations_with_replacementJust like combinations(), but allows individual elements to be repeated more than once.<jupyter_code>itertools.combinations_with_replacement(iterable, r)<jupyter_output><empty_output><jupyter_text>Example:<jupyter_code>shapes = ['circle', 'triangle', 'square']
result = itertools.combinations_with_replacement(shapes, 2)

for each in result:
   print(each)<jupyter_output><empty_output><jupyter_text>countMakes an iterator that returns evenly spaced values starting with number start.<jupyter_code>itertools.count(start=0, step=1)<jupyter_output><empty_output><jupyter_text>Example:<jupyter_code>for i in itertools.count(10,3):
   print(i)
   if i > 20:
       break<jupyter_output><empty_output><jupyter_text>cycleThis function cycles through an iterator endlessly.<jupyter_code>itertools.cycle(iterable)<jupyter_output><empty_output><jupyter_text>Example:<jupyter_code>colors = ['red', 'orange', 'yellow', 'green', 'blue', 'violet']
for color in itertools.cycle(colors):
   print(color)<jupyter_output><empty_output><jupyter_text>When reached the end of the iterable it start over again from the beginning. chainTake a series of iterables and return them as one long iterable.<jupyter_code>itertools.chain(*iterables)<jupyter_output><empty_output><jupyter_text>Example:<jupyter_code>colors = ['red', 'orange', 'yellow', 'green', 'blue']
shapes = ['circle', 'triangle', 'square', 'pentagon']
result = itertools.chain(colors, shapes)

for each in result:
   print(each)<jupyter_output><empty_output><jupyter_text>compressFilters one iterable with another.<jupyter_code>itertools.compress(data, selectors)<jupyter_output><empty_output><jupyter_text>Example:<jupyter_code>shapes = ['circle', 'triangle', 'square', 'pentagon']
selections = [True, False, True, False]
result = itertools.compress(shapes, selections)

for each in result:
   print(each)<jupyter_output><empty_output><jupyter_text>dropwhileMake an iterator that drops elements from the iterable as long as the predicate is true; afterwards, returns every element.<jupyter_code>itertools.dropwhile(predicate, iterable)<jupyter_output><empty_output><jupyter_text>Example:<jupyter_code>data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1]
result = itertools.dropwhile(lambda x: x<5, data)

for each in result:
   print(each)<jupyter_output><empty_output><jupyter_text>filterfalseMakes an iterator that filters elements from iterable returning only those for which the predicate is False.<jupyter_code>itertools.filterfalse(predicate, iterable)<jupyter_output><empty_output><jupyter_text>Example:<jupyter_code>data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
result = itertools.filterfalse(lambda x: x<5, data)

for each in result:
   print(each)<jupyter_output><empty_output><jupyter_text>groupbySimply put, this function groups things together.<jupyter_code>itertools.groupby(iterable, key=None)<jupyter_output><empty_output><jupyter_text>Example:<jupyter_code>robots = [{
    'name': 'blaster',
    'faction': 'autobot'
}, {
    'name': 'galvatron',
    'faction': 'decepticon'
}, {
    'name': 'jazz',
    'faction': 'autobot'
}, {
    'name': 'metroplex',
    'faction': 'autobot'
}, {
    'name': 'megatron',
    'faction': 'decepticon'
}, {
    'name': 'starcream',
    'faction': 'decepticon'
}]

for key, group in itertools.groupby(robots, key=lambda x: x['faction']):
   print(key)
   print(list(group))<jupyter_output><empty_output><jupyter_text>isliceThis function is very much like slices. This allows you to cut out a piece of an iterable.<jupyter_code>itertools.islice(iterable, start, stop[, step])<jupyter_output><empty_output><jupyter_text>Example:<jupyter_code>colors = ['red', 'orange', 'yellow', 'green', 'blue',]
few_colors = itertools.islice(colors, 2)

for each in few_colors:
   print(each)<jupyter_output><empty_output><jupyter_text>permutations<jupyter_code>itertools.permutations(iterable, r=None)<jupyter_output><empty_output><jupyter_text>Example:<jupyter_code>alpha_data = ['a', 'b', 'c']
result = itertools.permutations(alpha_data)

for each in result:
   print(each)<jupyter_output><empty_output><jupyter_text>productCreates the cartesian products from a series of iterables.<jupyter_code>num_data = [1, 2, 3]
alpha_data = ['a', 'b', 'c']
result = itertools.product(num_data, alpha_data)

for each in result:
    print(each)<jupyter_output><empty_output><jupyter_text>repeatThis function will repeat an object over and over again. Unless, there is a times argument.<jupyter_code>itertools.repeat(object[, times])<jupyter_output><empty_output><jupyter_text>Example:<jupyter_code>for i in itertools.repeat("spam", 3):
    print(i)<jupyter_output><empty_output><jupyter_text>starmapMakes an iterator that computes the function using arguments obtained from the iterable.<jupyter_code>itertools.starmap(function, iterable)<jupyter_output><empty_output><jupyter_text>Example:<jupyter_code>data = [(2, 6), (8, 4), (7, 3)]
result = itertools.starmap(operator.mul, data)

for each in result:
   print(each)<jupyter_output><empty_output><jupyter_text>takewhileThe opposite of dropwhile(). Makes an iterator and returns elements from the iterable as long as the predicate is true.<jupyter_code>itertools.takewhile(predicate, iterable)<jupyter_output><empty_output><jupyter_text>Example:<jupyter_code>data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1]
result = itertools.takewhile(lambda x: x<5, data)

for each in result:
   print(each)<jupyter_output><empty_output><jupyter_text>teeReturn n independent iterators from a single iterable.<jupyter_code>itertools.tee(iterable, n=2)<jupyter_output><empty_output><jupyter_text>Example:<jupyter_code>colors = ['red', 'orange', 'yellow', 'green', 'blue']
alpha_colors, beta_colors = itertools.tee(colors)

for each in alpha_colors:
   print(each)
colors = ['red', 'orange', 'yellow', 'green', 'blue']
alpha_colors, beta_colors = itertools.tee(colors)

for each in beta_colors:
   print(each)<jupyter_output><empty_output><jupyter_text>zip_longestMakes an iterator that aggregates elements from each of the iterables. If the iterables are of uneven length, missing values are filled-in with fillvalue. Iteration continues until the longest iterable is exhausted.<jupyter_code>itertools.zip_longest(*iterables, fillvalue=None)<jupyter_output><empty_output><jupyter_text>Example:<jupyter_code>colors = ['red', 'orange', 'yellow', 'green', 'blue',]
data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10,]

for each in itertools.zip_longest(colors, data, fillvalue=None):
   print(each)<jupyter_output><empty_output><jupyter_text>Comprehensions List comprehension<jupyter_code>a = [1, 3, 5, 7, 9, 11]
[i - 1 for i in a]<jupyter_output><empty_output><jupyter_text>Set comprehension<jupyter_code>b = {"abc", "def"}
{s.upper() for s in b}<jupyter_output><empty_output><jupyter_text>Dict comprehension<jupyter_code>c = {'name': 'Pooka', 'age': 5}
{v, k for k, v in c.items()}<jupyter_output><empty_output><jupyter_text>A List comprehension can be generated from a dictionary:<jupyter_code>c = {'name': 'Pooka', 'first_name': 'Oooka'}
["{}:{}".format(k.upper(), v.upper()) for k, v in c.items()]<jupyter_output><empty_output><jupyter_text>Manipulating Strings Escape Characters| Escape character | Prints as            || ---------------- | -------------------- || `\'`             | Single quote         || `\"`             | Double quote         || `\t`             | Tab                  || `\n`             | Newline (line break) || `\\`             | Backslash            |Example:<jupyter_code>print("Hello there!\nHow are you?\nI\'m doing fine.")
Hello there!
How are you?<jupyter_output><empty_output><jupyter_text>Raw StringsA raw string completely ignores all escape characters and prints any backslash that appears in the string.<jupyter_code>print(r'That is Carol\'s cat.')<jupyter_output><empty_output><jupyter_text>Note: mostly used for regular expression definition (see `re` package) Multiline Strings with Triple Quotes<jupyter_code>print('''Dear Alice,

Eve's cat has been arrested for catnapping, cat burglary, and extortion.

Sincerely,
Bob''')<jupyter_output><empty_output><jupyter_text>To keep a nicer flow in your code, you can use the `dedent` function from the `textwrap` standard package.<jupyter_code>from textwrap import dedent

def my_function():
    print('''
        Dear Alice,

        Eve's cat has been arrested for catnapping, cat burglary, and extortion.

        Sincerely,
        Bob
        ''').strip()<jupyter_output><empty_output><jupyter_text>This generates the same string than before. Indexing and Slicing Strings<jupyter_code>H   e   l   l   o       w   o   r   l   d    !
0   1   2   3   4   5   6   7   8   9   10   11
spam = 'Hello world!'
spam[0]
spam[4]
spam[-1]<jupyter_output><empty_output><jupyter_text>Slicing:<jupyter_code>spam[0:5]
spam[:5]
spam[6:]
spam[6:-1]
spam[:-1]
spam[::-1]
spam = 'Hello world!'
fizz = spam[0:5]
fizz<jupyter_output><empty_output><jupyter_text>The in and not in Operators with Strings<jupyter_code>'Hello' in 'Hello World'
'Hello' in 'Hello'
'HELLO' in 'Hello World'
'' in 'spam'
'cats' not in 'cats and dogs'<jupyter_output><empty_output><jupyter_text>The in and not in Operators with list<jupyter_code>a = [1, 2, 3, 4]
5 in a
2 in a<jupyter_output><empty_output><jupyter_text>The upper, lower, isupper, and islower String Methods`upper()` and `lower()`:<jupyter_code>spam = 'Hello world!'
spam = spam.upper()
spam
spam = spam.lower()
spam<jupyter_output><empty_output><jupyter_text>isupper() and islower():<jupyter_code>spam = 'Hello world!'
spam.islower()
spam.isupper()
'HELLO'.isupper()
'abc12345'.islower()
'12345'.islower()
'12345'.isupper()<jupyter_output><empty_output><jupyter_text>The isX String Methods- **isalpha()** returns True if the string consists only of letters and is not blank.- **isalnum()** returns True if the string consists only of lettersand numbers and is not blank.- **isdecimal()** returns True if the string consists only ofnumeric characters and is not blank.- **isspace()** returns True if the string consists only of spaces,tabs, and new-lines and is not blank.- **istitle()** returns True if the string consists only of wordsthat begin with an uppercase letter followed by onlylowercase letters. The startswith and endswith String Methods<jupyter_code>'Hello world!'.startswith('Hello')
'Hello world!'.endswith('world!')
'abc123'.startswith('abcdef')
'abc123'.endswith('12')
'Hello world!'.startswith('Hello world!')
'Hello world!'.endswith('Hello world!')<jupyter_output><empty_output><jupyter_text>The join and split String Methodsjoin():<jupyter_code>', '.join(['cats', 'rats', 'bats'])
' '.join(['My', 'name', 'is', 'Simon'])
'ABC'.join(['My', 'name', 'is', 'Simon'])<jupyter_output><empty_output><jupyter_text>split():<jupyter_code>'My name is Simon'.split()
'MyABCnameABCisABCSimon'.split('ABC')
'My name is Simon'.split('m')<jupyter_output><empty_output><jupyter_text>Justifying Text with rjust, ljust, and centerrjust() and ljust():<jupyter_code>'Hello'.rjust(10)
'Hello'.rjust(20)
'Hello World'.rjust(20)
'Hello'.ljust(10)<jupyter_output><empty_output><jupyter_text>An optional second argument to rjust() and ljust() will specify a fill character other than a space character. Enter the following into the interactive shell:<jupyter_code>'Hello'.rjust(20, '*')
'Hello'.ljust(20, '-')<jupyter_output><empty_output><jupyter_text>center():<jupyter_code>'Hello'.center(20)
'Hello'.center(20, '=')<jupyter_output><empty_output><jupyter_text>Removing Whitespace with strip, rstrip, and lstrip<jupyter_code>spam = '    Hello World     '
spam.strip()
spam.lstrip()
spam.rstrip()
spam = 'SpamSpamBaconSpamEggsSpamSpam'
spam.strip('ampS')<jupyter_output><empty_output><jupyter_text>Copying and Pasting Strings with the pyperclip ModuleFirst, install `pypeerclip` with pip:<jupyter_code>pip install pyperclip
import pyperclip

pyperclip.copy('Hello world!')
pyperclip.paste()<jupyter_output><empty_output><jupyter_text>String Formatting % operator<jupyter_code>name = 'Pete'
'Hello %s' % name<jupyter_output><empty_output><jupyter_text>We can use the `%x` format specifier to convert an int value to a string:<jupyter_code>num = 5
'I have %x apples' % num<jupyter_output><empty_output><jupyter_text>Note: For new code, using str.format or f-strings is strongly recommended over the `%` operator. str.formatPython 3 introduced a new way to do string formatting that was later back-ported to Python 2.7. This makes the syntax for string formatting more regular.<jupyter_code>name = 'John'
age = 20'

"Hello I'm {}, my age is {}".format(name, age)
"Hello I'm {0}, my age is {1}".format(name, age)<jupyter_output><empty_output><jupyter_text>The official [Python 3.x documentation](https://docs.python.org/3/library/stdtypes.html?highlight=sprintfprintf-style-string-formatting) recommend `str.format` over the `%` operator:> The formatting operations described here exhibit a variety of quirks that lead to a number of common errors (such as failing to display tuples and dictionaries correctly). Using the newer formatted string literals or the str.format() interface helps avoid these errors. These alternatives also provide more powerful, flexible and extensible approaches to formatting text. Lazy string formattingYou would only use `%s` string formatting on functions that can do lazy parameters evaluation,the most common being logging:Prefer:<jupyter_code>name = "alice"
logging.debug("User name: %s", name)<jupyter_output><empty_output><jupyter_text>Over:<jupyter_code>logging.debug("User name: {}".format(name))<jupyter_output><empty_output><jupyter_text>Or:<jupyter_code>logging.debug("User name: " + name)<jupyter_output><empty_output><jupyter_text>Formatted String Literals or f-stringsPython 3.6+<jupyter_code>name = 'Elizabeth'
f'Hello {name}!'<jupyter_output><empty_output><jupyter_text>It is even possible to do inline arithmetic with it:<jupyter_code>a = 5
b = 10
f'Five plus ten is {a + b} and not {2 * (a + b)}.'<jupyter_output><empty_output><jupyter_text>Template Strings A simpler and less powerful mechanism, but it is recommended when handling format strings generated by users. Due to their reduced complexity template strings are a safer choice.<jupyter_code>from string import Template

name = 'Elizabeth'
t = Template('Hey $name!')
t.substitute(name=name)<jupyter_output><empty_output><jupyter_text>Regular Expressions1. Import the regex module with `import re`.2. Create a Regex object with the `re.compile()` function. (Remember to use a raw string.)3. Pass the string you want to search into the Regex object’s `search()` method. This returns a `Match` object.4. Call the Match object’s `group()` method to return a string of the actual matched text.All the regex functions in Python are in the re module:<jupyter_code>import re<jupyter_output><empty_output><jupyter_text>Matching Regex Objects<jupyter_code>phone_num_regex = re.compile(r'\d\d\d-\d\d\d-\d\d\d\d')
mo = phone_num_regex.search('My number is 415-555-4242.')
print('Phone number found: {}'.format(mo.group()))<jupyter_output><empty_output><jupyter_text>Grouping with Parentheses<jupyter_code>phone_num_regex = re.compile(r'(\d\d\d)-(\d\d\d-\d\d\d\d)')
mo = phone_num_regex.search('My number is 415-555-4242.')
mo.group(1)
mo.group(2)
mo.group(0)
mo.group()<jupyter_output><empty_output><jupyter_text>To retrieve all the groups at once: use the groups() method—note the plural form for the name.<jupyter_code>mo.groups()
area_code, main_number = mo.groups()
print(area_code)
print(main_number)<jupyter_output><empty_output><jupyter_text>Matching Multiple Groups with the PipeThe | character is called a pipe. You can use it anywhere you want to match one of many expressions. For example, the regular expression r'Batman|<NAME>' will match either 'Batman' or '<NAME>'.<jupyter_code>hero_regex = re.compile (r'Batman|<NAME>')
mo1 = hero_regex.search('Batman and <NAME>.')
mo1.group()
mo2 = hero_regex.search('<NAME> and Batman.')
mo2.group()<jupyter_output><empty_output><jupyter_text>You can also use the pipe to match one of several patterns as part of your regex:<jupyter_code>bat_regex = re.compile(r'Bat(man|mobile|copter|bat)')
mo = bat_regex.search('Batmobile lost a wheel')
mo.group()
mo.group(1)<jupyter_output><empty_output><jupyter_text>Optional Matching with the Question MarkThe ? character flags the group that precedes it as an optional part of the pattern.<jupyter_code>bat_regex = re.compile(r'Bat(wo)?man')
mo1 = bat_regex.search('The Adventures of Batman')
mo1.group()
mo2 = bat_regex.search('The Adventures of Batwoman')
mo2.group()<jupyter_output><empty_output><jupyter_text>Matching Zero or More with the StarThe * (called the star or asterisk) means “match zero or more”—the group that precedes the star can occur any number of times in the text.<jupyter_code>bat_regex = re.compile(r'Bat(wo)*man')
mo1 = bat_regex.search('The Adventures of Batman')
mo1.group()
mo2 = bat_regex.search('The Adventures of Batwoman')
mo2.group()
mo3 = bat_regex.search('The Adventures of Batwowowowoman')
mo3.group()<jupyter_output><empty_output><jupyter_text>Matching One or More with the PlusWhile * means “match zero or more,” the + (or plus) means “match one or more”. The group preceding a plus must appear at least once. It is not optional:<jupyter_code>bat_regex = re.compile(r'Bat(wo)+man')
mo1 = bat_regex.search('The Adventures of Batwoman')
mo1.group()
mo2 = bat_regex.search('The Adventures of Batwowowowoman')
mo2.group()
mo3 = bat_regex.search('The Adventures of Batman')
mo3 is None<jupyter_output><empty_output><jupyter_text>Matching Specific Repetitions with Curly BracketsIf you have a group that you want to repeat a specific number of times, follow the group in your regex with a number in curly brackets. For example, the regex (Ha){3} will match the string 'HaHaHa', but it will not match 'HaHa', since the latter has only two repeats of the (Ha) group.Instead of one number, you can specify a range by writing a minimum, a comma, and a maximum in between the curly brackets. For example, the regex (Ha){3,5} will match 'HaHaHa', 'HaHaHaHa', and 'HaHaHaHaHa'.<jupyter_code>ha_regex = re.compile(r'(Ha){3}')
mo1 = ha_regex.search('HaHaHa')
mo1.group()
mo2 = ha_regex.search('Ha')
mo2 is None<jupyter_output><empty_output><jupyter_text>Greedy and Nongreedy MatchingPython’s regular expressions are greedy by default, which means that in ambiguous situations they will match the longest string possible. The non-greedy version of the curly brackets, which matches the shortest string possible, has the closing curly bracket followed by a question mark.<jupyter_code>greedy_ha_regex = re.compile(r'(Ha){3,5}')
mo1 = greedy_ha_regex.search('HaHaHaHaHa')
mo1.group()
nongreedy_ha_regex = re.compile(r'(Ha){3,5}?')
mo2 = nongreedy_ha_regex.search('HaHaHaHaHa')
mo2.group()<jupyter_output><empty_output><jupyter_text>The findall MethodIn addition to the search() method, Regex objects also have a findall() method. While search() will return a Match object of the first matched text in the searched string, the findall() method will return the strings of every match in the searched string.<jupyter_code>phone_num_regex = re.compile(r'\d\d\d-\d\d\d-\d\d\d\d') # has no groups
phone_num_regex.findall('Cell: 415-555-9999 Work: 212-555-0000')<jupyter_output><empty_output><jupyter_text>To summarize what the findall() method returns, remember the following:- When called on a regex with no groups, such as \d-\d\d\d-\d\d\d\d, the method findall() returns a list of ng matches, such as ['415-555-9999', '212-555-0000'].- When called on a regex that has groups, such as (\d\d\d)-d\d)-(\d\ d\d\d), the method findall() returns a list of es of strings (one string for each group), such as [('415', ', '9999'), ('212', '555', '0000')]. Making Your Own Character ClassesThere are times when you want to match a set of characters but the shorthand character classes (\d, \w, \s, and so on) are too broad. You can define your own character class using square brackets. For example, the character class [aeiouAEIOU] will match any vowel, both lowercase and uppercase.<jupyter_code>vowel_regex = re.compile(r'[aeiouAEIOU]')
vowel_regex.findall('Robocop eats baby food. BABY FOOD.')<jupyter_output><empty_output><jupyter_text>You can also include ranges of letters or numbers by using a hyphen. For example, the character class [a-zA-Z0-9] will match all lowercase letters, uppercase letters, and numbers.By placing a caret character (^) just after the character class’s opening bracket, you can make a negative character class. A negative character class will match all the characters that are not in the character class. For example, enter the following into the interactive shell:<jupyter_code>consonant_regex = re.compile(r'[^aeiouAEIOU]')
consonant_regex.findall('Robocop eats baby food. BABY FOOD.')<jupyter_output><empty_output><jupyter_text>The Caret and Dollar Sign Characters- You can also use the caret symbol (^) at the start of a regex to indicate that a match must occur at the beginning of the searched text.- Likewise, you can put a dollar sign ($) at the end of the regex to indicate the string must end with this regex pattern.- And you can use the ^ and $ together to indicate that the entire string must match the regex—that is, it’s not enough for a match to be made on some subset of the string.The r'^Hello' regular expression string matches strings that begin with 'Hello':<jupyter_code>begins_with_hello = re.compile(r'^Hello')
begins_with_hello.search('Hello world!')
begins_with_hello.search('He said hello.') is None<jupyter_output><empty_output><jupyter_text>The r'\d$' regular expression string matches strings that end with a numeric character from 0 to 9:<jupyter_code>whole_string_is_num = re.compile(r'^\d+$')
whole_string_is_num.search('1234567890')
whole_string_is_num.search('12345xyz67890') is None
whole_string_is_num.search('12 34567890') is None<jupyter_output><empty_output><jupyter_text>The Wildcard CharacterThe . (or dot) character in a regular expression is called a wildcard and will match any character except for a newline:<jupyter_code>at_regex = re.compile(r'.at')
at_regex.findall('The cat in the hat sat on the flat mat.')<jupyter_output><empty_output><jupyter_text>Matching Everything with Dot-Star<jupyter_code>name_regex = re.compile(r'First Name: (.*) Last Name: (.*)')
mo = name_regex.search('First Name: Some Last Name: One')
mo.group(1)
mo.group(2)<jupyter_output><empty_output><jupyter_text>The dot-star uses greedy mode: It will always try to match as much text as possible. To match any and all text in a nongreedy fashion, use the dot, star, and question mark (.*?). The question mark tells Python to match in a nongreedy way:<jupyter_code>nongreedy_regex = re.compile(r'<.*?>')
mo = nongreedy_regex.search('<To serve man> for dinner.>')
mo.group()
greedy_regex = re.compile(r'<.*>')
mo = greedy_regex.search('<To serve man> for dinner.>')
mo.group()<jupyter_output><empty_output><jupyter_text>Matching Newlines with the Dot CharacterThe dot-star will match everything except a newline. By passing re.DOTALL as the second argument to re.compile(), you can make the dot character match all characters, including the newline character:<jupyter_code>no_newline_regex = re.compile('.*')
no_newline_regex.search('Serve the public trust.\nProtect the innocent.\nUphold the law.').group()
newline_regex = re.compile('.*', re.DOTALL)
newline_regex.search('Serve the public trust.\nProtect the innocent.\nUphold the law.').group()<jupyter_output><empty_output><jupyter_text>Review of Regex Symbols| Symbol                   | Matches                                                      || ------------------------ | ------------------------------------------------------------ || `?`                      | zero or one of the preceding group.                          || `*`                      | zero or more of the preceding group.                         || `+`                      | one or more of the preceding group.                          || `{n}`                    | exactly n of the preceding group.                            || `{n,}`                   | n or more of the preceding group.                            || `{,m}`                   | 0 to m of the preceding group.                               || `{n,m}`                  | at least n and at most m of the preceding p.                 || `{n,m}?` or `*?` or `+?` | performs a nongreedy match of the preceding p.               || `^spam`                  | means the string must begin with spam.                       || `spam$`                  | means the string must end with spam.                         || `.`                      | any character, except newline characters.                    || `\d`, `\w`, and `\s`     | a digit, word, or space character, resectively.              || `\D`, `\W`, and `\S`     | anything except a digit, word, or space acter, respectively. || `[abc]`                  | any character between the brackets (such as a, b, ).         || `[^abc]`                 | any character that isn’t between the brackets.              | Case-Insensitive MatchingTo make your regex case-insensitive, you can pass re.IGNORECASE or re.I as a second argument to re.compile():<jupyter_code>robocop = re.compile(r'robocop', re.I)
robocop.search('Robocop is part man, part machine, all cop.').group()
robocop.search('ROBOCOP protects the innocent.').group()
robocop.search('Al, why does your programming book talk about robocop so much?').group()<jupyter_output><empty_output><jupyter_text>Substituting Strings with the sub() MethodThe sub() method for Regex objects is passed two arguments:1. The first argument is a string to replace any matches.1. The second is the string for the regular expression.The sub() method returns a string with the substitutions applied:<jupyter_code>names_regex = re.compile(r'Agent \w+')
names_regex.sub('CENSORED', 'Agent Alice gave the secret documents to Agent Bob.')<jupyter_output><empty_output><jupyter_text>Another example:<jupyter_code>agent_names_regex = re.compile(r'Agent (\w)\w*')
agent_names_regex.sub(r'\1****', 'Agent Alice told Agent Carol that Agent Eve knew Agent Bob was a double agent.')<jupyter_output><empty_output><jupyter_text>Managing Complex RegexesTo tell the re.compile() function to ignore whitespace and comments inside the regular expression string, “verbose mode” can be enabled by passing the variable re.VERBOSE as the second argument to re.compile().Now instead of a hard-to-read regular expression like this:<jupyter_code>phone_regex = re.compile(r'((\d{3}|\(\d{3}\))?(\s|-|\.)?\d{3}(\s|-|\.)\d{4}(\s*(ext|x|ext.)\s*\d{2,5})?)')<jupyter_output><empty_output><jupyter_text>you can spread the regular expression over multiple lines with comments like this:<jupyter_code>phone_regex = re.compile(r'''(
    (\d{3}|\(\d{3}\))?            # area code
    (\s|-|\.)?                    # separator
    \d{3}                         # first 3 digits
    (\s|-|\.)                     # separator
    \d{4}                         # last 4 digits
    (\s*(ext|x|ext.)\s*\d{2,5})?  # extension
    )''', re.VERBOSE)<jupyter_output><empty_output><jupyter_text>Handling File and Directory PathsThere are two main modules in Python that deals with path manipulation.One is the `os.path` module and the other is the `pathlib` module.The `pathlib` module was added in Python 3.4, offering an object-oriented wayto handle file system paths. Backslash on Windows and Forward Slash on OS X and LinuxOn Windows, paths are written using backslashes (\) as the separator betweenfolder names. On Unix based operating system such as macOS, Linux, and BSDs,the forward slash (/) is used as the path separator. Joining paths can bea headache if your code needs to work on different platforms.Fortunately, Python provides easy ways to handle this. We will showcasehow to deal with this with both `os.path.join` and `pathlib.Path.joinpath`Using `os.path.join` on Windows:<jupyter_code>import os

os.path.join('usr', 'bin', 'spam')<jupyter_output><empty_output><jupyter_text>And using `pathlib` on \*nix:<jupyter_code>from pathlib import Path

print(Path('usr').joinpath('bin').joinpath('spam'))<jupyter_output><empty_output><jupyter_text>`pathlib` also provides a shortcut to joinpath using the `/` operator:<jupyter_code>from pathlib import Path

print(Path('usr') / 'bin' / 'spam')<jupyter_output><empty_output><jupyter_text>Notice the path separator is different between Windows and Unix based operatingsystem, that's why you want to use one of the above methods instead ofadding strings together to join paths together.Joining paths is helpful if you need to create different file paths underthe same directory.Using `os.path.join` on Windows:<jupyter_code>my_files = ['accounts.txt', 'details.csv', 'invite.docx']

for filename in my_files:
    print(os.path.join('C:\\Users\\asweigart', filename))<jupyter_output><empty_output><jupyter_text>Using `pathlib` on \*nix:<jupyter_code>my_files = ['accounts.txt', 'details.csv', 'invite.docx']
home = Path.home()
for filename in my_files:
    print(home / filename)<jupyter_output><empty_output><jupyter_text>The Current Working DirectoryUsing `os` on Windows:<jupyter_code>import os

os.getcwd()
os.chdir('C:\\Windows\\System32')
os.getcwd()<jupyter_output><empty_output><jupyter_text>Using `pathlib` on \*nix:<jupyter_code>from pathlib import Path
from os import chdir

print(Path.cwd())
chdir('/usr/lib/python3.6')
print(Path.cwd())<jupyter_output><empty_output><jupyter_text>Creating New FoldersUsing `os` on Windows:<jupyter_code>import os

os.makedirs('C:\\delicious\\walnut\\waffles')<jupyter_output><empty_output><jupyter_text>Using `pathlib` on \*nix:<jupyter_code>from pathlib import Path
cwd = Path.cwd()
(cwd / 'delicious' / 'walnut' / 'waffles').mkdir()<jupyter_output><empty_output><jupyter_text>Oh no, we got a nasty error! The reason is that the 'delicious' directory doesnot exist, so we cannot make the 'walnut' and the 'waffles' directories underit. To fix this, do:<jupyter_code>from pathlib import Path
cwd = Path.cwd()
(cwd / 'delicious' / 'walnut' / 'waffles').mkdir(parents=True)<jupyter_output><empty_output><jupyter_text>And all is good :) Absolute vs. Relative PathsThere are two ways to specify a file path.- An absolute path, which always begins with the root folder- A relative path, which is relative to the program’s current working directoryThere are also the dot (.) and dot-dot (..) folders. These are not real folders but special names that can be used in a path. A single period (“dot”) for a folder name is shorthand for “this directory.” Two periods (“dot-dot”) means “the parent folder.” Handling Absolute and Relative PathsTo see if a path is an absolute path:Using `os.path` on \*nix:<jupyter_code>import os

os.path.isabs('/')
os.path.isabs('..')<jupyter_output><empty_output><jupyter_text>Using `pathlib` on \*nix:<jupyter_code>from pathlib import Path
Path('/').is_absolute()
Path('..').is_absolute()<jupyter_output><empty_output><jupyter_text>You can extract an absolute path with both `os.path` and `pathlib`Using `os.path` on \*nix:<jupyter_code>import os

os.getcwd()
os.path.abspath('..')<jupyter_output><empty_output><jupyter_text>Using `pathlib` on \*nix:<jupyter_code>from pathlib import Path

print(Path.cwd())
print(Path('..').resolve())<jupyter_output><empty_output><jupyter_text>You can get a relative path from a starting path to another path.Using `os.path` on \*nix:<jupyter_code>import os

os.path.relpath('/etc/passwd', '/')<jupyter_output><empty_output><jupyter_text>Using `pathlib` on \*nix:<jupyter_code>from pathlib import Path

print(Path('/etc/passwd').relative_to('/'))<jupyter_output><empty_output><jupyter_text>Checking Path ValidityChecking if a file/directory exists:Using `os.path` on \*nix:<jupyter_code>import os
os.path.exists('.')
os.path.exists('setup.py')
os.path.exists('/etc')
os.path.exists('nonexistentfile')<jupyter_output><empty_output><jupyter_text>Using `pathlib` on \*nix:<jupyter_code>from pathlib import Path
Path('.').exists()
Path('setup.py').exists()
Path('/etc').exists()
Path('nonexistentfile').exists()<jupyter_output><empty_output><jupyter_text>Checking if a path is a file:Using `os.path` on \*nix:<jupyter_code>import os
os.path.isfile('setup.py')
os.path.isfile('/home')
os.path.isfile('nonexistentfile')<jupyter_output><empty_output><jupyter_text>Using `pathlib` on \*nix:<jupyter_code>from pathlib import Path

Path('setup.py').is_file()
Path('/home').is_file()
Path('nonexistentfile').is_file()<jupyter_output><empty_output><jupyter_text>Checking if a path is a directory:Using `os.path` on \*nix:<jupyter_code>import os
os.path.isdir('/')
os.path.isdir('setup.py')
os.path.isdir('/spam')<jupyter_output><empty_output><jupyter_text>Using `pathlib` on \*nix:<jupyter_code>from pathlib import Path

Path('/').is_dir()
Path('setup.py').is_dir()
Path('/spam').is_dir()<jupyter_output><empty_output><jupyter_text>Finding File Sizes and Folder ContentsGetting a file's size in bytes:Using `os.path` on Windows:<jupyter_code>import os
os.path.getsize('C:\\Windows\\System32\\calc.exe')<jupyter_output><empty_output><jupyter_text>Using `pathlib` on \*nix:<jupyter_code>from pathlib import Path

stat = Path('/bin/python3.6').stat()
print(stat) # stat contains some other information about the file as well
print(stat.st_size) # size in bytes<jupyter_output><empty_output><jupyter_text>Listing directory contents using `os.listdir` on Windows:<jupyter_code>import os
os.listdir('C:\\Windows\\System32')<jupyter_output><empty_output><jupyter_text>Listing directory contents using `pathlib` on \*nix:<jupyter_code>from pathlib import Path

for f in Path('/usr/bin').iterdir():
    print(f)<jupyter_output><empty_output><jupyter_text>To find the total size of all the files in this directory:**WARNING**: Directories themselves also have a size! So you might want tocheck for whether a path is a file or directory using the methods in the methods discussed in the above section!Using `os.path.getsize()` and `os.listdir()` together on Windows:<jupyter_code>import os

total_size = 0

for filename in os.listdir('C:\\Windows\\System32'):
      total_size = total_size + os.path.getsize(os.path.join('C:\\Windows\\System32', filename))

print(total_size)<jupyter_output><empty_output><jupyter_text>Using `pathlib` on \*nix:<jupyter_code>from pathlib import Path
total_size = 0

for sub_path in Path('/usr/bin').iterdir():
    total_size += sub_path.stat().st_size

print(total_size)<jupyter_output><empty_output><jupyter_text>Copying Files and FoldersThe shutil module provides functions for copying files, as well as entire folders.<jupyter_code>import shutil, os

os.chdir('C:\\')

shutil.copy('C:\\spam.txt', 'C:\\delicious')
shutil.copy('eggs.txt', 'C:\\delicious\\eggs2.txt')
   'C:\\delicious\\eggs2.txt'<jupyter_output><empty_output><jupyter_text>While shutil.copy() will copy a single file, shutil.copytree() will copy an entire folder and every folder and file contained in it:<jupyter_code>import shutil, os

os.chdir('C:\\')
shutil.copytree('C:\\bacon', 'C:\\bacon_backup')<jupyter_output><empty_output><jupyter_text>Moving and Renaming Files and Folders<jupyter_code>import shutil

shutil.move('C:\\bacon.txt', 'C:\\eggs')<jupyter_output><empty_output><jupyter_text>The destination path can also specify a filename. In the following example, the source file is moved and renamed:<jupyter_code>shutil.move('C:\\bacon.txt', 'C:\\eggs\\new_bacon.txt')<jupyter_output><empty_output><jupyter_text>If there is no eggs folder, then move() will rename bacon.txt to a file named eggs.<jupyter_code>shutil.move('C:\\bacon.txt', 'C:\\eggs')<jupyter_output><empty_output><jupyter_text>Permanently Deleting Files and Folders- Calling os.unlink(path) or Path.unlink() will delete the file at path.- Calling os.rmdir(path) or Path.rmdir() will delete the folder at path. This folder must be empty of any files or folders.- Calling shutil.rmtree(path) will remove the folder at path, and all files and folders it contains will also be deleted. Safe Deletes with the send2trash Module You can install this module by running pip install send2trash from a Terminal window.<jupyter_code>import send2trash

with open('bacon.txt', 'a') as bacon_file: # creates the file
    bacon_file.write('Bacon is not a vegetable.')

send2trash.send2trash('bacon.txt')<jupyter_output><empty_output><jupyter_text>Walking a Directory Tree<jupyter_code>import os

for folder_name, subfolders, filenames in os.walk('C:\\delicious'):
    print('The current folder is {}'.format(folder_name))

    for subfolder in subfolders:
        print('SUBFOLDER OF {}: {}'.format(folder_name, subfolder))
    for filename in filenames:
        print('FILE INSIDE {}: {}'.format(folder_name, filename))

    print('')<jupyter_output><empty_output><jupyter_text>`pathlib` provides a lot more functionality than the ones listed above,like getting file name, getting file extension, reading/writing a file withoutmanually opening it, etc. Check out the[official documentation](https://docs.python.org/3/library/pathlib.html)if you want to know more! Reading and Writing Files The File Reading/Writing ProcessTo read/write to a file in Python, you will want to use the `with`statement, which will close the file for you after you are done. Opening and reading files with the open function<jupyter_code>with open('C:\\Users\\your_home_folder\\hello.txt') as hello_file:
    hello_content = hello_file.read()

hello_content<jupyter_output><empty_output><jupyter_text>Alternatively, you can use the *readlines()* method to get a list of string values from the file, one string for each line of text:<jupyter_code>with open('sonnet29.txt') as sonnet_file:
    sonnet_file.readlines()<jupyter_output><empty_output><jupyter_text>You can also iterate through the file line by line:<jupyter_code>with open('sonnet29.txt') as sonnet_file:
    for line in sonnet_file: # note the new line character will be included in the line
        print(line, end='')<jupyter_output><empty_output><jupyter_text>Writing to Files<jupyter_code>with open('bacon.txt', 'w') as bacon_file:
    bacon_file.write('Hello world!\n')
with open('bacon.txt', 'a') as bacon_file:
    bacon_file.write('Bacon is not a vegetable.')
with open('bacon.txt') as bacon_file:
    content = bacon_file.read()

print(content)<jupyter_output><empty_output><jupyter_text>Saving Variables with the shelve ModuleTo save variables:<jupyter_code>import shelve

cats = ['Zophie', 'Pooka', 'Simon']
with shelve.open('mydata') as shelf_file:
    shelf_file['cats'] = cats<jupyter_output><empty_output><jupyter_text>To open and read variables:<jupyter_code>with shelve.open('mydata') as shelf_file:
    print(type(shelf_file))
    print(shelf_file['cats'])<jupyter_output><empty_output><jupyter_text>Just like dictionaries, shelf values have keys() and values() methods that will return list-like values of the keys and values in the shelf. Since these methods return list-like values instead of true lists, you should pass them to the list() function to get them in list form.<jupyter_code>with shelve.open('mydata') as shelf_file:
    print(list(shelf_file.keys()))
    print(list(shelf_file.values()))<jupyter_output><empty_output><jupyter_text>Saving Variables with pprint.pformat<jupyter_code>import pprint

cats = [{'name': 'Zophie', 'desc': 'chubby'}, {'name': 'Pooka', 'desc': 'fluffy'}]
pprint.pformat(cats)
with open('myCats.py', 'w') as file_obj:
    file_obj.write('cats = {}\n'.format(pprint.pformat(cats)))<jupyter_output><empty_output><jupyter_text>Reading ZIP Files<jupyter_code>import zipfile, os

os.chdir('C:\\')    # move to the folder with example.zip
with zipfile.ZipFile('example.zip') as example_zip:
    print(example_zip.namelist())
    spam_info = example_zip.getinfo('spam.txt')
    print(spam_info.file_size)
    print(spam_info.compress_size)
    print('Compressed file is %sx smaller!' % (round(spam_info.file_size / spam_info.compress_size, 2)))<jupyter_output><empty_output><jupyter_text>Extracting from ZIP FilesThe extractall() method for ZipFile objects extracts all the files and folders from a ZIP file into the current working directory.<jupyter_code>import zipfile, os

os.chdir('C:\\')    # move to the folder with example.zip

with zipfile.ZipFile('example.zip') as example_zip:
    example_zip.extractall()<jupyter_output><empty_output><jupyter_text>The extract() method for ZipFile objects will extract a single file from the ZIP file. Continue the interactive shell example:<jupyter_code>with zipfile.ZipFile('example.zip') as example_zip:
    print(example_zip.extract('spam.txt'))
    print(example_zip.extract('spam.txt', 'C:\\some\\new\\folders'))<jupyter_output><empty_output><jupyter_text>Creating and Adding to ZIP Files<jupyter_code>import zipfile

with zipfile.ZipFile('new.zip', 'w') as new_zip:
    new_zip.write('spam.txt', compress_type=zipfile.ZIP_DEFLATED)<jupyter_output><empty_output><jupyter_text>This code will create a new ZIP file named new.zip that has the compressed contents of spam.txt. JSON, YAML and configuration files JSONOpen a JSON file with:<jupyter_code>import json
with open("filename.json", "r") as f:
    content = json.loads(f.read())<jupyter_output><empty_output><jupyter_text>Write a JSON file with:<jupyter_code>import json

content = {"name": "Joe", "age": 20}
with open("filename.json", "w") as f:
    f.write(json.dumps(content, indent=2))<jupyter_output><empty_output><jupyter_text>YAMLCompared to JSON, YAML allows a much better humain maintainance and gives ability to add comments.It is a convinient choice for configuration files where human will have to edit.There are two main librairies allowing to access to YAML files:- [PyYaml](https://pypi.python.org/pypi/PyYAML)- [Ruamel.yaml](https://pypi.python.org/pypi/ruamel.yaml)Install them using `pip install` in your virtual environment.The first one it easier to use but the second one, Ruamel, implements much better the YAMLspecification, and allow for example to modify a YAML content without altering comments.Open a YAML file with:<jupyter_code>from ruamel.yaml import YAML

with open("filename.yaml") as f:
    yaml=YAML()
    yaml.load(f)<jupyter_output><empty_output><jupyter_text>Anyconfig[Anyconfig](https://pypi.python.org/pypi/anyconfig) is a very handy package allowing to abstract completly the underlying configuration file format. It allows to load a Python dictionary from JSON, YAML, TOML, and so on.Install it with:<jupyter_code>%%bash
pip install anyconfig<jupyter_output><empty_output><jupyter_text>Usage:<jupyter_code>import anyconfig

conf1 = anyconfig.load("/path/to/foo/conf.d/a.yml")<jupyter_output><empty_output><jupyter_text>Debugging Raising ExceptionsExceptions are raised with a raise statement. In code, a raise statement consists of the following:- The raise keyword- A call to the Exception() function- A string with a helpful error message passed to the Exception() function<jupyter_code>raise Exception('This is the error message.')
Traceback (most recent call last):
  File "<pyshell#191>", line 1, in <module>
    raise Exception('This is the error message.')
Exception: This is the error message.<jupyter_output><empty_output><jupyter_text>Often it’s the code that calls the function, not the function itself, that knows how to handle an expection. So you will commonly see a raise statement inside a function and the try and except statements in the code calling the function.<jupyter_code>def box_print(symbol, width, height):
    if len(symbol) != 1:
      raise Exception('Symbol must be a single character string.')
    if width <= 2:
      raise Exception('Width must be greater than 2.')
    if height <= 2:
      raise Exception('Height must be greater than 2.')
    print(symbol * width)
    for i in range(height - 2):
        print(symbol + (' ' * (width - 2)) + symbol)
    print(symbol * width)
for sym, w, h in (('*', 4, 4), ('O', 20, 5), ('x', 1, 3), ('ZZ', 3, 3)):
    try:
        box_print(sym, w, h)
    except Exception as err:
        print('An exception happened: ' + str(err))<jupyter_output><empty_output><jupyter_text>Getting the Traceback as a StringThe traceback is displayed by Python whenever a raised exception goes unhandled. But can also obtain it as a string by calling traceback.format_exc(). This function is useful if you want the information from an exception’s traceback but also want an except statement to gracefully handle the exception. You will need to import Python’s traceback module before calling this function.<jupyter_code>import traceback

try:
     raise Exception('This is the error message.')
except:
     with open('errorInfo.txt', 'w') as error_file:
         error_file.write(traceback.format_exc())
     print('The traceback info was written to errorInfo.txt.')<jupyter_output><empty_output><jupyter_text>The 116 is the return value from the write() method, since 116 characters were written to the file. The traceback text was written to errorInfo.txt.<jupyter_code>Traceback (most recent call last):
  File "<pyshell#28>", line 2, in <module>
Exception: This is the error message.<jupyter_output><empty_output><jupyter_text>AssertionsAn assertion is a sanity check to make sure your code isn’t doing something obviously wrong. These sanity checks are performed by assert statements. If the sanity check fails, then an AssertionError exception is raised. In code, an assert statement consists of the following:- The assert keyword- A condition (that is, an expression that evaluates to True or False)- A comma- A string to display when the condition is False<jupyter_code>pod_bay_door_status = 'open'

assert pod_bay_door_status == 'open', 'The pod bay doors need to be "open".'

pod_bay_door_status = 'I\'m sorry, Dave. I\'m afraid I can\'t do that.'

assert pod_bay_door_status == 'open', 'The pod bay doors need to be "open".'<jupyter_output><empty_output><jupyter_text>In plain English, an assert statement says, “I assert that this condition holds true, and if not, there is a bug somewhere in the program.” Unlike exceptions, your code should not handle assert statements with try and except; if an assert fails, your program should crash. By failing fast like this, you shorten the time between the original cause of the bug and when you first notice the bug. This will reduce the amount of code you will have to check before finding the code that’s causing the bug.Disabling AssertionsAssertions can be disabled by passing the -O option when running Python. LoggingTo enable the logging module to display log messages on your screen as your program runs, copy the following to the top of your program (but under the ! python shebang line):<jupyter_code>import logging

logging.basicConfig(level=logging.DEBUG, format=' %(asctime)s - %(levelname)s- %(message)s')<jupyter_output><empty_output><jupyter_text>Say you wrote a function to calculate the factorial of a number. In mathematics, factorial 4 is 1 × 2 × 3 × 4, or 24. Factorial 7 is 1 × 2 × 3 × 4 × 5 × 6 × 7, or 5,040. Open a new file editor window and enter the following code. It has a bug in it, but you will also enter several log messages to help yourself figure out what is going wrong. Save the program as factorialLog.py.<jupyter_code>import logging

logging.basicConfig(level=logging.DEBUG, format=' %(asctime)s - %(levelname)s- %(message)s')

logging.debug('Start of program')

def factorial(n):

    logging.debug('Start of factorial(%s)' % (n))
    total = 1

    for i in range(1, n + 1):
        total *= i
        logging.debug('i is ' + str(i) + ', total is ' + str(total))

    logging.debug('End of factorial(%s)' % (n))

    return total

print(factorial(5))
logging.debug('End of program')<jupyter_output><empty_output><jupyter_text>Logging LevelsLogging levels provide a way to categorize your log messages by importance. There are five logging levels, described in Table 10-1 from least to most important. Messages can be logged at each level using a different logging function.| Level      | Logging Function     | Description                                                                                                                    || ---------- | -------------------- | ------------------------------------------------------------------------------------------------------------------------------ || `DEBUG`    | `logging.debug()`    | The lowest level. Used for small details. Usually you care about these messages only when diagnosing problems.                 || `INFO`     | `logging.info()`     | Used to record information on general events in your program or confirm that things are working at their point in the program. || `WARNING`  | `logging.warning()`  | Used to indicate a potential problem that doesn’t prevent the program from working but might do so in the future.             || `ERROR`    | `logging.error()`    | Used to record an error that caused the program to fail to do something.                                                       || `CRITICAL` | `logging.critical()` | The highest level. Used to indicate a fatal error that has caused or is about to cause the program to stop running entirely.   | Disabling LoggingAfter you’ve debugged your program, you probably don’t want all these log messages cluttering the screen. The logging.disable() function disables these so that you don’t have to go into your program and remove all the logging calls by hand.<jupyter_code>import logging

logging.basicConfig(level=logging.INFO, format=' %(asctime)s -%(levelname)s - %(message)s')
logging.critical('Critical error! Critical error!')
logging.disable(logging.CRITICAL)
logging.critical('Critical error! Critical error!')
logging.error('Error! Error!')<jupyter_output><empty_output><jupyter_text>Logging to a FileInstead of displaying the log messages to the screen, you can write them to a text file. The logging.basicConfig() function takes a filename keyword argument, like so:<jupyter_code>import logging

logging.basicConfig(filename='myProgramLog.txt', level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')<jupyter_output><empty_output><jupyter_text>Lambda FunctionsThis function:<jupyter_code>def add(x, y):
        return x + y

add(5, 3)<jupyter_output><empty_output><jupyter_text>Is equivalent to the *lambda* function:<jupyter_code>add = lambda x, y: x + y
add(5, 3)<jupyter_output><empty_output><jupyter_text>It's not even need to bind it to a name like add before:<jupyter_code>(lambda x, y: x + y)(5, 3)<jupyter_output><empty_output><jupyter_text>Like regular nested functions, lambdas also work as lexical closures:<jupyter_code>def make_adder(n):
        return lambda x: x + n

plus_3 = make_adder(3)
plus_5(4)<jupyter_output><empty_output><jupyter_text>Note: lambda can only evaluate an expression, like a single line of code. Ternary Conditional OperatorMany programming languages have a ternary operator, which define a conditional expression. The most common usage is to make a terse simple conditional assignment statement. In other words, it offers one-line code to evaluate the first expression if the condition is true, otherwise it evaluates the second expression.<jupyter_code><expression1> if <condition> else <expression2><jupyter_output><empty_output><jupyter_text>Example:<jupyter_code>age = 15

print('kid' if age < 18 else 'adult')<jupyter_output><empty_output><jupyter_text>Ternary operators can be changed:<jupyter_code>age = 15
print('kid' if age < 13 else 'teenager' if age < 18 else 'adult')<jupyter_output><empty_output><jupyter_text>The code above is equivalent to:<jupyter_code>if age < 18:
    if age < 12:
        print('kid')
    else:
        print('teenager')
else:
    print('adult')<jupyter_output><empty_output><jupyter_text>args and kwargsThe names ```args and kwargs``` are arbitrary - the important thing are the ```*``` and ```**``` operators. They can mean:1. In a function declaration, ```*``` means “pack all remaining positional arguments into a tuple named ``”, while ```**``` is the same for keyword arguments (except it uses a dictionary, not a tuple).2. In a function call, ```*``` means “unpack tuple or list named `` to positional arguments at this position”, while ```**``` is the same for keyword arguments.For example you can make a function that you can use to call any other function, no matter what parameters it has:<jupyter_code>def forward(f, *args, **kwargs):
    return f(*args, **kwargs)<jupyter_output><empty_output><jupyter_text>Inside forward, args is a tuple (of all positional arguments except the first one, because we specified it - the f), kwargs is a dict. Then we call f and unpack them so they become normal arguments to f.You use ```*args``` when you have an indefinite amount of positional arguments.<jupyter_code>def fruits(*args):
   for fruit in args:
      print(fruit)

fruits("apples", "bananas", "grapes")<jupyter_output><empty_output><jupyter_text>Similarly, you use ```**kwargs``` when you have an indefinite number of keyword arguments.<jupyter_code>def fruit(**kwargs):
   for key, value in kwargs.items():
       print("{0}: {1}".format(key, value))

fruit(name = "apple", color = "red")
def show(arg1, arg2, *args, kwarg1=None, kwarg2=None, **kwargs):
  print(arg1)
  print(arg2)
  print(args)
  print(kwarg1)
  print(kwarg2)
  print(kwargs)

data1 = [1,2,3]
data2 = [4,5,6]
data3 = {'a':7,'b':8,'c':9}

show(*data1,*data2, kwarg1="python",kwarg2="cheatsheet",**data3)
show(*data1, *data2, **data3)<jupyter_output><empty_output><jupyter_text>If you do not specify ** for kwargs<jupyter_code>show(*data1, *data2, *data3)<jupyter_output><empty_output><jupyter_text>Thinks to Remember(args)1. Functions can accept a variable number of positional arguments by using ```*args``` in the def statement.2. You can use the items from a sequence as the positional arguments for a function with the ```*``` operator.3. Using the ```*``` operator with a generator may cause your program to run out of memory and crash.4. Adding new positional parameters to functions that accept ```*args``` can introduce hard-to-find bugs. Thinks to remember(kwargs)1. Function arguments can be specified by position or by keyword.2. Keywords make it clear what the purpose of each argument is when it would be confusing with only positional arguments.3. Keyword arguments with default values make it easy to add new behaviors to a function, especially when the function has existing callers.4. Optional keyword arguments should always be passed by keyword instead of by position. Context ManagerWhile Python's context managers are widely used, few understand the purpose behind their use. These statements, commonly used with reading and writing files, assist the application in conserving system memory and improve resource management by ensuring specific resources are only in use for certain processes. with statementA context manager is an object that is notified when a context (a block of code) starts and ends. You commonly use one with the with statement. It takes care of the notifying.For example, file objects are context managers. When a context ends, the file object is closed automatically:<jupyter_code>with open(filename) as f:
    file_contents = f.read()

# the open_file object has automatically been closed.<jupyter_output><empty_output><jupyter_text>Anything that ends execution of the block causes the context manager's exit method to be called. This includes exceptions, and can be useful when an error causes you to prematurely exit from an open file or connection. Exiting a script without properly closing files/connections is a bad idea, that may cause data loss or other problems. By using a context manager you can ensure that precautions are always taken to prevent damage or loss in this way. Writing your own contextmanager using generator syntaxIt is also possible to write a context manager using generator syntax thanks to the ```contextlib.contextmanager``` decorator:<jupyter_code>import contextlib

@contextlib.contextmanager
def context_manager(num):
    print('Enter')
    yield num + 1
    print('Exit')
with context_manager(2) as cm:
    # the following instructions are run when the 'yield' point of the context
    # manager is reached.
    # 'cm' will have the value that was yielded
    print('Right in the middle with cm = {}'.format(cm))<jupyter_output><empty_output><jupyter_text>`__main__` Top-level script environment`__main__` is the name of the scope in which top-level code executes.A module’s __name__ is set equal to `__main__` when read from standard input, a script, or from an interactive prompt.A module can discover whether or not it is running in the main scope by checking its own `__name__`, which allows a common idiom for conditionally executing code in a module when it is run as a script or with `python -m` but not when it is imported:<jupyter_code>if __name__ == "__main__":
    # execute only if run as a script
    main()<jupyter_output><empty_output><jupyter_text>For a package, the same effect can be achieved by including a __main__.py module, the contents of which will be executed when the module is run with -m.For example we are developing script which is designed to be used as module, we should do:<jupyter_code># Python program to execute function directly
def add(a, b):
    return a+b

add(10, 20) # we can test it by calling the function save it as calculate.py
# Now if we want to use that module by importing we have to comment out our call,
# Instead we can write like this in calculate.py
if __name__ == "__main__":
    add(3, 5)
import calculate

calculate.add(3, 5)<jupyter_output><empty_output><jupyter_text>Advantages1. Every Python module has it’s `__name__` defined and if this is `__main__`, it implies that the module is being run standalone by the user and we can do corresponding appropriate actions.2. If you import this script as a module in another script, the __name__ is set to the name of the script/module.3. Python files can act as either reusable modules, or as standalone programs.4. if `__name__ == “main”:` is used to execute some code only if the file was run directly, and not imported. setup.pyThe setup script is the centre of all activity in building, distributing, and installing modules using the Distutils. The main purpose of the setup script is to describe your module distribution to the Distutils, so that the various commands that operate on your modules do the right thing.The `setup.py` file is at the heart of a Python project. It describes all of the metadata about your project. There a quite a few fields you can add to a project to give it a rich set of metadata describing the project. However, there are only three required fields: name, version, and packages. The name field must be unique if you wish to publish your package on the Python Package Index (PyPI). The version field keeps track of different releases of the project. The packages field describes where you’ve put the Python source code within your project.This allows you to easily install Python packages. Often it's enough to write:<jupyter_code>%%bash
python setup.py install<jupyter_output><empty_output><jupyter_text>and module will install itself.Our initial setup.py will also include information about the license and will re-use the README.txt file for the long_description field. This will look like:<jupyter_code>from distutils.core import setup
setup(
   name='pythonCheatsheet',
   version='0.1',
   packages=['pipenv',],
   license='MIT',
   long_description=open('README.txt').read(),
)<jupyter_output><empty_output><jupyter_text>Find more information visit [http://docs.python.org/install/index.html](http://docs.python.org/install/index.html). Dataclasses`Dataclasses` are python classes but are suited for storing data objects.This module provides a decorator and functions for automatically adding generated special methods such as `__init__()` and `__repr__()` to user-defined classes. Features1. They store data and represent a certain data type. Ex: A number. For people familiar with ORMs, a model instance is a data object. It represents a specific kind of entity. It holds attributes that define or represent the entity.2. They can be compared to other objects of the same type. Ex: A number can be greater than, less than, or equal to another number.Python 3.7 provides a decorator dataclass that is used to convert a class into a dataclass.python 2.7<jupyter_code>class Number:
    def __init__(self, val):
        self.val = val

obj = Number(2)
obj.val<jupyter_output><empty_output><jupyter_text>with dataclass<jupyter_code>from dataclasses import dataclass

@dataclass
class Number:
    val: int

obj = Number(2)
obj.val<jupyter_output><empty_output><jupyter_text>Default valuesIt is easy to add default values to the fields of your data class.<jupyter_code>from dataclasses import dataclass

@dataclass
class Product:
    name: str
    count: int = 0
    price: float = 0.0

obj = Product("Python")
obj.name
obj.count
obj.price<jupyter_output><empty_output><jupyter_text>Type hintsIt is mandatory to define the data type in dataclass. However, If you don't want specify the datatype then, use ```typing.Any```.<jupyter_code>from dataclasses import dataclass
from typing import Any

@dataclass
class WithoutExplicitTypes:
   name: Any
   value: Any = 42<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12128209546786145, 'fraction_numerical': 0.01098282497558734, 'mean_word_length': 5.0427629295383545, 'pattern_counts': {'":': 4, '<': 1104, '<?xml version=': 0, '>': 1093, 'https://': 10, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 22, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '267', 'dir': 'jupyter-structured-clean-dedup', 'id': '199858', 'license': 'MIT', 'n_tokens_mistral': 28930, 'n_tokens_neox': 27637, 'n_words': 8939, 'path': 'notebooks/The Python Notebook.ipynb', 'repo_name': 'ninjakwarl/DXCPythonBootCamp', 'size': '87083'}