[38;5;246;03m# Single line comments start with a number symbol.[39;00m

[38;5;214m""" Multiline strings can be written[39m
[38;5;214m    using three "s, and are often used[39m
[38;5;214m    as documentation.[39m
[38;5;214m"""[39m

[38;5;246;03m####################################################[39;00m
[38;5;246;03m## 1. Primitive Datatypes and Operators[39;00m
[38;5;246;03m####################################################[39;00m

[38;5;246;03m# You have numbers[39;00m
[38;5;67m3[39m[38;5;252m  [39m[38;5;246;03m# => 3[39;00m

[38;5;246;03m# Math is what you would expect[39;00m
[38;5;67m1[39m[38;5;252m [39m[38;5;252m+[39m[38;5;252m [39m[38;5;67m1[39m[38;5;252m   [39m[38;5;246;03m# => 2[39;00m
[38;5;67m8[39m[38;5;252m [39m[38;5;252m-[39m[38;5;252m [39m[38;5;67m1[39m[38;5;252m   [39m[38;5;246;03m# => 7[39;00m
[38;5;67m10[39m[38;5;252m [39m[38;5;252m*[39m[38;5;252m [39m[38;5;67m2[39m[38;5;252m  [39m[38;5;246;03m# => 20[39;00m
[38;5;67m35[39m[38;5;252m [39m[38;5;252m/[39m[38;5;252m [39m[38;5;67m5[39m[38;5;252m  [39m[38;5;246;03m# => 7.0[39;00m

[38;5;246;03m# Integer division rounds down for both positive and negative numbers.[39;00m
[38;5;67m5[39m[38;5;252m [39m[38;5;252m/[39m[38;5;252m/[39m[38;5;252m [39m[38;5;67m3[39m[38;5;252m       [39m[38;5;246;03m# => 1[39;00m
[38;5;252m-[39m[38;5;67m5[39m[38;5;252m [39m[38;5;252m/[39m[38;5;252m/[39m[38;5;252m [39m[38;5;67m3[39m[38;5;252m      [39m[38;5;246;03m# => -2[39;00m
[38;5;67m5.0[39m[38;5;252m [39m[38;5;252m/[39m[38;5;252m/[39m[38;5;252m [39m[38;5;67m3.0[39m[38;5;252m   [39m[38;5;246;03m# => 1.0 # works on floats too[39;00m
[38;5;252m-[39m[38;5;67m5.0[39m[38;5;252m [39m[38;5;252m/[39m[38;5;252m/[39m[38;5;252m [39m[38;5;67m3.0[39m[38;5;252m  [39m[38;5;246;03m# => -2.0[39;00m

[38;5;246;03m# The result of division is always a float[39;00m
[38;5;67m10.0[39m[38;5;252m [39m[38;5;252m/[39m[38;5;252m [39m[38;5;67m3[39m[38;5;252m  [39m[38;5;246;03m# => 3.3333333333333335[39;00m

[38;5;246;03m# Modulo operation[39;00m
[38;5;67m7[39m[38;5;252m [39m[38;5;252m%[39m[38;5;252m [39m[38;5;67m3[39m[38;5;252m   [39m[38;5;246;03m# => 1[39;00m
[38;5;246;03m# i % j have the same sign as j, unlike C[39;00m
[38;5;252m-[39m[38;5;67m7[39m[38;5;252m [39m[38;5;252m%[39m[38;5;252m [39m[38;5;67m3[39m[38;5;252m  [39m[38;5;246;03m# => 2[39;00m

[38;5;246;03m# Exponentiation (x**y, x to the yth power)[39;00m
[38;5;67m2[39m[38;5;252m*[39m[38;5;252m*[39m[38;5;67m3[39m[38;5;252m  [39m[38;5;246;03m# => 8[39;00m

[38;5;246;03m# Enforce precedence with parentheses[39;00m
[38;5;67m1[39m[38;5;252m [39m[38;5;252m+[39m[38;5;252m [39m[38;5;67m3[39m[38;5;252m [39m[38;5;252m*[39m[38;5;252m [39m[38;5;67m2[39m[38;5;252m    [39m[38;5;246;03m# => 7[39;00m
[38;5;252m([39m[38;5;67m1[39m[38;5;252m [39m[38;5;252m+[39m[38;5;252m [39m[38;5;67m3[39m[38;5;252m)[39m[38;5;252m [39m[38;5;252m*[39m[38;5;252m [39m[38;5;67m2[39m[38;5;252m  [39m[38;5;246;03m# => 8[39;00m

[38;5;246;03m# Boolean values are primitives (Note: the capitalization)[39;00m
[38;5;70;01mTrue[39;00m[38;5;252m   [39m[38;5;246;03m# => True[39;00m
[38;5;70;01mFalse[39;00m[38;5;252m  [39m[38;5;246;03m# => False[39;00m

[38;5;246;03m# negate with not[39;00m
[38;5;70;01mnot[39;00m[38;5;252m [39m[38;5;70;01mTrue[39;00m[38;5;252m   [39m[38;5;246;03m# => False[39;00m
[38;5;70;01mnot[39;00m[38;5;252m [39m[38;5;70;01mFalse[39;00m[38;5;252m  [39m[38;5;246;03m# => True[39;00m

[38;5;246;03m# Boolean Operators[39;00m
[38;5;246;03m# Note "and" and "or" are case-sensitive[39;00m
[38;5;70;01mTrue[39;00m[38;5;252m [39m[38;5;70;01mand[39;00m[38;5;252m [39m[38;5;70;01mFalse[39;00m[38;5;252m  [39m[38;5;246;03m# => False[39;00m
[38;5;70;01mFalse[39;00m[38;5;252m [39m[38;5;70;01mor[39;00m[38;5;252m [39m[38;5;70;01mTrue[39;00m[38;5;252m   [39m[38;5;246;03m# => True[39;00m

[38;5;246;03m# True and False are actually 1 and 0 but with different keywords[39;00m
[38;5;70;01mTrue[39;00m[38;5;252m [39m[38;5;252m+[39m[38;5;252m [39m[38;5;70;01mTrue[39;00m[38;5;252m [39m[38;5;246;03m# => 2[39;00m
[38;5;70;01mTrue[39;00m[38;5;252m [39m[38;5;252m*[39m[38;5;252m [39m[38;5;67m8[39m[38;5;252m    [39m[38;5;246;03m# => 8[39;00m
[38;5;70;01mFalse[39;00m[38;5;252m [39m[38;5;252m-[39m[38;5;252m [39m[38;5;67m5[39m[38;5;252m   [39m[38;5;246;03m# => -5[39;00m

[38;5;246;03m# Comparison operators look at the numerical value of True and False[39;00m
[38;5;67m0[39m[38;5;252m [39m[38;5;252m==[39m[38;5;252m [39m[38;5;70;01mFalse[39;00m[38;5;252m  [39m[38;5;246;03m# => True[39;00m
[38;5;67m1[39m[38;5;252m [39m[38;5;252m==[39m[38;5;252m [39m[38;5;70;01mTrue[39;00m[38;5;252m   [39m[38;5;246;03m# => True[39;00m
[38;5;67m2[39m[38;5;252m [39m[38;5;252m==[39m[38;5;252m [39m[38;5;70;01mTrue[39;00m[38;5;252m   [39m[38;5;246;03m# => False[39;00m
[38;5;252m-[39m[38;5;67m5[39m[38;5;252m [39m[38;5;252m!=[39m[38;5;252m [39m[38;5;70;01mFalse[39;00m[38;5;252m [39m[38;5;246;03m# => True[39;00m

[38;5;246;03m# Using boolean logical operators on ints casts them to booleans for evaluation, but their non-cast value is returned[39;00m
[38;5;246;03m# Don't mix up with bool(ints) and bitwise and/or (&,|)[39;00m
[38;5;31mbool[39m[38;5;252m([39m[38;5;67m0[39m[38;5;252m)[39m[38;5;252m     [39m[38;5;246;03m# => False[39;00m
[38;5;31mbool[39m[38;5;252m([39m[38;5;67m4[39m[38;5;252m)[39m[38;5;252m     [39m[38;5;246;03m# => True[39;00m
[38;5;31mbool[39m[38;5;252m([39m[38;5;252m-[39m[38;5;67m6[39m[38;5;252m)[39m[38;5;252m    [39m[38;5;246;03m# => True[39;00m
[38;5;67m0[39m[38;5;252m [39m[38;5;70;01mand[39;00m[38;5;252m [39m[38;5;67m2[39m[38;5;252m     [39m[38;5;246;03m# => 0[39;00m
[38;5;252m-[39m[38;5;67m5[39m[38;5;252m [39m[38;5;70;01mor[39;00m[38;5;252m [39m[38;5;67m0[39m[38;5;252m     [39m[38;5;246;03m# => -5[39;00m

[38;5;246;03m# Equality is ==[39;00m
[38;5;67m1[39m[38;5;252m [39m[38;5;252m==[39m[38;5;252m [39m[38;5;67m1[39m[38;5;252m  [39m[38;5;246;03m# => True[39;00m
[38;5;67m2[39m[38;5;252m [39m[38;5;252m==[39m[38;5;252m [39m[38;5;67m1[39m[38;5;252m  [39m[38;5;246;03m# => False[39;00m

[38;5;246;03m# Inequality is !=[39;00m
[38;5;67m1[39m[38;5;252m [39m[38;5;252m!=[39m[38;5;252m [39m[38;5;67m1[39m[38;5;252m  [39m[38;5;246;03m# => False[39;00m
[38;5;67m2[39m[38;5;252m [39m[38;5;252m!=[39m[38;5;252m [39m[38;5;67m1[39m[38;5;252m  [39m[38;5;246;03m# => True[39;00m

[38;5;246;03m# More comparisons[39;00m
[38;5;67m1[39m[38;5;252m [39m[38;5;252m<[39m[38;5;252m [39m[38;5;67m10[39m[38;5;252m  [39m[38;5;246;03m# => True[39;00m
[38;5;67m1[39m[38;5;252m [39m[38;5;252m>[39m[38;5;252m [39m[38;5;67m10[39m[38;5;252m  [39m[38;5;246;03m# => False[39;00m
[38;5;67m2[39m[38;5;252m [39m[38;5;252m<[39m[38;5;252m=[39m[38;5;252m [39m[38;5;67m2[39m[38;5;252m  [39m[38;5;246;03m# => True[39;00m
[38;5;67m2[39m[38;5;252m [39m[38;5;252m>[39m[38;5;252m=[39m[38;5;252m [39m[38;5;67m2[39m[38;5;252m  [39m[38;5;246;03m# => True[39;00m

[38;5;246;03m# Seeing whether a value is in a range[39;00m
[38;5;67m1[39m[38;5;252m [39m[38;5;252m<[39m[38;5;252m [39m[38;5;67m2[39m[38;5;252m [39m[38;5;70;01mand[39;00m[38;5;252m [39m[38;5;67m2[39m[38;5;252m [39m[38;5;252m<[39m[38;5;252m [39m[38;5;67m3[39m[38;5;252m  [39m[38;5;246;03m# => True[39;00m
[38;5;67m2[39m[38;5;252m [39m[38;5;252m<[39m[38;5;252m [39m[38;5;67m3[39m[38;5;252m [39m[38;5;70;01mand[39;00m[38;5;252m [39m[38;5;67m3[39m[38;5;252m [39m[38;5;252m<[39m[38;5;252m [39m[38;5;67m2[39m[38;5;252m  [39m[38;5;246;03m# => False[39;00m
[38;5;246;03m# Chaining makes this look nicer[39;00m
[38;5;67m1[39m[38;5;252m [39m[38;5;252m<[39m[38;5;252m [39m[38;5;67m2[39m[38;5;252m [39m[38;5;252m<[39m[38;5;252m [39m[38;5;67m3[39m[38;5;252m  [39m[38;5;246;03m# => True[39;00m
[38;5;67m2[39m[38;5;252m [39m[38;5;252m<[39m[38;5;252m [39m[38;5;67m3[39m[38;5;252m [39m[38;5;252m<[39m[38;5;252m [39m[38;5;67m2[39m[38;5;252m  [39m[38;5;246;03m# => False[39;00m

[38;5;246;03m# (is vs. ==) is checks if two variables refer to the same object, but == checks[39;00m
[38;5;246;03m# if the objects pointed to have the same values.[39;00m
[38;5;252ma[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252m[[39m[38;5;67m1[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m2[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m3[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m4[39m[38;5;252m][39m[38;5;252m  [39m[38;5;246;03m# Point a at a new list, [1, 2, 3, 4][39;00m
[38;5;252mb[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252ma[39m[38;5;252m             [39m[38;5;246;03m# Point b at what a is pointing to[39;00m
[38;5;252mb[39m[38;5;252m [39m[38;5;70;01mis[39;00m[38;5;252m [39m[38;5;252ma[39m[38;5;252m            [39m[38;5;246;03m# => True, a and b refer to the same object[39;00m
[38;5;252mb[39m[38;5;252m [39m[38;5;252m==[39m[38;5;252m [39m[38;5;252ma[39m[38;5;252m            [39m[38;5;246;03m# => True, a's and b's objects are equal[39;00m
[38;5;252mb[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252m[[39m[38;5;67m1[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m2[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m3[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m4[39m[38;5;252m][39m[38;5;252m  [39m[38;5;246;03m# Point b at a new list, [1, 2, 3, 4][39;00m
[38;5;252mb[39m[38;5;252m [39m[38;5;70;01mis[39;00m[38;5;252m [39m[38;5;252ma[39m[38;5;252m            [39m[38;5;246;03m# => False, a and b do not refer to the same object[39;00m
[38;5;252mb[39m[38;5;252m [39m[38;5;252m==[39m[38;5;252m [39m[38;5;252ma[39m[38;5;252m            [39m[38;5;246;03m# => True, a's and b's objects are equal[39;00m

[38;5;246;03m# Strings are created with " or '[39;00m
[38;5;214m"[39m[38;5;214mThis is a string.[39m[38;5;214m"[39m
[38;5;214m'[39m[38;5;214mThis is also a string.[39m[38;5;214m'[39m

[38;5;246;03m# Strings can be added too[39;00m
[38;5;214m"[39m[38;5;214mHello [39m[38;5;214m"[39m[38;5;252m [39m[38;5;252m+[39m[38;5;252m [39m[38;5;214m"[39m[38;5;214mworld![39m[38;5;214m"[39m[38;5;252m  [39m[38;5;246;03m# => "Hello world!"[39;00m
[38;5;246;03m# String literals (but not variables) can be concatenated without using '+'[39;00m
[38;5;214m"[39m[38;5;214mHello [39m[38;5;214m"[39m[38;5;252m [39m[38;5;214m"[39m[38;5;214mworld![39m[38;5;214m"[39m[38;5;252m    [39m[38;5;246;03m# => "Hello world!"[39;00m

[38;5;246;03m# A string can be treated like a list of characters[39;00m
[38;5;214m"[39m[38;5;214mHello world![39m[38;5;214m"[39m[38;5;252m[[39m[38;5;67m0[39m[38;5;252m][39m[38;5;252m  [39m[38;5;246;03m# => 'H'[39;00m

[38;5;246;03m# You can find the length of a string[39;00m
[38;5;31mlen[39m[38;5;252m([39m[38;5;214m"[39m[38;5;214mThis is a string[39m[38;5;214m"[39m[38;5;252m)[39m[38;5;252m  [39m[38;5;246;03m# => 16[39;00m

[38;5;246;03m# You can also format using f-strings or formatted string literals (in Python 3.6+)[39;00m
[38;5;252mname[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;214m"[39m[38;5;214mReiko[39m[38;5;214m"[39m
[38;5;214mf[39m[38;5;214m"[39m[38;5;214mShe said her name is [39m[38;5;214m{[39m[38;5;252mname[39m[38;5;214m}[39m[38;5;214m.[39m[38;5;214m"[39m[38;5;252m [39m[38;5;246;03m# => "She said her name is Reiko"[39;00m
[38;5;246;03m# You can basically put any Python expression inside the braces and it will be output in the string.[39;00m
[38;5;214mf[39m[38;5;214m"[39m[38;5;214m{[39m[38;5;252mname[39m[38;5;214m}[39m[38;5;214m is [39m[38;5;214m{[39m[38;5;31mlen[39m[38;5;252m([39m[38;5;252mname[39m[38;5;252m)[39m[38;5;214m}[39m[38;5;214m characters long.[39m[38;5;214m"[39m[38;5;252m [39m[38;5;246;03m# => "Reiko is 5 characters long."[39;00m

[38;5;246;03m# None is an object[39;00m
[38;5;70;01mNone[39;00m[38;5;252m  [39m[38;5;246;03m# => None[39;00m

[38;5;246;03m# Don't use the equality "==" symbol to compare objects to None[39;00m
[38;5;246;03m# Use "is" instead. This checks for equality of object identity.[39;00m
[38;5;214m"[39m[38;5;214metc[39m[38;5;214m"[39m[38;5;252m [39m[38;5;70;01mis[39;00m[38;5;252m [39m[38;5;70;01mNone[39;00m[38;5;252m  [39m[38;5;246;03m# => False[39;00m
[38;5;70;01mNone[39;00m[38;5;252m [39m[38;5;70;01mis[39;00m[38;5;252m [39m[38;5;70;01mNone[39;00m[38;5;252m   [39m[38;5;246;03m# => True[39;00m

[38;5;246;03m# None, 0, and empty strings/lists/dicts/tuples all evaluate to False.[39;00m
[38;5;246;03m# All other values are True[39;00m
[38;5;31mbool[39m[38;5;252m([39m[38;5;67m0[39m[38;5;252m)[39m[38;5;252m   [39m[38;5;246;03m# => False[39;00m
[38;5;31mbool[39m[38;5;252m([39m[38;5;214m"[39m[38;5;214m"[39m[38;5;252m)[39m[38;5;252m  [39m[38;5;246;03m# => False[39;00m
[38;5;31mbool[39m[38;5;252m([39m[38;5;252m[[39m[38;5;252m][39m[38;5;252m)[39m[38;5;252m  [39m[38;5;246;03m# => False[39;00m
[38;5;31mbool[39m[38;5;252m([39m[38;5;252m{[39m[38;5;252m}[39m[38;5;252m)[39m[38;5;252m  [39m[38;5;246;03m# => False[39;00m
[38;5;31mbool[39m[38;5;252m([39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m)[39m[38;5;252m  [39m[38;5;246;03m# => False[39;00m

[38;5;246;03m####################################################[39;00m
[38;5;246;03m## 2. Variables and Collections[39;00m
[38;5;246;03m####################################################[39;00m

[38;5;246;03m# Python has a print function[39;00m
[38;5;31mprint[39m[38;5;252m([39m[38;5;214m"[39m[38;5;214mI[39m[38;5;214m'[39m[38;5;214mm Python. Nice to meet you![39m[38;5;214m"[39m[38;5;252m)[39m[38;5;252m  [39m[38;5;246;03m# => I'm Python. Nice to meet you![39;00m

[38;5;246;03m# By default the print function also prints out a newline at the end.[39;00m
[38;5;246;03m# Use the optional argument end to change the end string.[39;00m
[38;5;31mprint[39m[38;5;252m([39m[38;5;214m"[39m[38;5;214mHello, World[39m[38;5;214m"[39m[38;5;252m,[39m[38;5;252m [39m[38;5;252mend[39m[38;5;252m=[39m[38;5;214m"[39m[38;5;214m![39m[38;5;214m"[39m[38;5;252m)[39m[38;5;252m  [39m[38;5;246;03m# => Hello, World![39;00m

[38;5;246;03m# Simple way to get input data from console[39;00m
[38;5;252minput_string_var[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;31minput[39m[38;5;252m([39m[38;5;214m"[39m[38;5;214mEnter some data: [39m[38;5;214m"[39m[38;5;252m)[39m[38;5;252m [39m[38;5;246;03m# Returns the data as a string[39;00m

[38;5;246;03m# There are no declarations, only assignments.[39;00m
[38;5;246;03m# Convention is to use lower_case_with_underscores[39;00m
[38;5;252msome_var[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;67m5[39m
[38;5;252msome_var[39m[38;5;252m  [39m[38;5;246;03m# => 5[39;00m

[38;5;246;03m# Accessing a previously unassigned variable is an exception.[39;00m
[38;5;246;03m# See Control Flow to learn more about exception handling.[39;00m
[38;5;252msome_unknown_var[39m[38;5;252m  [39m[38;5;246;03m# Raises a NameError[39;00m

[38;5;246;03m# if can be used as an expression[39;00m
[38;5;246;03m# Equivalent of C's '?:' ternary operator[39;00m
[38;5;214m"[39m[38;5;214myay![39m[38;5;214m"[39m[38;5;252m [39m[38;5;70;01mif[39;00m[38;5;252m [39m[38;5;67m0[39m[38;5;252m [39m[38;5;252m>[39m[38;5;252m [39m[38;5;67m1[39m[38;5;252m [39m[38;5;70;01melse[39;00m[38;5;252m [39m[38;5;214m"[39m[38;5;214mnay![39m[38;5;214m"[39m[38;5;252m  [39m[38;5;246;03m# => "nay!"[39;00m

[38;5;246;03m# Lists store sequences[39;00m
[38;5;252mli[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252m[[39m[38;5;252m][39m
[38;5;246;03m# You can start with a prefilled list[39;00m
[38;5;252mother_li[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252m[[39m[38;5;67m4[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m5[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m6[39m[38;5;252m][39m

[38;5;246;03m# Add stuff to the end of a list with append[39;00m
[38;5;252mli[39m[38;5;252m.[39m[38;5;252mappend[39m[38;5;252m([39m[38;5;67m1[39m[38;5;252m)[39m[38;5;252m    [39m[38;5;246;03m# li is now [1][39;00m
[38;5;252mli[39m[38;5;252m.[39m[38;5;252mappend[39m[38;5;252m([39m[38;5;67m2[39m[38;5;252m)[39m[38;5;252m    [39m[38;5;246;03m# li is now [1, 2][39;00m
[38;5;252mli[39m[38;5;252m.[39m[38;5;252mappend[39m[38;5;252m([39m[38;5;67m4[39m[38;5;252m)[39m[38;5;252m    [39m[38;5;246;03m# li is now [1, 2, 4][39;00m
[38;5;252mli[39m[38;5;252m.[39m[38;5;252mappend[39m[38;5;252m([39m[38;5;67m3[39m[38;5;252m)[39m[38;5;252m    [39m[38;5;246;03m# li is now [1, 2, 4, 3][39;00m
[38;5;246;03m# Remove from the end with pop[39;00m
[38;5;252mli[39m[38;5;252m.[39m[38;5;252mpop[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m        [39m[38;5;246;03m# => 3 and li is now [1, 2, 4][39;00m
[38;5;246;03m# Let's put it back[39;00m
[38;5;252mli[39m[38;5;252m.[39m[38;5;252mappend[39m[38;5;252m([39m[38;5;67m3[39m[38;5;252m)[39m[38;5;252m    [39m[38;5;246;03m# li is now [1, 2, 4, 3] again.[39;00m

[38;5;246;03m# Access a list like you would any array[39;00m
[38;5;252mli[39m[38;5;252m[[39m[38;5;67m0[39m[38;5;252m][39m[38;5;252m   [39m[38;5;246;03m# => 1[39;00m
[38;5;246;03m# Look at the last element[39;00m
[38;5;252mli[39m[38;5;252m[[39m[38;5;252m-[39m[38;5;67m1[39m[38;5;252m][39m[38;5;252m  [39m[38;5;246;03m# => 3[39;00m

[38;5;246;03m# Looking out of bounds is an IndexError[39;00m
[38;5;252mli[39m[38;5;252m[[39m[38;5;67m4[39m[38;5;252m][39m[38;5;252m  [39m[38;5;246;03m# Raises an IndexError[39;00m

[38;5;246;03m# You can look at ranges with slice syntax.[39;00m
[38;5;246;03m# The start index is included, the end index is not[39;00m
[38;5;246;03m# (It's a closed/open range for you mathy types.)[39;00m
[38;5;252mli[39m[38;5;252m[[39m[38;5;67m1[39m[38;5;252m:[39m[38;5;67m3[39m[38;5;252m][39m[38;5;252m   [39m[38;5;246;03m# Return list from index 1 to 3 => [2, 4][39;00m
[38;5;252mli[39m[38;5;252m[[39m[38;5;67m2[39m[38;5;252m:[39m[38;5;252m][39m[38;5;252m    [39m[38;5;246;03m# Return list starting from index 2 => [4, 3][39;00m
[38;5;252mli[39m[38;5;252m[[39m[38;5;252m:[39m[38;5;67m3[39m[38;5;252m][39m[38;5;252m    [39m[38;5;246;03m# Return list from beginning until index 3  => [1, 2, 4][39;00m
[38;5;252mli[39m[38;5;252m[[39m[38;5;252m:[39m[38;5;252m:[39m[38;5;67m2[39m[38;5;252m][39m[38;5;252m   [39m[38;5;246;03m# Return list selecting every second entry => [1, 4][39;00m
[38;5;252mli[39m[38;5;252m[[39m[38;5;252m:[39m[38;5;252m:[39m[38;5;252m-[39m[38;5;67m1[39m[38;5;252m][39m[38;5;252m  [39m[38;5;246;03m# Return list in reverse order => [3, 4, 2, 1][39;00m
[38;5;246;03m# Use any combination of these to make advanced slices[39;00m
[38;5;246;03m# li[start:end:step][39;00m

[38;5;246;03m# Make a one layer deep copy using slices[39;00m
[38;5;252mli2[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252mli[39m[38;5;252m[[39m[38;5;252m:[39m[38;5;252m][39m[38;5;252m  [39m[38;5;246;03m# => li2 = [1, 2, 4, 3] but (li2 is li) will result in false.[39;00m

[38;5;246;03m# Remove arbitrary elements from a list with "del"[39;00m
[38;5;70;01mdel[39;00m[38;5;252m [39m[38;5;252mli[39m[38;5;252m[[39m[38;5;67m2[39m[38;5;252m][39m[38;5;252m  [39m[38;5;246;03m# li is now [1, 2, 3][39;00m

[38;5;246;03m# Remove first occurrence of a value[39;00m
[38;5;252mli[39m[38;5;252m.[39m[38;5;252mremove[39m[38;5;252m([39m[38;5;67m2[39m[38;5;252m)[39m[38;5;252m  [39m[38;5;246;03m# li is now [1, 3][39;00m
[38;5;252mli[39m[38;5;252m.[39m[38;5;252mremove[39m[38;5;252m([39m[38;5;67m2[39m[38;5;252m)[39m[38;5;252m  [39m[38;5;246;03m# Raises a ValueError as 2 is not in the list[39;00m

[38;5;246;03m# Insert an element at a specific index[39;00m
[38;5;252mli[39m[38;5;252m.[39m[38;5;252minsert[39m[38;5;252m([39m[38;5;67m1[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m2[39m[38;5;252m)[39m[38;5;252m  [39m[38;5;246;03m# li is now [1, 2, 3] again[39;00m

[38;5;246;03m# Get the index of the first item found matching the argument[39;00m
[38;5;252mli[39m[38;5;252m.[39m[38;5;252mindex[39m[38;5;252m([39m[38;5;67m2[39m[38;5;252m)[39m[38;5;252m  [39m[38;5;246;03m# => 1[39;00m
[38;5;252mli[39m[38;5;252m.[39m[38;5;252mindex[39m[38;5;252m([39m[38;5;67m4[39m[38;5;252m)[39m[38;5;252m  [39m[38;5;246;03m# Raises a ValueError as 4 is not in the list[39;00m

[38;5;246;03m# You can add lists[39;00m
[38;5;246;03m# Note: values for li and for other_li are not modified.[39;00m
[38;5;252mli[39m[38;5;252m [39m[38;5;252m+[39m[38;5;252m [39m[38;5;252mother_li[39m[38;5;252m  [39m[38;5;246;03m# => [1, 2, 3, 4, 5, 6][39;00m

[38;5;246;03m# Concatenate lists with "extend()"[39;00m
[38;5;252mli[39m[38;5;252m.[39m[38;5;252mextend[39m[38;5;252m([39m[38;5;252mother_li[39m[38;5;252m)[39m[38;5;252m  [39m[38;5;246;03m# Now li is [1, 2, 3, 4, 5, 6][39;00m

[38;5;246;03m# Check for existence in a list with "in"[39;00m
[38;5;67m1[39m[38;5;252m [39m[38;5;70;01min[39;00m[38;5;252m [39m[38;5;252mli[39m[38;5;252m  [39m[38;5;246;03m# => True[39;00m

[38;5;246;03m# Examine the length with "len()"[39;00m
[38;5;31mlen[39m[38;5;252m([39m[38;5;252mli[39m[38;5;252m)[39m[38;5;252m  [39m[38;5;246;03m# => 6[39;00m


[38;5;246;03m# Tuples are like lists but are immutable.[39;00m
[38;5;252mtup[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252m([39m[38;5;67m1[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m2[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m3[39m[38;5;252m)[39m
[38;5;252mtup[39m[38;5;252m[[39m[38;5;67m0[39m[38;5;252m][39m[38;5;252m      [39m[38;5;246;03m# => 1[39;00m
[38;5;252mtup[39m[38;5;252m[[39m[38;5;67m0[39m[38;5;252m][39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;67m3[39m[38;5;252m  [39m[38;5;246;03m# Raises a TypeError[39;00m

[38;5;246;03m# Note that a tuple of length one has to have a comma after the last element but[39;00m
[38;5;246;03m# tuples of other lengths, even zero, do not.[39;00m
[38;5;31mtype[39m[38;5;252m([39m[38;5;252m([39m[38;5;67m1[39m[38;5;252m)[39m[38;5;252m)[39m[38;5;252m   [39m[38;5;246;03m# => <class 'int'>[39;00m
[38;5;31mtype[39m[38;5;252m([39m[38;5;252m([39m[38;5;67m1[39m[38;5;252m,[39m[38;5;252m)[39m[38;5;252m)[39m[38;5;252m  [39m[38;5;246;03m# => <class 'tuple'>[39;00m
[38;5;31mtype[39m[38;5;252m([39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m)[39m[38;5;252m    [39m[38;5;246;03m# => <class 'tuple'>[39;00m

[38;5;246;03m# You can do most of the list operations on tuples too[39;00m
[38;5;31mlen[39m[38;5;252m([39m[38;5;252mtup[39m[38;5;252m)[39m[38;5;252m         [39m[38;5;246;03m# => 3[39;00m
[38;5;252mtup[39m[38;5;252m [39m[38;5;252m+[39m[38;5;252m [39m[38;5;252m([39m[38;5;67m4[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m5[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m6[39m[38;5;252m)[39m[38;5;252m  [39m[38;5;246;03m# => (1, 2, 3, 4, 5, 6)[39;00m
[38;5;252mtup[39m[38;5;252m[[39m[38;5;252m:[39m[38;5;67m2[39m[38;5;252m][39m[38;5;252m          [39m[38;5;246;03m# => (1, 2)[39;00m
[38;5;67m2[39m[38;5;252m [39m[38;5;70;01min[39;00m[38;5;252m [39m[38;5;252mtup[39m[38;5;252m         [39m[38;5;246;03m# => True[39;00m

[38;5;246;03m# You can unpack tuples (or lists) into variables[39;00m
[38;5;252ma[39m[38;5;252m,[39m[38;5;252m [39m[38;5;252mb[39m[38;5;252m,[39m[38;5;252m [39m[38;5;252mc[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252m([39m[38;5;67m1[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m2[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m3[39m[38;5;252m)[39m[38;5;252m  [39m[38;5;246;03m# a is now 1, b is now 2 and c is now 3[39;00m
[38;5;246;03m# You can also do extended unpacking[39;00m
[38;5;252ma[39m[38;5;252m,[39m[38;5;252m [39m[38;5;252m*[39m[38;5;252mb[39m[38;5;252m,[39m[38;5;252m [39m[38;5;252mc[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252m([39m[38;5;67m1[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m2[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m3[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m4[39m[38;5;252m)[39m[38;5;252m  [39m[38;5;246;03m# a is now 1, b is now [2, 3] and c is now 4[39;00m
[38;5;246;03m# Tuples are created by default if you leave out the parentheses[39;00m
[38;5;252md[39m[38;5;252m,[39m[38;5;252m [39m[38;5;252me[39m[38;5;252m,[39m[38;5;252m [39m[38;5;252mf[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;67m4[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m5[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m6[39m[38;5;252m  [39m[38;5;246;03m# tuple 4, 5, 6 is unpacked into variables d, e and f[39;00m
[38;5;246;03m# respectively such that d = 4, e = 5 and f = 6[39;00m
[38;5;246;03m# Now look how easy it is to swap two values[39;00m
[38;5;252me[39m[38;5;252m,[39m[38;5;252m [39m[38;5;252md[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252md[39m[38;5;252m,[39m[38;5;252m [39m[38;5;252me[39m[38;5;252m  [39m[38;5;246;03m# d is now 5 and e is now 4[39;00m


[38;5;246;03m# Dictionaries store mappings from keys to values[39;00m
[38;5;252mempty_dict[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252m{[39m[38;5;252m}[39m
[38;5;246;03m# Here is a prefilled dictionary[39;00m
[38;5;252mfilled_dict[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252m{[39m[38;5;214m"[39m[38;5;214mone[39m[38;5;214m"[39m[38;5;252m:[39m[38;5;252m [39m[38;5;67m1[39m[38;5;252m,[39m[38;5;252m [39m[38;5;214m"[39m[38;5;214mtwo[39m[38;5;214m"[39m[38;5;252m:[39m[38;5;252m [39m[38;5;67m2[39m[38;5;252m,[39m[38;5;252m [39m[38;5;214m"[39m[38;5;214mthree[39m[38;5;214m"[39m[38;5;252m:[39m[38;5;252m [39m[38;5;67m3[39m[38;5;252m}[39m

[38;5;246;03m# Note keys for dictionaries have to be immutable types. This is to ensure that[39;00m
[38;5;246;03m# the key can be converted to a constant hash value for quick look-ups.[39;00m
[38;5;246;03m# Immutable types include ints, floats, strings, tuples.[39;00m
[38;5;252minvalid_dict[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252m{[39m[38;5;252m[[39m[38;5;67m1[39m[38;5;252m,[39m[38;5;67m2[39m[38;5;252m,[39m[38;5;67m3[39m[38;5;252m][39m[38;5;252m:[39m[38;5;252m [39m[38;5;214m"[39m[38;5;214m123[39m[38;5;214m"[39m[38;5;252m}[39m[38;5;252m  [39m[38;5;246;03m# => Raises a TypeError: unhashable type: 'list'[39;00m
[38;5;252mvalid_dict[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252m{[39m[38;5;252m([39m[38;5;67m1[39m[38;5;252m,[39m[38;5;67m2[39m[38;5;252m,[39m[38;5;67m3[39m[38;5;252m)[39m[38;5;252m:[39m[38;5;252m[[39m[38;5;67m1[39m[38;5;252m,[39m[38;5;67m2[39m[38;5;252m,[39m[38;5;67m3[39m[38;5;252m][39m[38;5;252m}[39m[38;5;252m   [39m[38;5;246;03m# Values can be of any type, however.[39;00m

[38;5;246;03m# Look up values with [][39;00m
[38;5;252mfilled_dict[39m[38;5;252m[[39m[38;5;214m"[39m[38;5;214mone[39m[38;5;214m"[39m[38;5;252m][39m[38;5;252m  [39m[38;5;246;03m# => 1[39;00m

[38;5;246;03m# Get all keys as an iterable with "keys()". We need to wrap the call in list()[39;00m
[38;5;246;03m# to turn it into a list. We'll talk about those later.  Note - for Python[39;00m
[38;5;246;03m# versions <3.7, dictionary key ordering is not guaranteed. Your results might[39;00m
[38;5;246;03m# not match the example below exactly. However, as of Python 3.7, dictionary[39;00m
[38;5;246;03m# items maintain the order at which they are inserted into the dictionary.[39;00m
[38;5;31mlist[39m[38;5;252m([39m[38;5;252mfilled_dict[39m[38;5;252m.[39m[38;5;252mkeys[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m)[39m[38;5;252m  [39m[38;5;246;03m# => ["three", "two", "one"] in Python <3.7[39;00m
[38;5;31mlist[39m[38;5;252m([39m[38;5;252mfilled_dict[39m[38;5;252m.[39m[38;5;252mkeys[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m)[39m[38;5;252m  [39m[38;5;246;03m# => ["one", "two", "three"] in Python 3.7+[39;00m


[38;5;246;03m# Get all values as an iterable with "values()". Once again we need to wrap it[39;00m
[38;5;246;03m# in list() to get it out of the iterable. Note - Same as above regarding key[39;00m
[38;5;246;03m# ordering.[39;00m
[38;5;31mlist[39m[38;5;252m([39m[38;5;252mfilled_dict[39m[38;5;252m.[39m[38;5;252mvalues[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m)[39m[38;5;252m  [39m[38;5;246;03m# => [3, 2, 1]  in Python <3.7[39;00m
[38;5;31mlist[39m[38;5;252m([39m[38;5;252mfilled_dict[39m[38;5;252m.[39m[38;5;252mvalues[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m)[39m[38;5;252m  [39m[38;5;246;03m# => [1, 2, 3] in Python 3.7+[39;00m

[38;5;246;03m# Check for existence of keys in a dictionary with "in"[39;00m
[38;5;214m"[39m[38;5;214mone[39m[38;5;214m"[39m[38;5;252m [39m[38;5;70;01min[39;00m[38;5;252m [39m[38;5;252mfilled_dict[39m[38;5;252m  [39m[38;5;246;03m# => True[39;00m
[38;5;67m1[39m[38;5;252m [39m[38;5;70;01min[39;00m[38;5;252m [39m[38;5;252mfilled_dict[39m[38;5;252m      [39m[38;5;246;03m# => False[39;00m

[38;5;246;03m# Looking up a non-existing key is a KeyError[39;00m
[38;5;252mfilled_dict[39m[38;5;252m[[39m[38;5;214m"[39m[38;5;214mfour[39m[38;5;214m"[39m[38;5;252m][39m[38;5;252m  [39m[38;5;246;03m# KeyError[39;00m

[38;5;246;03m# Use "get()" method to avoid the KeyError[39;00m
[38;5;252mfilled_dict[39m[38;5;252m.[39m[38;5;252mget[39m[38;5;252m([39m[38;5;214m"[39m[38;5;214mone[39m[38;5;214m"[39m[38;5;252m)[39m[38;5;252m      [39m[38;5;246;03m# => 1[39;00m
[38;5;252mfilled_dict[39m[38;5;252m.[39m[38;5;252mget[39m[38;5;252m([39m[38;5;214m"[39m[38;5;214mfour[39m[38;5;214m"[39m[38;5;252m)[39m[38;5;252m     [39m[38;5;246;03m# => None[39;00m
[38;5;246;03m# The get method supports a default argument when the value is missing[39;00m
[38;5;252mfilled_dict[39m[38;5;252m.[39m[38;5;252mget[39m[38;5;252m([39m[38;5;214m"[39m[38;5;214mone[39m[38;5;214m"[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m4[39m[38;5;252m)[39m[38;5;252m   [39m[38;5;246;03m# => 1[39;00m
[38;5;252mfilled_dict[39m[38;5;252m.[39m[38;5;252mget[39m[38;5;252m([39m[38;5;214m"[39m[38;5;214mfour[39m[38;5;214m"[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m4[39m[38;5;252m)[39m[38;5;252m  [39m[38;5;246;03m# => 4[39;00m

[38;5;246;03m# "setdefault()" inserts into a dictionary only if the given key isn't present[39;00m
[38;5;252mfilled_dict[39m[38;5;252m.[39m[38;5;252msetdefault[39m[38;5;252m([39m[38;5;214m"[39m[38;5;214mfive[39m[38;5;214m"[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m5[39m[38;5;252m)[39m[38;5;252m  [39m[38;5;246;03m# filled_dict["five"] is set to 5[39;00m
[38;5;252mfilled_dict[39m[38;5;252m.[39m[38;5;252msetdefault[39m[38;5;252m([39m[38;5;214m"[39m[38;5;214mfive[39m[38;5;214m"[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m6[39m[38;5;252m)[39m[38;5;252m  [39m[38;5;246;03m# filled_dict["five"] is still 5[39;00m

[38;5;246;03m# Adding to a dictionary[39;00m
[38;5;252mfilled_dict[39m[38;5;252m.[39m[38;5;252mupdate[39m[38;5;252m([39m[38;5;252m{[39m[38;5;214m"[39m[38;5;214mfour[39m[38;5;214m"[39m[38;5;252m:[39m[38;5;67m4[39m[38;5;252m}[39m[38;5;252m)[39m[38;5;252m  [39m[38;5;246;03m# => {"one": 1, "two": 2, "three": 3, "four": 4}[39;00m
[38;5;252mfilled_dict[39m[38;5;252m[[39m[38;5;214m"[39m[38;5;214mfour[39m[38;5;214m"[39m[38;5;252m][39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;67m4[39m[38;5;252m         [39m[38;5;246;03m# another way to add to dict[39;00m

[38;5;246;03m# Remove keys from a dictionary with del[39;00m
[38;5;70;01mdel[39;00m[38;5;252m [39m[38;5;252mfilled_dict[39m[38;5;252m[[39m[38;5;214m"[39m[38;5;214mone[39m[38;5;214m"[39m[38;5;252m][39m[38;5;252m  [39m[38;5;246;03m# Removes the key "one" from filled dict[39;00m

[38;5;246;03m# From Python 3.5 you can also use the additional unpacking options[39;00m
[38;5;252m{[39m[38;5;214m'[39m[38;5;214ma[39m[38;5;214m'[39m[38;5;252m:[39m[38;5;252m [39m[38;5;67m1[39m[38;5;252m,[39m[38;5;252m [39m[38;5;252m*[39m[38;5;252m*[39m[38;5;252m{[39m[38;5;214m'[39m[38;5;214mb[39m[38;5;214m'[39m[38;5;252m:[39m[38;5;252m [39m[38;5;67m2[39m[38;5;252m}[39m[38;5;252m}[39m[38;5;252m  [39m[38;5;246;03m# => {'a': 1, 'b': 2}[39;00m
[38;5;252m{[39m[38;5;214m'[39m[38;5;214ma[39m[38;5;214m'[39m[38;5;252m:[39m[38;5;252m [39m[38;5;67m1[39m[38;5;252m,[39m[38;5;252m [39m[38;5;252m*[39m[38;5;252m*[39m[38;5;252m{[39m[38;5;214m'[39m[38;5;214ma[39m[38;5;214m'[39m[38;5;252m:[39m[38;5;252m [39m[38;5;67m2[39m[38;5;252m}[39m[38;5;252m}[39m[38;5;252m  [39m[38;5;246;03m# => {'a': 2}[39;00m



[38;5;246;03m# Sets store ... well sets[39;00m
[38;5;252mempty_set[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;31mset[39m[38;5;252m([39m[38;5;252m)[39m
[38;5;246;03m# Initialize a set with a bunch of values. Yeah, it looks a bit like a dict. Sorry.[39;00m
[38;5;252msome_set[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252m{[39m[38;5;67m1[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m1[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m2[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m2[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m3[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m4[39m[38;5;252m}[39m[38;5;252m  [39m[38;5;246;03m# some_set is now {1, 2, 3, 4}[39;00m

[38;5;246;03m# Similar to keys of a dictionary, elements of a set have to be immutable.[39;00m
[38;5;252minvalid_set[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252m{[39m[38;5;252m[[39m[38;5;67m1[39m[38;5;252m][39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m1[39m[38;5;252m}[39m[38;5;252m  [39m[38;5;246;03m# => Raises a TypeError: unhashable type: 'list'[39;00m
[38;5;252mvalid_set[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252m{[39m[38;5;252m([39m[38;5;67m1[39m[38;5;252m,[39m[38;5;252m)[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m1[39m[38;5;252m}[39m

[38;5;246;03m# Add one more item to the set[39;00m
[38;5;252mfilled_set[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252msome_set[39m
[38;5;252mfilled_set[39m[38;5;252m.[39m[38;5;252madd[39m[38;5;252m([39m[38;5;67m5[39m[38;5;252m)[39m[38;5;252m  [39m[38;5;246;03m# filled_set is now {1, 2, 3, 4, 5}[39;00m
[38;5;246;03m# Sets do not have duplicate elements[39;00m
[38;5;252mfilled_set[39m[38;5;252m.[39m[38;5;252madd[39m[38;5;252m([39m[38;5;67m5[39m[38;5;252m)[39m[38;5;252m  [39m[38;5;246;03m# it remains as before {1, 2, 3, 4, 5}[39;00m

[38;5;246;03m# Do set intersection with &[39;00m
[38;5;252mother_set[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252m{[39m[38;5;67m3[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m4[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m5[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m6[39m[38;5;252m}[39m
[38;5;252mfilled_set[39m[38;5;252m [39m[38;5;252m&[39m[38;5;252m [39m[38;5;252mother_set[39m[38;5;252m  [39m[38;5;246;03m# => {3, 4, 5}[39;00m

[38;5;246;03m# Do set union with |[39;00m
[38;5;252mfilled_set[39m[38;5;252m [39m[38;5;252m|[39m[38;5;252m [39m[38;5;252mother_set[39m[38;5;252m  [39m[38;5;246;03m# => {1, 2, 3, 4, 5, 6}[39;00m

[38;5;246;03m# Do set difference with -[39;00m
[38;5;252m{[39m[38;5;67m1[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m2[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m3[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m4[39m[38;5;252m}[39m[38;5;252m [39m[38;5;252m-[39m[38;5;252m [39m[38;5;252m{[39m[38;5;67m2[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m3[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m5[39m[38;5;252m}[39m[38;5;252m  [39m[38;5;246;03m# => {1, 4}[39;00m

[38;5;246;03m# Do set symmetric difference with ^[39;00m
[38;5;252m{[39m[38;5;67m1[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m2[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m3[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m4[39m[38;5;252m}[39m[38;5;252m [39m[38;5;252m^[39m[38;5;252m [39m[38;5;252m{[39m[38;5;67m2[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m3[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m5[39m[38;5;252m}[39m[38;5;252m  [39m[38;5;246;03m# => {1, 4, 5}[39;00m

[38;5;246;03m# Check if set on the left is a superset of set on the right[39;00m
[38;5;252m{[39m[38;5;67m1[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m2[39m[38;5;252m}[39m[38;5;252m [39m[38;5;252m>[39m[38;5;252m=[39m[38;5;252m [39m[38;5;252m{[39m[38;5;67m1[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m2[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m3[39m[38;5;252m}[39m[38;5;252m [39m[38;5;246;03m# => False[39;00m

[38;5;246;03m# Check if set on the left is a subset of set on the right[39;00m
[38;5;252m{[39m[38;5;67m1[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m2[39m[38;5;252m}[39m[38;5;252m [39m[38;5;252m<[39m[38;5;252m=[39m[38;5;252m [39m[38;5;252m{[39m[38;5;67m1[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m2[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m3[39m[38;5;252m}[39m[38;5;252m [39m[38;5;246;03m# => True[39;00m

[38;5;246;03m# Check for existence in a set with in[39;00m
[38;5;67m2[39m[38;5;252m [39m[38;5;70;01min[39;00m[38;5;252m [39m[38;5;252mfilled_set[39m[38;5;252m   [39m[38;5;246;03m# => True[39;00m
[38;5;67m10[39m[38;5;252m [39m[38;5;70;01min[39;00m[38;5;252m [39m[38;5;252mfilled_set[39m[38;5;252m  [39m[38;5;246;03m# => False[39;00m

[38;5;246;03m# Make a one layer deep copy[39;00m
[38;5;252mfilled_set[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252msome_set[39m[38;5;252m.[39m[38;5;252mcopy[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m  [39m[38;5;246;03m# filled_set is {1, 2, 3, 4, 5}[39;00m
[38;5;252mfilled_set[39m[38;5;252m [39m[38;5;70;01mis[39;00m[38;5;252m [39m[38;5;252msome_set[39m[38;5;252m        [39m[38;5;246;03m# => False[39;00m


[38;5;246;03m####################################################[39;00m
[38;5;246;03m## 3. Control Flow and Iterables[39;00m
[38;5;246;03m####################################################[39;00m

[38;5;246;03m# Let's just make a variable[39;00m
[38;5;252msome_var[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;67m5[39m

[38;5;246;03m# Here is an if statement. Indentation is significant in Python![39;00m
[38;5;246;03m# Convention is to use four spaces, not tabs.[39;00m
[38;5;246;03m# This prints "some_var is smaller than 10"[39;00m
[38;5;70;01mif[39;00m[38;5;252m [39m[38;5;252msome_var[39m[38;5;252m [39m[38;5;252m>[39m[38;5;252m [39m[38;5;67m10[39m[38;5;252m:[39m
[38;5;252m    [39m[38;5;31mprint[39m[38;5;252m([39m[38;5;214m"[39m[38;5;214msome_var is totally bigger than 10.[39m[38;5;214m"[39m[38;5;252m)[39m
[38;5;70;01melif[39;00m[38;5;252m [39m[38;5;252msome_var[39m[38;5;252m [39m[38;5;252m<[39m[38;5;252m [39m[38;5;67m10[39m[38;5;252m:[39m[38;5;252m    [39m[38;5;246;03m# This elif clause is optional.[39;00m
[38;5;252m    [39m[38;5;31mprint[39m[38;5;252m([39m[38;5;214m"[39m[38;5;214msome_var is smaller than 10.[39m[38;5;214m"[39m[38;5;252m)[39m
[38;5;70;01melse[39;00m[38;5;252m:[39m[38;5;252m                  [39m[38;5;246;03m# This is optional too.[39;00m
[38;5;252m    [39m[38;5;31mprint[39m[38;5;252m([39m[38;5;214m"[39m[38;5;214msome_var is indeed 10.[39m[38;5;214m"[39m[38;5;252m)[39m


[38;5;214m"""[39m
[38;5;214mFor loops iterate over lists[39m
[38;5;214mprints:[39m
[38;5;214m    dog is a mammal[39m
[38;5;214m    cat is a mammal[39m
[38;5;214m    mouse is a mammal[39m
[38;5;214m"""[39m
[38;5;70;01mfor[39;00m[38;5;252m [39m[38;5;252manimal[39m[38;5;252m [39m[38;5;70;01min[39;00m[38;5;252m [39m[38;5;252m[[39m[38;5;214m"[39m[38;5;214mdog[39m[38;5;214m"[39m[38;5;252m,[39m[38;5;252m [39m[38;5;214m"[39m[38;5;214mcat[39m[38;5;214m"[39m[38;5;252m,[39m[38;5;252m [39m[38;5;214m"[39m[38;5;214mmouse[39m[38;5;214m"[39m[38;5;252m][39m[38;5;252m:[39m
[38;5;252m    [39m[38;5;246;03m# You can use format() to interpolate formatted strings[39;00m
[38;5;252m    [39m[38;5;31mprint[39m[38;5;252m([39m[38;5;214m"[39m[38;5;214m{}[39m[38;5;214m is a mammal[39m[38;5;214m"[39m[38;5;252m.[39m[38;5;252mformat[39m[38;5;252m([39m[38;5;252manimal[39m[38;5;252m)[39m[38;5;252m)[39m

[38;5;214m"""[39m
[38;5;214m"range(number)" returns an iterable of numbers[39m
[38;5;214mfrom zero to the given number[39m
[38;5;214mprints:[39m
[38;5;214m    0[39m
[38;5;214m    1[39m
[38;5;214m    2[39m
[38;5;214m    3[39m
[38;5;214m"""[39m
[38;5;70;01mfor[39;00m[38;5;252m [39m[38;5;252mi[39m[38;5;252m [39m[38;5;70;01min[39;00m[38;5;252m [39m[38;5;31mrange[39m[38;5;252m([39m[38;5;67m4[39m[38;5;252m)[39m[38;5;252m:[39m
[38;5;252m    [39m[38;5;31mprint[39m[38;5;252m([39m[38;5;252mi[39m[38;5;252m)[39m

[38;5;214m"""[39m
[38;5;214m"range(lower, upper)" returns an iterable of numbers[39m
[38;5;214mfrom the lower number to the upper number[39m
[38;5;214mprints:[39m
[38;5;214m    4[39m
[38;5;214m    5[39m
[38;5;214m    6[39m
[38;5;214m    7[39m
[38;5;214m"""[39m
[38;5;70;01mfor[39;00m[38;5;252m [39m[38;5;252mi[39m[38;5;252m [39m[38;5;70;01min[39;00m[38;5;252m [39m[38;5;31mrange[39m[38;5;252m([39m[38;5;67m4[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m8[39m[38;5;252m)[39m[38;5;252m:[39m
[38;5;252m    [39m[38;5;31mprint[39m[38;5;252m([39m[38;5;252mi[39m[38;5;252m)[39m

[38;5;214m"""[39m
[38;5;214m"range(lower, upper, step)" returns an iterable of numbers[39m
[38;5;214mfrom the lower number to the upper number, while incrementing[39m
[38;5;214mby step. If step is not indicated, the default value is 1.[39m
[38;5;214mprints:[39m
[38;5;214m    4[39m
[38;5;214m    6[39m
[38;5;214m"""[39m
[38;5;70;01mfor[39;00m[38;5;252m [39m[38;5;252mi[39m[38;5;252m [39m[38;5;70;01min[39;00m[38;5;252m [39m[38;5;31mrange[39m[38;5;252m([39m[38;5;67m4[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m8[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m2[39m[38;5;252m)[39m[38;5;252m:[39m
[38;5;252m    [39m[38;5;31mprint[39m[38;5;252m([39m[38;5;252mi[39m[38;5;252m)[39m

[38;5;214m"""[39m
[38;5;214mTo loop over a list, and retrieve both the index and the value of each item in the list[39m
[38;5;214mprints:[39m
[38;5;214m    0 dog[39m
[38;5;214m    1 cat[39m
[38;5;214m    2 mouse[39m
[38;5;214m"""[39m
[38;5;252manimals[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252m[[39m[38;5;214m"[39m[38;5;214mdog[39m[38;5;214m"[39m[38;5;252m,[39m[38;5;252m [39m[38;5;214m"[39m[38;5;214mcat[39m[38;5;214m"[39m[38;5;252m,[39m[38;5;252m [39m[38;5;214m"[39m[38;5;214mmouse[39m[38;5;214m"[39m[38;5;252m][39m
[38;5;70;01mfor[39;00m[38;5;252m [39m[38;5;252mi[39m[38;5;252m,[39m[38;5;252m [39m[38;5;252mvalue[39m[38;5;252m [39m[38;5;70;01min[39;00m[38;5;252m [39m[38;5;31menumerate[39m[38;5;252m([39m[38;5;252manimals[39m[38;5;252m)[39m[38;5;252m:[39m
[38;5;252m    [39m[38;5;31mprint[39m[38;5;252m([39m[38;5;252mi[39m[38;5;252m,[39m[38;5;252m [39m[38;5;252mvalue[39m[38;5;252m)[39m

[38;5;214m"""[39m
[38;5;214mWhile loops go until a condition is no longer met.[39m
[38;5;214mprints:[39m
[38;5;214m    0[39m
[38;5;214m    1[39m
[38;5;214m    2[39m
[38;5;214m    3[39m
[38;5;214m"""[39m
[38;5;252mx[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;67m0[39m
[38;5;70;01mwhile[39;00m[38;5;252m [39m[38;5;252mx[39m[38;5;252m [39m[38;5;252m<[39m[38;5;252m [39m[38;5;67m4[39m[38;5;252m:[39m
[38;5;252m    [39m[38;5;31mprint[39m[38;5;252m([39m[38;5;252mx[39m[38;5;252m)[39m
[38;5;252m    [39m[38;5;252mx[39m[38;5;252m [39m[38;5;252m+[39m[38;5;252m=[39m[38;5;252m [39m[38;5;67m1[39m[38;5;252m  [39m[38;5;246;03m# Shorthand for x = x + 1[39;00m

[38;5;246;03m# Handle exceptions with a try/except block[39;00m
[38;5;70;01mtry[39;00m[38;5;252m:[39m
[38;5;252m    [39m[38;5;246;03m# Use "raise" to raise an error[39;00m
[38;5;252m    [39m[38;5;70;01mraise[39;00m[38;5;252m [39m[38;5;250mIndexError[39m[38;5;252m([39m[38;5;214m"[39m[38;5;214mThis is an index error[39m[38;5;214m"[39m[38;5;252m)[39m
[38;5;70;01mexcept[39;00m[38;5;252m [39m[38;5;250mIndexError[39m[38;5;252m [39m[38;5;70;01mas[39;00m[38;5;252m [39m[38;5;252me[39m[38;5;252m:[39m
[38;5;252m    [39m[38;5;70;01mpass[39;00m[38;5;252m                 [39m[38;5;246;03m# Pass is just a no-op. Usually you would do recovery here.[39;00m
[38;5;70;01mexcept[39;00m[38;5;252m [39m[38;5;252m([39m[38;5;250mTypeError[39m[38;5;252m,[39m[38;5;252m [39m[38;5;250mNameError[39m[38;5;252m)[39m[38;5;252m:[39m
[38;5;252m    [39m[38;5;70;01mpass[39;00m[38;5;252m                 [39m[38;5;246;03m# Multiple exceptions can be handled together, if required.[39;00m
[38;5;70;01melse[39;00m[38;5;252m:[39m[38;5;252m                    [39m[38;5;246;03m# Optional clause to the try/except block. Must follow all except blocks[39;00m
[38;5;252m    [39m[38;5;31mprint[39m[38;5;252m([39m[38;5;214m"[39m[38;5;214mAll good![39m[38;5;214m"[39m[38;5;252m)[39m[38;5;252m   [39m[38;5;246;03m# Runs only if the code in try raises no exceptions[39;00m
[38;5;70;01mfinally[39;00m[38;5;252m:[39m[38;5;252m                 [39m[38;5;246;03m# Execute under all circumstances[39;00m
[38;5;252m    [39m[38;5;31mprint[39m[38;5;252m([39m[38;5;214m"[39m[38;5;214mWe can clean up resources here[39m[38;5;214m"[39m[38;5;252m)[39m

[38;5;246;03m# Instead of try/finally to cleanup resources you can use a with statement[39;00m
[38;5;70;01mwith[39;00m[38;5;252m [39m[38;5;31mopen[39m[38;5;252m([39m[38;5;214m"[39m[38;5;214mmyfile.txt[39m[38;5;214m"[39m[38;5;252m)[39m[38;5;252m [39m[38;5;70;01mas[39;00m[38;5;252m [39m[38;5;252mf[39m[38;5;252m:[39m
[38;5;252m    [39m[38;5;70;01mfor[39;00m[38;5;252m [39m[38;5;252mline[39m[38;5;252m [39m[38;5;70;01min[39;00m[38;5;252m [39m[38;5;252mf[39m[38;5;252m:[39m
[38;5;252m        [39m[38;5;31mprint[39m[38;5;252m([39m[38;5;252mline[39m[38;5;252m)[39m

[38;5;246;03m# Writing to a file[39;00m
[38;5;252mcontents[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252m{[39m[38;5;214m"[39m[38;5;214maa[39m[38;5;214m"[39m[38;5;252m:[39m[38;5;252m [39m[38;5;67m12[39m[38;5;252m,[39m[38;5;252m [39m[38;5;214m"[39m[38;5;214mbb[39m[38;5;214m"[39m[38;5;252m:[39m[38;5;252m [39m[38;5;67m21[39m[38;5;252m}[39m
[38;5;70;01mwith[39;00m[38;5;252m [39m[38;5;31mopen[39m[38;5;252m([39m[38;5;214m"[39m[38;5;214mmyfile1.txt[39m[38;5;214m"[39m[38;5;252m,[39m[38;5;252m [39m[38;5;214m"[39m[38;5;214mw+[39m[38;5;214m"[39m[38;5;252m)[39m[38;5;252m [39m[38;5;70;01mas[39;00m[38;5;252m [39m[38;5;252mfile[39m[38;5;252m:[39m
[38;5;252m    [39m[38;5;252mfile[39m[38;5;252m.[39m[38;5;252mwrite[39m[38;5;252m([39m[38;5;31mstr[39m[38;5;252m([39m[38;5;252mcontents[39m[38;5;252m)[39m[38;5;252m)[39m[38;5;252m        [39m[38;5;246;03m# writes a string to a file[39;00m

[38;5;70;01mwith[39;00m[38;5;252m [39m[38;5;31mopen[39m[38;5;252m([39m[38;5;214m"[39m[38;5;214mmyfile2.txt[39m[38;5;214m"[39m[38;5;252m,[39m[38;5;252m [39m[38;5;214m"[39m[38;5;214mw+[39m[38;5;214m"[39m[38;5;252m)[39m[38;5;252m [39m[38;5;70;01mas[39;00m[38;5;252m [39m[38;5;252mfile[39m[38;5;252m:[39m
[38;5;252m    [39m[38;5;252mfile[39m[38;5;252m.[39m[38;5;252mwrite[39m[38;5;252m([39m[38;5;252mjson[39m[38;5;252m.[39m[38;5;252mdumps[39m[38;5;252m([39m[38;5;252mcontents[39m[38;5;252m)[39m[38;5;252m)[39m[38;5;252m [39m[38;5;246;03m# writes an object to a file[39;00m

[38;5;246;03m# Reading from a file[39;00m
[38;5;70;01mwith[39;00m[38;5;252m [39m[38;5;31mopen[39m[38;5;252m([39m[38;5;214m'[39m[38;5;214mmyfile1.txt[39m[38;5;214m'[39m[38;5;252m,[39m[38;5;252m [39m[38;5;214m"[39m[38;5;214mr+[39m[38;5;214m"[39m[38;5;252m)[39m[38;5;252m [39m[38;5;70;01mas[39;00m[38;5;252m [39m[38;5;252mfile[39m[38;5;252m:[39m
[38;5;252m    [39m[38;5;252mcontents[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252mfile[39m[38;5;252m.[39m[38;5;252mread[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m           [39m[38;5;246;03m# reads a string from a file[39;00m
[38;5;31mprint[39m[38;5;252m([39m[38;5;252mcontents[39m[38;5;252m)[39m
[38;5;246;03m# print: {"aa": 12, "bb": 21}[39;00m

[38;5;70;01mwith[39;00m[38;5;252m [39m[38;5;31mopen[39m[38;5;252m([39m[38;5;214m'[39m[38;5;214mmyfile2.txt[39m[38;5;214m'[39m[38;5;252m,[39m[38;5;252m [39m[38;5;214m"[39m[38;5;214mr+[39m[38;5;214m"[39m[38;5;252m)[39m[38;5;252m [39m[38;5;70;01mas[39;00m[38;5;252m [39m[38;5;252mfile[39m[38;5;252m:[39m
[38;5;252m    [39m[38;5;252mcontents[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252mjson[39m[38;5;252m.[39m[38;5;252mload[39m[38;5;252m([39m[38;5;252mfile[39m[38;5;252m)[39m[38;5;252m       [39m[38;5;246;03m# reads a json object from a file[39;00m
[38;5;31mprint[39m[38;5;252m([39m[38;5;252mcontents[39m[38;5;252m)[39m
[38;5;246;03m# print: {"aa": 12, "bb": 21}[39;00m


[38;5;246;03m# Python offers a fundamental abstraction called the Iterable.[39;00m
[38;5;246;03m# An iterable is an object that can be treated as a sequence.[39;00m
[38;5;246;03m# The object returned by the range function, is an iterable.[39;00m

[38;5;252mfilled_dict[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252m{[39m[38;5;214m"[39m[38;5;214mone[39m[38;5;214m"[39m[38;5;252m:[39m[38;5;252m [39m[38;5;67m1[39m[38;5;252m,[39m[38;5;252m [39m[38;5;214m"[39m[38;5;214mtwo[39m[38;5;214m"[39m[38;5;252m:[39m[38;5;252m [39m[38;5;67m2[39m[38;5;252m,[39m[38;5;252m [39m[38;5;214m"[39m[38;5;214mthree[39m[38;5;214m"[39m[38;5;252m:[39m[38;5;252m [39m[38;5;67m3[39m[38;5;252m}[39m
[38;5;252mour_iterable[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252mfilled_dict[39m[38;5;252m.[39m[38;5;252mkeys[39m[38;5;252m([39m[38;5;252m)[39m
[38;5;31mprint[39m[38;5;252m([39m[38;5;252mour_iterable[39m[38;5;252m)[39m[38;5;252m  [39m[38;5;246;03m# => dict_keys(['one', 'two', 'three']). This is an object that implements our Iterable interface.[39;00m

[38;5;246;03m# We can loop over it.[39;00m
[38;5;70;01mfor[39;00m[38;5;252m [39m[38;5;252mi[39m[38;5;252m [39m[38;5;70;01min[39;00m[38;5;252m [39m[38;5;252mour_iterable[39m[38;5;252m:[39m
[38;5;252m    [39m[38;5;31mprint[39m[38;5;252m([39m[38;5;252mi[39m[38;5;252m)[39m[38;5;252m  [39m[38;5;246;03m# Prints one, two, three[39;00m

[38;5;246;03m# However we cannot address elements by index.[39;00m
[38;5;252mour_iterable[39m[38;5;252m[[39m[38;5;67m1[39m[38;5;252m][39m[38;5;252m  [39m[38;5;246;03m# Raises a TypeError[39;00m

[38;5;246;03m# An iterable is an object that knows how to create an iterator.[39;00m
[38;5;252mour_iterator[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;31miter[39m[38;5;252m([39m[38;5;252mour_iterable[39m[38;5;252m)[39m

[38;5;246;03m# Our iterator is an object that can remember the state as we traverse through it.[39;00m
[38;5;246;03m# We get the next object with "next()".[39;00m
[38;5;31mnext[39m[38;5;252m([39m[38;5;252mour_iterator[39m[38;5;252m)[39m[38;5;252m  [39m[38;5;246;03m# => "one"[39;00m

[38;5;246;03m# It maintains state as we iterate.[39;00m
[38;5;31mnext[39m[38;5;252m([39m[38;5;252mour_iterator[39m[38;5;252m)[39m[38;5;252m  [39m[38;5;246;03m# => "two"[39;00m
[38;5;31mnext[39m[38;5;252m([39m[38;5;252mour_iterator[39m[38;5;252m)[39m[38;5;252m  [39m[38;5;246;03m# => "three"[39;00m

[38;5;246;03m# After the iterator has returned all of its data, it raises a StopIteration exception[39;00m
[38;5;31mnext[39m[38;5;252m([39m[38;5;252mour_iterator[39m[38;5;252m)[39m[38;5;252m  [39m[38;5;246;03m# Raises StopIteration[39;00m

[38;5;246;03m# We can also loop over it, in fact, "for" does this implicitly![39;00m
[38;5;252mour_iterator[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;31miter[39m[38;5;252m([39m[38;5;252mour_iterable[39m[38;5;252m)[39m
[38;5;70;01mfor[39;00m[38;5;252m [39m[38;5;252mi[39m[38;5;252m [39m[38;5;70;01min[39;00m[38;5;252m [39m[38;5;252mour_iterator[39m[38;5;252m:[39m
[38;5;252m    [39m[38;5;31mprint[39m[38;5;252m([39m[38;5;252mi[39m[38;5;252m)[39m[38;5;252m  [39m[38;5;246;03m# Prints one, two, three[39;00m

[38;5;246;03m# You can grab all the elements of an iterable or iterator by calling list() on it.[39;00m
[38;5;31mlist[39m[38;5;252m([39m[38;5;252mour_iterable[39m[38;5;252m)[39m[38;5;252m  [39m[38;5;246;03m# => Returns ["one", "two", "three"][39;00m
[38;5;31mlist[39m[38;5;252m([39m[38;5;252mour_iterator[39m[38;5;252m)[39m[38;5;252m  [39m[38;5;246;03m# => Returns [] because state is saved[39;00m


[38;5;246;03m####################################################[39;00m
[38;5;246;03m## 4. Functions[39;00m
[38;5;246;03m####################################################[39;00m

[38;5;246;03m# Use "def" to create new functions[39;00m
[38;5;70;01mdef[39;00m[38;5;252m [39m[38;5;68madd[39m[38;5;252m([39m[38;5;252mx[39m[38;5;252m,[39m[38;5;252m [39m[38;5;252my[39m[38;5;252m)[39m[38;5;252m:[39m
[38;5;252m    [39m[38;5;31mprint[39m[38;5;252m([39m[38;5;214m"[39m[38;5;214mx is [39m[38;5;214m{}[39m[38;5;214m and y is [39m[38;5;214m{}[39m[38;5;214m"[39m[38;5;252m.[39m[38;5;252mformat[39m[38;5;252m([39m[38;5;252mx[39m[38;5;252m,[39m[38;5;252m [39m[38;5;252my[39m[38;5;252m)[39m[38;5;252m)[39m
[38;5;252m    [39m[38;5;70;01mreturn[39;00m[38;5;252m [39m[38;5;252mx[39m[38;5;252m [39m[38;5;252m+[39m[38;5;252m [39m[38;5;252my[39m[38;5;252m  [39m[38;5;246;03m# Return values with a return statement[39;00m

[38;5;246;03m# Calling functions with parameters[39;00m
[38;5;252madd[39m[38;5;252m([39m[38;5;67m5[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m6[39m[38;5;252m)[39m[38;5;252m  [39m[38;5;246;03m# => prints out "x is 5 and y is 6" and returns 11[39;00m

[38;5;246;03m# Another way to call functions is with keyword arguments[39;00m
[38;5;252madd[39m[38;5;252m([39m[38;5;252my[39m[38;5;252m=[39m[38;5;67m6[39m[38;5;252m,[39m[38;5;252m [39m[38;5;252mx[39m[38;5;252m=[39m[38;5;67m5[39m[38;5;252m)[39m[38;5;252m  [39m[38;5;246;03m# Keyword arguments can arrive in any order.[39;00m

[38;5;246;03m# You can define functions that take a variable number of[39;00m
[38;5;246;03m# positional arguments[39;00m
[38;5;70;01mdef[39;00m[38;5;252m [39m[38;5;68mvarargs[39m[38;5;252m([39m[38;5;252m*[39m[38;5;252margs[39m[38;5;252m)[39m[38;5;252m:[39m
[38;5;252m    [39m[38;5;70;01mreturn[39;00m[38;5;252m [39m[38;5;252margs[39m

[38;5;252mvarargs[39m[38;5;252m([39m[38;5;67m1[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m2[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m3[39m[38;5;252m)[39m[38;5;252m  [39m[38;5;246;03m# => (1, 2, 3)[39;00m

[38;5;246;03m# You can define functions that take a variable number of[39;00m
[38;5;246;03m# keyword arguments, as well[39;00m
[38;5;70;01mdef[39;00m[38;5;252m [39m[38;5;68mkeyword_args[39m[38;5;252m([39m[38;5;252m*[39m[38;5;252m*[39m[38;5;252mkwargs[39m[38;5;252m)[39m[38;5;252m:[39m
[38;5;252m    [39m[38;5;70;01mreturn[39;00m[38;5;252m [39m[38;5;252mkwargs[39m

[38;5;246;03m# Let's call it to see what happens[39;00m
[38;5;252mkeyword_args[39m[38;5;252m([39m[38;5;252mbig[39m[38;5;252m=[39m[38;5;214m"[39m[38;5;214mfoot[39m[38;5;214m"[39m[38;5;252m,[39m[38;5;252m [39m[38;5;252mloch[39m[38;5;252m=[39m[38;5;214m"[39m[38;5;214mness[39m[38;5;214m"[39m[38;5;252m)[39m[38;5;252m  [39m[38;5;246;03m# => {"big": "foot", "loch": "ness"}[39;00m


[38;5;246;03m# You can do both at once, if you like[39;00m
[38;5;70;01mdef[39;00m[38;5;252m [39m[38;5;68mall_the_args[39m[38;5;252m([39m[38;5;252m*[39m[38;5;252margs[39m[38;5;252m,[39m[38;5;252m [39m[38;5;252m*[39m[38;5;252m*[39m[38;5;252mkwargs[39m[38;5;252m)[39m[38;5;252m:[39m
[38;5;252m    [39m[38;5;31mprint[39m[38;5;252m([39m[38;5;252margs[39m[38;5;252m)[39m
[38;5;252m    [39m[38;5;31mprint[39m[38;5;252m([39m[38;5;252mkwargs[39m[38;5;252m)[39m
[38;5;214m"""[39m
[38;5;214mall_the_args(1, 2, a=3, b=4) prints:[39m
[38;5;214m    (1, 2)[39m
[38;5;214m    {"a": 3, "b": 4}[39m
[38;5;214m"""[39m

[38;5;246;03m# When calling functions, you can do the opposite of args/kwargs![39;00m
[38;5;246;03m# Use * to expand tuples and use ** to expand kwargs.[39;00m
[38;5;252margs[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252m([39m[38;5;67m1[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m2[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m3[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m4[39m[38;5;252m)[39m
[38;5;252mkwargs[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252m{[39m[38;5;214m"[39m[38;5;214ma[39m[38;5;214m"[39m[38;5;252m:[39m[38;5;252m [39m[38;5;67m3[39m[38;5;252m,[39m[38;5;252m [39m[38;5;214m"[39m[38;5;214mb[39m[38;5;214m"[39m[38;5;252m:[39m[38;5;252m [39m[38;5;67m4[39m[38;5;252m}[39m
[38;5;252mall_the_args[39m[38;5;252m([39m[38;5;252m*[39m[38;5;252margs[39m[38;5;252m)[39m[38;5;252m            [39m[38;5;246;03m# equivalent to all_the_args(1, 2, 3, 4)[39;00m
[38;5;252mall_the_args[39m[38;5;252m([39m[38;5;252m*[39m[38;5;252m*[39m[38;5;252mkwargs[39m[38;5;252m)[39m[38;5;252m         [39m[38;5;246;03m# equivalent to all_the_args(a=3, b=4)[39;00m
[38;5;252mall_the_args[39m[38;5;252m([39m[38;5;252m*[39m[38;5;252margs[39m[38;5;252m,[39m[38;5;252m [39m[38;5;252m*[39m[38;5;252m*[39m[38;5;252mkwargs[39m[38;5;252m)[39m[38;5;252m  [39m[38;5;246;03m# equivalent to all_the_args(1, 2, 3, 4, a=3, b=4)[39;00m

[38;5;246;03m# Returning multiple values (with tuple assignments)[39;00m
[38;5;70;01mdef[39;00m[38;5;252m [39m[38;5;68mswap[39m[38;5;252m([39m[38;5;252mx[39m[38;5;252m,[39m[38;5;252m [39m[38;5;252my[39m[38;5;252m)[39m[38;5;252m:[39m
[38;5;252m    [39m[38;5;70;01mreturn[39;00m[38;5;252m [39m[38;5;252my[39m[38;5;252m,[39m[38;5;252m [39m[38;5;252mx[39m[38;5;252m  [39m[38;5;246;03m# Return multiple values as a tuple without the parenthesis.[39;00m
[38;5;252m                 [39m[38;5;246;03m# (Note: parenthesis have been excluded but can be included)[39;00m

[38;5;252mx[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;67m1[39m
[38;5;252my[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;67m2[39m
[38;5;252mx[39m[38;5;252m,[39m[38;5;252m [39m[38;5;252my[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252mswap[39m[38;5;252m([39m[38;5;252mx[39m[38;5;252m,[39m[38;5;252m [39m[38;5;252my[39m[38;5;252m)[39m[38;5;252m     [39m[38;5;246;03m# => x = 2, y = 1[39;00m
[38;5;246;03m# (x, y) = swap(x,y)  # Again parenthesis have been excluded but can be included.[39;00m

[38;5;246;03m# Function Scope[39;00m
[38;5;252mx[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;67m5[39m

[38;5;70;01mdef[39;00m[38;5;252m [39m[38;5;68mset_x[39m[38;5;252m([39m[38;5;252mnum[39m[38;5;252m)[39m[38;5;252m:[39m
[38;5;252m    [39m[38;5;246;03m# Local var x not the same as global variable x[39;00m
[38;5;252m    [39m[38;5;252mx[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252mnum[39m[38;5;252m    [39m[38;5;246;03m# => 43[39;00m
[38;5;252m    [39m[38;5;31mprint[39m[38;5;252m([39m[38;5;252mx[39m[38;5;252m)[39m[38;5;252m   [39m[38;5;246;03m# => 43[39;00m

[38;5;70;01mdef[39;00m[38;5;252m [39m[38;5;68mset_global_x[39m[38;5;252m([39m[38;5;252mnum[39m[38;5;252m)[39m[38;5;252m:[39m
[38;5;252m    [39m[38;5;70;01mglobal[39;00m[38;5;252m [39m[38;5;252mx[39m
[38;5;252m    [39m[38;5;31mprint[39m[38;5;252m([39m[38;5;252mx[39m[38;5;252m)[39m[38;5;252m   [39m[38;5;246;03m# => 5[39;00m
[38;5;252m    [39m[38;5;252mx[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252mnum[39m[38;5;252m    [39m[38;5;246;03m# global var x is now set to 6[39;00m
[38;5;252m    [39m[38;5;31mprint[39m[38;5;252m([39m[38;5;252mx[39m[38;5;252m)[39m[38;5;252m   [39m[38;5;246;03m# => 6[39;00m

[38;5;252mset_x[39m[38;5;252m([39m[38;5;67m43[39m[38;5;252m)[39m
[38;5;252mset_global_x[39m[38;5;252m([39m[38;5;67m6[39m[38;5;252m)[39m


[38;5;246;03m# Python has first class functions[39;00m
[38;5;70;01mdef[39;00m[38;5;252m [39m[38;5;68mcreate_adder[39m[38;5;252m([39m[38;5;252mx[39m[38;5;252m)[39m[38;5;252m:[39m
[38;5;252m    [39m[38;5;70;01mdef[39;00m[38;5;252m [39m[38;5;68madder[39m[38;5;252m([39m[38;5;252my[39m[38;5;252m)[39m[38;5;252m:[39m
[38;5;252m        [39m[38;5;70;01mreturn[39;00m[38;5;252m [39m[38;5;252mx[39m[38;5;252m [39m[38;5;252m+[39m[38;5;252m [39m[38;5;252my[39m
[38;5;252m    [39m[38;5;70;01mreturn[39;00m[38;5;252m [39m[38;5;252madder[39m

[38;5;252madd_10[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252mcreate_adder[39m[38;5;252m([39m[38;5;67m10[39m[38;5;252m)[39m
[38;5;252madd_10[39m[38;5;252m([39m[38;5;67m3[39m[38;5;252m)[39m[38;5;252m   [39m[38;5;246;03m# => 13[39;00m

[38;5;246;03m# There are also anonymous functions[39;00m
[38;5;252m([39m[38;5;70;01mlambda[39;00m[38;5;252m [39m[38;5;252mx[39m[38;5;252m:[39m[38;5;252m [39m[38;5;252mx[39m[38;5;252m [39m[38;5;252m>[39m[38;5;252m [39m[38;5;67m2[39m[38;5;252m)[39m[38;5;252m([39m[38;5;67m3[39m[38;5;252m)[39m[38;5;252m                  [39m[38;5;246;03m# => True[39;00m
[38;5;252m([39m[38;5;70;01mlambda[39;00m[38;5;252m [39m[38;5;252mx[39m[38;5;252m,[39m[38;5;252m [39m[38;5;252my[39m[38;5;252m:[39m[38;5;252m [39m[38;5;252mx[39m[38;5;252m [39m[38;5;252m*[39m[38;5;252m*[39m[38;5;252m [39m[38;5;67m2[39m[38;5;252m [39m[38;5;252m+[39m[38;5;252m [39m[38;5;252my[39m[38;5;252m [39m[38;5;252m*[39m[38;5;252m*[39m[38;5;252m [39m[38;5;67m2[39m[38;5;252m)[39m[38;5;252m([39m[38;5;67m2[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m1[39m[38;5;252m)[39m[38;5;252m  [39m[38;5;246;03m# => 5[39;00m

[38;5;246;03m# There are built-in higher order functions[39;00m
[38;5;31mlist[39m[38;5;252m([39m[38;5;31mmap[39m[38;5;252m([39m[38;5;252madd_10[39m[38;5;252m,[39m[38;5;252m [39m[38;5;252m[[39m[38;5;67m1[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m2[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m3[39m[38;5;252m][39m[38;5;252m)[39m[38;5;252m)[39m[38;5;252m          [39m[38;5;246;03m# => [11, 12, 13][39;00m
[38;5;31mlist[39m[38;5;252m([39m[38;5;31mmap[39m[38;5;252m([39m[38;5;31mmax[39m[38;5;252m,[39m[38;5;252m [39m[38;5;252m[[39m[38;5;67m1[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m2[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m3[39m[38;5;252m][39m[38;5;252m,[39m[38;5;252m [39m[38;5;252m[[39m[38;5;67m4[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m2[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m1[39m[38;5;252m][39m[38;5;252m)[39m[38;5;252m)[39m[38;5;252m  [39m[38;5;246;03m# => [4, 2, 3][39;00m

[38;5;31mlist[39m[38;5;252m([39m[38;5;31mfilter[39m[38;5;252m([39m[38;5;70;01mlambda[39;00m[38;5;252m [39m[38;5;252mx[39m[38;5;252m:[39m[38;5;252m [39m[38;5;252mx[39m[38;5;252m [39m[38;5;252m>[39m[38;5;252m [39m[38;5;67m5[39m[38;5;252m,[39m[38;5;252m [39m[38;5;252m[[39m[38;5;67m3[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m4[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m5[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m6[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m7[39m[38;5;252m][39m[38;5;252m)[39m[38;5;252m)[39m[38;5;252m  [39m[38;5;246;03m# => [6, 7][39;00m

[38;5;246;03m# We can use list comprehensions for nice maps and filters[39;00m
[38;5;246;03m# List comprehension stores the output as a list which can itself be a nested list[39;00m
[38;5;252m[[39m[38;5;252madd_10[39m[38;5;252m([39m[38;5;252mi[39m[38;5;252m)[39m[38;5;252m [39m[38;5;70;01mfor[39;00m[38;5;252m [39m[38;5;252mi[39m[38;5;252m [39m[38;5;70;01min[39;00m[38;5;252m [39m[38;5;252m[[39m[38;5;67m1[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m2[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m3[39m[38;5;252m][39m[38;5;252m][39m[38;5;252m         [39m[38;5;246;03m# => [11, 12, 13][39;00m
[38;5;252m[[39m[38;5;252mx[39m[38;5;252m [39m[38;5;70;01mfor[39;00m[38;5;252m [39m[38;5;252mx[39m[38;5;252m [39m[38;5;70;01min[39;00m[38;5;252m [39m[38;5;252m[[39m[38;5;67m3[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m4[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m5[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m6[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m7[39m[38;5;252m][39m[38;5;252m [39m[38;5;70;01mif[39;00m[38;5;252m [39m[38;5;252mx[39m[38;5;252m [39m[38;5;252m>[39m[38;5;252m [39m[38;5;67m5[39m[38;5;252m][39m[38;5;252m  [39m[38;5;246;03m# => [6, 7][39;00m

[38;5;246;03m# You can construct set and dict comprehensions as well.[39;00m
[38;5;252m{[39m[38;5;252mx[39m[38;5;252m [39m[38;5;70;01mfor[39;00m[38;5;252m [39m[38;5;252mx[39m[38;5;252m [39m[38;5;70;01min[39;00m[38;5;252m [39m[38;5;214m'[39m[38;5;214mabcddeef[39m[38;5;214m'[39m[38;5;252m [39m[38;5;70;01mif[39;00m[38;5;252m [39m[38;5;252mx[39m[38;5;252m [39m[38;5;70;01mnot[39;00m[38;5;252m [39m[38;5;70;01min[39;00m[38;5;252m [39m[38;5;214m'[39m[38;5;214mabc[39m[38;5;214m'[39m[38;5;252m}[39m[38;5;252m  [39m[38;5;246;03m# => {'d', 'e', 'f'}[39;00m
[38;5;252m{[39m[38;5;252mx[39m[38;5;252m:[39m[38;5;252m [39m[38;5;252mx[39m[38;5;252m*[39m[38;5;252m*[39m[38;5;67m2[39m[38;5;252m [39m[38;5;70;01mfor[39;00m[38;5;252m [39m[38;5;252mx[39m[38;5;252m [39m[38;5;70;01min[39;00m[38;5;252m [39m[38;5;31mrange[39m[38;5;252m([39m[38;5;67m5[39m[38;5;252m)[39m[38;5;252m}[39m[38;5;252m  [39m[38;5;246;03m# => {0: 0, 1: 1, 2: 4, 3: 9, 4: 16}[39;00m


[38;5;246;03m####################################################[39;00m
[38;5;246;03m## 5. Modules[39;00m
[38;5;246;03m####################################################[39;00m

[38;5;246;03m# You can import modules[39;00m
[38;5;70;01mimport[39;00m[38;5;252m [39m[38;5;68;04mmath[39;00m
[38;5;31mprint[39m[38;5;252m([39m[38;5;252mmath[39m[38;5;252m.[39m[38;5;252msqrt[39m[38;5;252m([39m[38;5;67m16[39m[38;5;252m)[39m[38;5;252m)[39m[38;5;252m  [39m[38;5;246;03m# => 4.0[39;00m

[38;5;246;03m# You can get specific functions from a module[39;00m
[38;5;70;01mfrom[39;00m[38;5;252m [39m[38;5;68;04mmath[39;00m[38;5;252m [39m[38;5;70;01mimport[39;00m[38;5;252m [39m[38;5;252mceil[39m[38;5;252m,[39m[38;5;252m [39m[38;5;252mfloor[39m
[38;5;31mprint[39m[38;5;252m([39m[38;5;252mceil[39m[38;5;252m([39m[38;5;67m3.7[39m[38;5;252m)[39m[38;5;252m)[39m[38;5;252m   [39m[38;5;246;03m# => 4.0[39;00m
[38;5;31mprint[39m[38;5;252m([39m[38;5;252mfloor[39m[38;5;252m([39m[38;5;67m3.7[39m[38;5;252m)[39m[38;5;252m)[39m[38;5;252m  [39m[38;5;246;03m# => 3.0[39;00m

[38;5;246;03m# You can import all functions from a module.[39;00m
[38;5;246;03m# Warning: this is not recommended[39;00m
[38;5;70;01mfrom[39;00m[38;5;252m [39m[38;5;68;04mmath[39;00m[38;5;252m [39m[38;5;70;01mimport[39;00m[38;5;252m [39m[38;5;252m*[39m

[38;5;246;03m# You can shorten module names[39;00m
[38;5;70;01mimport[39;00m[38;5;252m [39m[38;5;68;04mmath[39;00m[38;5;252m [39m[38;5;70;01mas[39;00m[38;5;252m [39m[38;5;68;04mm[39;00m
[38;5;252mmath[39m[38;5;252m.[39m[38;5;252msqrt[39m[38;5;252m([39m[38;5;67m16[39m[38;5;252m)[39m[38;5;252m [39m[38;5;252m==[39m[38;5;252m [39m[38;5;252mm[39m[38;5;252m.[39m[38;5;252msqrt[39m[38;5;252m([39m[38;5;67m16[39m[38;5;252m)[39m[38;5;252m  [39m[38;5;246;03m# => True[39;00m

[38;5;246;03m# Python modules are just ordinary Python files. You[39;00m
[38;5;246;03m# can write your own, and import them. The name of the[39;00m
[38;5;246;03m# module is the same as the name of the file.[39;00m

[38;5;246;03m# You can find out which functions and attributes[39;00m
[38;5;246;03m# are defined in a module.[39;00m
[38;5;70;01mimport[39;00m[38;5;252m [39m[38;5;68;04mmath[39;00m
[38;5;31mdir[39m[38;5;252m([39m[38;5;252mmath[39m[38;5;252m)[39m

[38;5;246;03m# If you have a Python script named math.py in the same[39;00m
[38;5;246;03m# folder as your current script, the file math.py will[39;00m
[38;5;246;03m# be loaded instead of the built-in Python module.[39;00m
[38;5;246;03m# This happens because the local folder has priority[39;00m
[38;5;246;03m# over Python's built-in libraries.[39;00m


[38;5;246;03m####################################################[39;00m
[38;5;246;03m## 6. Classes[39;00m
[38;5;246;03m####################################################[39;00m

[38;5;246;03m# We use the "class" statement to create a class[39;00m
[38;5;70;01mclass[39;00m[38;5;252m [39m[38;5;68;04mHuman[39;00m[38;5;252m:[39m

[38;5;252m    [39m[38;5;246;03m# A class attribute. It is shared by all instances of this class[39;00m
[38;5;252m    [39m[38;5;252mspecies[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;214m"[39m[38;5;214mH. sapiens[39m[38;5;214m"[39m

[38;5;252m    [39m[38;5;246;03m# Basic initializer, this is called when this class is instantiated.[39;00m
[38;5;252m    [39m[38;5;246;03m# Note that the double leading and trailing underscores denote objects[39;00m
[38;5;252m    [39m[38;5;246;03m# or attributes that are used by Python but that live in user-controlled[39;00m
[38;5;252m    [39m[38;5;246;03m# namespaces. Methods(or objects or attributes) like: __init__, __str__,[39;00m
[38;5;252m    [39m[38;5;246;03m# __repr__ etc. are called special methods (or sometimes called dunder methods)[39;00m
[38;5;252m    [39m[38;5;246;03m# You should not invent such names on your own.[39;00m
[38;5;252m    [39m[38;5;70;01mdef[39;00m[38;5;252m [39m[38;5;68m__init__[39m[38;5;252m([39m[38;5;31mself[39m[38;5;252m,[39m[38;5;252m [39m[38;5;252mname[39m[38;5;252m)[39m[38;5;252m:[39m
[38;5;252m        [39m[38;5;246;03m# Assign the argument to the instance's name attribute[39;00m
[38;5;252m        [39m[38;5;31mself[39m[38;5;252m.[39m[38;5;252mname[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252mname[39m

[38;5;252m        [39m[38;5;246;03m# Initialize property[39;00m
[38;5;252m        [39m[38;5;31mself[39m[38;5;252m.[39m[38;5;252m_age[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;67m0[39m

[38;5;252m    [39m[38;5;246;03m# An instance method. All methods take "self" as the first argument[39;00m
[38;5;252m    [39m[38;5;70;01mdef[39;00m[38;5;252m [39m[38;5;68msay[39m[38;5;252m([39m[38;5;31mself[39m[38;5;252m,[39m[38;5;252m [39m[38;5;252mmsg[39m[38;5;252m)[39m[38;5;252m:[39m
[38;5;252m        [39m[38;5;31mprint[39m[38;5;252m([39m[38;5;214m"[39m[38;5;214m{name}[39m[38;5;214m: [39m[38;5;214m{message}[39m[38;5;214m"[39m[38;5;252m.[39m[38;5;252mformat[39m[38;5;252m([39m[38;5;252mname[39m[38;5;252m=[39m[38;5;31mself[39m[38;5;252m.[39m[38;5;252mname[39m[38;5;252m,[39m[38;5;252m [39m[38;5;252mmessage[39m[38;5;252m=[39m[38;5;252mmsg[39m[38;5;252m)[39m[38;5;252m)[39m

[38;5;252m    [39m[38;5;246;03m# Another instance method[39;00m
[38;5;252m    [39m[38;5;70;01mdef[39;00m[38;5;252m [39m[38;5;68msing[39m[38;5;252m([39m[38;5;31mself[39m[38;5;252m)[39m[38;5;252m:[39m
[38;5;252m        [39m[38;5;70;01mreturn[39;00m[38;5;252m [39m[38;5;214m'[39m[38;5;214myo... yo... microphone check... one two... one two...[39m[38;5;214m'[39m

[38;5;252m    [39m[38;5;246;03m# A class method is shared among all instances[39;00m
[38;5;252m    [39m[38;5;246;03m# They are called with the calling class as the first argument[39;00m
[38;5;252m    [39m[38;5;214m@classmethod[39m
[38;5;252m    [39m[38;5;70;01mdef[39;00m[38;5;252m [39m[38;5;68mget_species[39m[38;5;252m([39m[38;5;31mcls[39m[38;5;252m)[39m[38;5;252m:[39m
[38;5;252m        [39m[38;5;70;01mreturn[39;00m[38;5;252m [39m[38;5;31mcls[39m[38;5;252m.[39m[38;5;252mspecies[39m

[38;5;252m    [39m[38;5;246;03m# A static method is called without a class or instance reference[39;00m
[38;5;252m    [39m[38;5;214m@staticmethod[39m
[38;5;252m    [39m[38;5;70;01mdef[39;00m[38;5;252m [39m[38;5;68mgrunt[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m:[39m
[38;5;252m        [39m[38;5;70;01mreturn[39;00m[38;5;252m [39m[38;5;214m"[39m[38;5;214m*grunt*[39m[38;5;214m"[39m

[38;5;252m    [39m[38;5;246;03m# A property is just like a getter.[39;00m
[38;5;252m    [39m[38;5;246;03m# It turns the method age() into a read-only attribute of the same name.[39;00m
[38;5;252m    [39m[38;5;246;03m# There's no need to write trivial getters and setters in Python, though.[39;00m
[38;5;252m    [39m[38;5;214m@property[39m
[38;5;252m    [39m[38;5;70;01mdef[39;00m[38;5;252m [39m[38;5;68mage[39m[38;5;252m([39m[38;5;31mself[39m[38;5;252m)[39m[38;5;252m:[39m
[38;5;252m        [39m[38;5;70;01mreturn[39;00m[38;5;252m [39m[38;5;31mself[39m[38;5;252m.[39m[38;5;252m_age[39m

[38;5;252m    [39m[38;5;246;03m# This allows the property to be set[39;00m
[38;5;252m    [39m[38;5;214m@age[39m[38;5;252m.[39m[38;5;252msetter[39m
[38;5;252m    [39m[38;5;70;01mdef[39;00m[38;5;252m [39m[38;5;68mage[39m[38;5;252m([39m[38;5;31mself[39m[38;5;252m,[39m[38;5;252m [39m[38;5;252mage[39m[38;5;252m)[39m[38;5;252m:[39m
[38;5;252m        [39m[38;5;31mself[39m[38;5;252m.[39m[38;5;252m_age[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252mage[39m

[38;5;252m    [39m[38;5;246;03m# This allows the property to be deleted[39;00m
[38;5;252m    [39m[38;5;214m@age[39m[38;5;252m.[39m[38;5;252mdeleter[39m
[38;5;252m    [39m[38;5;70;01mdef[39;00m[38;5;252m [39m[38;5;68mage[39m[38;5;252m([39m[38;5;31mself[39m[38;5;252m)[39m[38;5;252m:[39m
[38;5;252m        [39m[38;5;70;01mdel[39;00m[38;5;252m [39m[38;5;31mself[39m[38;5;252m.[39m[38;5;252m_age[39m


[38;5;246;03m# When a Python interpreter reads a source file it executes all its code.[39;00m
[38;5;246;03m# This __name__ check makes sure this code block is only executed when this[39;00m
[38;5;246;03m# module is the main program.[39;00m
[38;5;70;01mif[39;00m[38;5;252m [39m[38;5;87m__name__[39m[38;5;252m [39m[38;5;252m==[39m[38;5;252m [39m[38;5;214m'[39m[38;5;214m__main__[39m[38;5;214m'[39m[38;5;252m:[39m
[38;5;252m    [39m[38;5;246;03m# Instantiate a class[39;00m
[38;5;252m    [39m[38;5;252mi[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252mHuman[39m[38;5;252m([39m[38;5;252mname[39m[38;5;252m=[39m[38;5;214m"[39m[38;5;214mIan[39m[38;5;214m"[39m[38;5;252m)[39m
[38;5;252m    [39m[38;5;252mi[39m[38;5;252m.[39m[38;5;252msay[39m[38;5;252m([39m[38;5;214m"[39m[38;5;214mhi[39m[38;5;214m"[39m[38;5;252m)[39m[38;5;252m                     [39m[38;5;246;03m# "Ian: hi"[39;00m
[38;5;252m    [39m[38;5;252mj[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252mHuman[39m[38;5;252m([39m[38;5;214m"[39m[38;5;214mJoel[39m[38;5;214m"[39m[38;5;252m)[39m
[38;5;252m    [39m[38;5;252mj[39m[38;5;252m.[39m[38;5;252msay[39m[38;5;252m([39m[38;5;214m"[39m[38;5;214mhello[39m[38;5;214m"[39m[38;5;252m)[39m[38;5;252m                  [39m[38;5;246;03m# "Joel: hello"[39;00m
[38;5;252m    [39m[38;5;246;03m# i and j are instances of type Human, or in other words: they are Human objects[39;00m

[38;5;252m    [39m[38;5;246;03m# Call our class method[39;00m
[38;5;252m    [39m[38;5;252mi[39m[38;5;252m.[39m[38;5;252msay[39m[38;5;252m([39m[38;5;252mi[39m[38;5;252m.[39m[38;5;252mget_species[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m)[39m[38;5;252m          [39m[38;5;246;03m# "Ian: H. sapiens"[39;00m
[38;5;252m    [39m[38;5;246;03m# Change the shared attribute[39;00m
[38;5;252m    [39m[38;5;252mHuman[39m[38;5;252m.[39m[38;5;252mspecies[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;214m"[39m[38;5;214mH. neanderthalensis[39m[38;5;214m"[39m
[38;5;252m    [39m[38;5;252mi[39m[38;5;252m.[39m[38;5;252msay[39m[38;5;252m([39m[38;5;252mi[39m[38;5;252m.[39m[38;5;252mget_species[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m)[39m[38;5;252m          [39m[38;5;246;03m# => "Ian: H. neanderthalensis"[39;00m
[38;5;252m    [39m[38;5;252mj[39m[38;5;252m.[39m[38;5;252msay[39m[38;5;252m([39m[38;5;252mj[39m[38;5;252m.[39m[38;5;252mget_species[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m)[39m[38;5;252m          [39m[38;5;246;03m# => "Joel: H. neanderthalensis"[39;00m

[38;5;252m    [39m[38;5;246;03m# Call the static method[39;00m
[38;5;252m    [39m[38;5;31mprint[39m[38;5;252m([39m[38;5;252mHuman[39m[38;5;252m.[39m[38;5;252mgrunt[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m)[39m[38;5;252m            [39m[38;5;246;03m# => "*grunt*"[39;00m

[38;5;252m    [39m[38;5;246;03m# Static methods can be called by instances too[39;00m
[38;5;252m    [39m[38;5;31mprint[39m[38;5;252m([39m[38;5;252mi[39m[38;5;252m.[39m[38;5;252mgrunt[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m)[39m[38;5;252m                [39m[38;5;246;03m# => "*grunt*"[39;00m

[38;5;252m    [39m[38;5;246;03m# Update the property for this instance[39;00m
[38;5;252m    [39m[38;5;252mi[39m[38;5;252m.[39m[38;5;252mage[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;67m42[39m
[38;5;252m    [39m[38;5;246;03m# Get the property[39;00m
[38;5;252m    [39m[38;5;252mi[39m[38;5;252m.[39m[38;5;252msay[39m[38;5;252m([39m[38;5;252mi[39m[38;5;252m.[39m[38;5;252mage[39m[38;5;252m)[39m[38;5;252m                    [39m[38;5;246;03m# => "Ian: 42"[39;00m
[38;5;252m    [39m[38;5;252mj[39m[38;5;252m.[39m[38;5;252msay[39m[38;5;252m([39m[38;5;252mj[39m[38;5;252m.[39m[38;5;252mage[39m[38;5;252m)[39m[38;5;252m                    [39m[38;5;246;03m# => "Joel: 0"[39;00m
[38;5;252m    [39m[38;5;246;03m# Delete the property[39;00m
[38;5;252m    [39m[38;5;70;01mdel[39;00m[38;5;252m [39m[38;5;252mi[39m[38;5;252m.[39m[38;5;252mage[39m
[38;5;252m    [39m[38;5;246;03m# i.age                         # => this would raise an AttributeError[39;00m


[38;5;246;03m####################################################[39;00m
[38;5;246;03m## 6.1 Inheritance[39;00m
[38;5;246;03m####################################################[39;00m

[38;5;246;03m# Inheritance allows new child classes to be defined that inherit methods and[39;00m
[38;5;246;03m# variables from their parent class.[39;00m

[38;5;246;03m# Using the Human class defined above as the base or parent class, we can[39;00m
[38;5;246;03m# define a child class, Superhero, which inherits the class variables like[39;00m
[38;5;246;03m# "species", "name", and "age", as well as methods, like "sing" and "grunt"[39;00m
[38;5;246;03m# from the Human class, but can also have its own unique properties.[39;00m

[38;5;246;03m# To take advantage of modularization by file you could place the classes above in their own files,[39;00m
[38;5;246;03m# say, human.py[39;00m

[38;5;246;03m# To import functions from other files use the following format[39;00m
[38;5;246;03m# from "filename-without-extension" import "function-or-class"[39;00m

[38;5;70;01mfrom[39;00m[38;5;252m [39m[38;5;68;04mhuman[39;00m[38;5;252m [39m[38;5;70;01mimport[39;00m[38;5;252m [39m[38;5;252mHuman[39m


[38;5;246;03m# Specify the parent class(es) as parameters to the class definition[39;00m
[38;5;70;01mclass[39;00m[38;5;252m [39m[38;5;68;04mSuperhero[39;00m[38;5;252m([39m[38;5;252mHuman[39m[38;5;252m)[39m[38;5;252m:[39m

[38;5;252m    [39m[38;5;246;03m# If the child class should inherit all of the parent's definitions without[39;00m
[38;5;252m    [39m[38;5;246;03m# any modifications, you can just use the "pass" keyword (and nothing else)[39;00m
[38;5;252m    [39m[38;5;246;03m# but in this case it is commented out to allow for a unique child class:[39;00m
[38;5;252m    [39m[38;5;246;03m# pass[39;00m

[38;5;252m    [39m[38;5;246;03m# Child classes can override their parents' attributes[39;00m
[38;5;252m    [39m[38;5;252mspecies[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;214m'[39m[38;5;214mSuperhuman[39m[38;5;214m'[39m

[38;5;252m    [39m[38;5;246;03m# Children automatically inherit their parent class's constructor including[39;00m
[38;5;252m    [39m[38;5;246;03m# its arguments, but can also define additional arguments or definitions[39;00m
[38;5;252m    [39m[38;5;246;03m# and override its methods such as the class constructor.[39;00m
[38;5;252m    [39m[38;5;246;03m# This constructor inherits the "name" argument from the "Human" class and[39;00m
[38;5;252m    [39m[38;5;246;03m# adds the "superpower" and "movie" arguments:[39;00m
[38;5;252m    [39m[38;5;70;01mdef[39;00m[38;5;252m [39m[38;5;68m__init__[39m[38;5;252m([39m[38;5;31mself[39m[38;5;252m,[39m[38;5;252m [39m[38;5;252mname[39m[38;5;252m,[39m[38;5;252m [39m[38;5;252mmovie[39m[38;5;252m=[39m[38;5;70;01mFalse[39;00m[38;5;252m,[39m
[38;5;252m                 [39m[38;5;252msuperpowers[39m[38;5;252m=[39m[38;5;252m[[39m[38;5;214m"[39m[38;5;214msuper strength[39m[38;5;214m"[39m[38;5;252m,[39m[38;5;252m [39m[38;5;214m"[39m[38;5;214mbulletproofing[39m[38;5;214m"[39m[38;5;252m][39m[38;5;252m)[39m[38;5;252m:[39m

[38;5;252m        [39m[38;5;246;03m# add additional class attributes:[39;00m
[38;5;252m        [39m[38;5;31mself[39m[38;5;252m.[39m[38;5;252mfictional[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;70;01mTrue[39;00m
[38;5;252m        [39m[38;5;31mself[39m[38;5;252m.[39m[38;5;252mmovie[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252mmovie[39m
[38;5;252m        [39m[38;5;246;03m# be aware of mutable default values, since defaults are shared[39;00m
[38;5;252m        [39m[38;5;31mself[39m[38;5;252m.[39m[38;5;252msuperpowers[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252msuperpowers[39m

[38;5;252m        [39m[38;5;246;03m# The "super" function lets you access the parent class's methods[39;00m
[38;5;252m        [39m[38;5;246;03m# that are overridden by the child, in this case, the __init__ method.[39;00m
[38;5;252m        [39m[38;5;246;03m# This calls the parent class constructor:[39;00m
[38;5;252m        [39m[38;5;31msuper[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m.[39m[38;5;68m__init__[39m[38;5;252m([39m[38;5;252mname[39m[38;5;252m)[39m

[38;5;252m    [39m[38;5;246;03m# override the sing method[39;00m
[38;5;252m    [39m[38;5;70;01mdef[39;00m[38;5;252m [39m[38;5;68msing[39m[38;5;252m([39m[38;5;31mself[39m[38;5;252m)[39m[38;5;252m:[39m
[38;5;252m        [39m[38;5;70;01mreturn[39;00m[38;5;252m [39m[38;5;214m'[39m[38;5;214mDun, dun, DUN![39m[38;5;214m'[39m

[38;5;252m    [39m[38;5;246;03m# add an additional instance method[39;00m
[38;5;252m    [39m[38;5;70;01mdef[39;00m[38;5;252m [39m[38;5;68mboast[39m[38;5;252m([39m[38;5;31mself[39m[38;5;252m)[39m[38;5;252m:[39m
[38;5;252m        [39m[38;5;70;01mfor[39;00m[38;5;252m [39m[38;5;252mpower[39m[38;5;252m [39m[38;5;70;01min[39;00m[38;5;252m [39m[38;5;31mself[39m[38;5;252m.[39m[38;5;252msuperpowers[39m[38;5;252m:[39m
[38;5;252m            [39m[38;5;31mprint[39m[38;5;252m([39m[38;5;214m"[39m[38;5;214mI wield the power of [39m[38;5;214m{pow}[39m[38;5;214m![39m[38;5;214m"[39m[38;5;252m.[39m[38;5;252mformat[39m[38;5;252m([39m[38;5;31mpow[39m[38;5;252m=[39m[38;5;252mpower[39m[38;5;252m)[39m[38;5;252m)[39m


[38;5;70;01mif[39;00m[38;5;252m [39m[38;5;87m__name__[39m[38;5;252m [39m[38;5;252m==[39m[38;5;252m [39m[38;5;214m'[39m[38;5;214m__main__[39m[38;5;214m'[39m[38;5;252m:[39m
[38;5;252m    [39m[38;5;252msup[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252mSuperhero[39m[38;5;252m([39m[38;5;252mname[39m[38;5;252m=[39m[38;5;214m"[39m[38;5;214mTick[39m[38;5;214m"[39m[38;5;252m)[39m

[38;5;252m    [39m[38;5;246;03m# Instance type checks[39;00m
[38;5;252m    [39m[38;5;70;01mif[39;00m[38;5;252m [39m[38;5;31misinstance[39m[38;5;252m([39m[38;5;252msup[39m[38;5;252m,[39m[38;5;252m [39m[38;5;252mHuman[39m[38;5;252m)[39m[38;5;252m:[39m
[38;5;252m        [39m[38;5;31mprint[39m[38;5;252m([39m[38;5;214m'[39m[38;5;214mI am human[39m[38;5;214m'[39m[38;5;252m)[39m
[38;5;252m    [39m[38;5;70;01mif[39;00m[38;5;252m [39m[38;5;31mtype[39m[38;5;252m([39m[38;5;252msup[39m[38;5;252m)[39m[38;5;252m [39m[38;5;70;01mis[39;00m[38;5;252m [39m[38;5;252mSuperhero[39m[38;5;252m:[39m
[38;5;252m        [39m[38;5;31mprint[39m[38;5;252m([39m[38;5;214m'[39m[38;5;214mI am a superhero[39m[38;5;214m'[39m[38;5;252m)[39m

[38;5;252m    [39m[38;5;246;03m# Get the Method Resolution search Order used by both getattr() and super()[39;00m
[38;5;252m    [39m[38;5;246;03m# This attribute is dynamic and can be updated[39;00m
[38;5;252m    [39m[38;5;31mprint[39m[38;5;252m([39m[38;5;252mSuperhero[39m[38;5;252m.[39m[38;5;87m__mro__[39m[38;5;252m)[39m[38;5;252m    [39m[38;5;246;03m# => (<class '__main__.Superhero'>,[39;00m
[38;5;252m                                [39m[38;5;246;03m# => <class 'human.Human'>, <class 'object'>)[39;00m

[38;5;252m    [39m[38;5;246;03m# Calls parent method but uses its own class attribute[39;00m
[38;5;252m    [39m[38;5;31mprint[39m[38;5;252m([39m[38;5;252msup[39m[38;5;252m.[39m[38;5;252mget_species[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m)[39m[38;5;252m    [39m[38;5;246;03m# => Superhuman[39;00m

[38;5;252m    [39m[38;5;246;03m# Calls overridden method[39;00m
[38;5;252m    [39m[38;5;31mprint[39m[38;5;252m([39m[38;5;252msup[39m[38;5;252m.[39m[38;5;252msing[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m)[39m[38;5;252m           [39m[38;5;246;03m# => Dun, dun, DUN![39;00m

[38;5;252m    [39m[38;5;246;03m# Calls method from Human[39;00m
[38;5;252m    [39m[38;5;252msup[39m[38;5;252m.[39m[38;5;252msay[39m[38;5;252m([39m[38;5;214m'[39m[38;5;214mSpoon[39m[38;5;214m'[39m[38;5;252m)[39m[38;5;252m            [39m[38;5;246;03m# => Tick: Spoon[39;00m

[38;5;252m    [39m[38;5;246;03m# Call method that exists only in Superhero[39;00m
[38;5;252m    [39m[38;5;252msup[39m[38;5;252m.[39m[38;5;252mboast[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m                 [39m[38;5;246;03m# => I wield the power of super strength![39;00m
[38;5;252m                                [39m[38;5;246;03m# => I wield the power of bulletproofing![39;00m

[38;5;252m    [39m[38;5;246;03m# Inherited class attribute[39;00m
[38;5;252m    [39m[38;5;252msup[39m[38;5;252m.[39m[38;5;252mage[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;67m31[39m
[38;5;252m    [39m[38;5;31mprint[39m[38;5;252m([39m[38;5;252msup[39m[38;5;252m.[39m[38;5;252mage[39m[38;5;252m)[39m[38;5;252m              [39m[38;5;246;03m# => 31[39;00m

[38;5;252m    [39m[38;5;246;03m# Attribute that only exists within Superhero[39;00m
[38;5;252m    [39m[38;5;31mprint[39m[38;5;252m([39m[38;5;214m'[39m[38;5;214mAm I Oscar eligible? [39m[38;5;214m'[39m[38;5;252m [39m[38;5;252m+[39m[38;5;252m [39m[38;5;31mstr[39m[38;5;252m([39m[38;5;252msup[39m[38;5;252m.[39m[38;5;252mmovie[39m[38;5;252m)[39m[38;5;252m)[39m

[38;5;246;03m####################################################[39;00m
[38;5;246;03m## 6.2 Multiple Inheritance[39;00m
[38;5;246;03m####################################################[39;00m

[38;5;246;03m# Another class definition[39;00m
[38;5;246;03m# bat.py[39;00m
[38;5;70;01mclass[39;00m[38;5;252m [39m[38;5;68;04mBat[39;00m[38;5;252m:[39m

[38;5;252m    [39m[38;5;252mspecies[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;214m'[39m[38;5;214mBaty[39m[38;5;214m'[39m

[38;5;252m    [39m[38;5;70;01mdef[39;00m[38;5;252m [39m[38;5;68m__init__[39m[38;5;252m([39m[38;5;31mself[39m[38;5;252m,[39m[38;5;252m [39m[38;5;252mcan_fly[39m[38;5;252m=[39m[38;5;70;01mTrue[39;00m[38;5;252m)[39m[38;5;252m:[39m
[38;5;252m        [39m[38;5;31mself[39m[38;5;252m.[39m[38;5;252mfly[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252mcan_fly[39m

[38;5;252m    [39m[38;5;246;03m# This class also has a say method[39;00m
[38;5;252m    [39m[38;5;70;01mdef[39;00m[38;5;252m [39m[38;5;68msay[39m[38;5;252m([39m[38;5;31mself[39m[38;5;252m,[39m[38;5;252m [39m[38;5;252mmsg[39m[38;5;252m)[39m[38;5;252m:[39m
[38;5;252m        [39m[38;5;252mmsg[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;214m'[39m[38;5;214m... ... ...[39m[38;5;214m'[39m
[38;5;252m        [39m[38;5;70;01mreturn[39;00m[38;5;252m [39m[38;5;252mmsg[39m

[38;5;252m    [39m[38;5;246;03m# And its own method as well[39;00m
[38;5;252m    [39m[38;5;70;01mdef[39;00m[38;5;252m [39m[38;5;68msonar[39m[38;5;252m([39m[38;5;31mself[39m[38;5;252m)[39m[38;5;252m:[39m
[38;5;252m        [39m[38;5;70;01mreturn[39;00m[38;5;252m [39m[38;5;214m'[39m[38;5;214m))) ... ((([39m[38;5;214m'[39m

[38;5;70;01mif[39;00m[38;5;252m [39m[38;5;87m__name__[39m[38;5;252m [39m[38;5;252m==[39m[38;5;252m [39m[38;5;214m'[39m[38;5;214m__main__[39m[38;5;214m'[39m[38;5;252m:[39m
[38;5;252m    [39m[38;5;252mb[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252mBat[39m[38;5;252m([39m[38;5;252m)[39m
[38;5;252m    [39m[38;5;31mprint[39m[38;5;252m([39m[38;5;252mb[39m[38;5;252m.[39m[38;5;252msay[39m[38;5;252m([39m[38;5;214m'[39m[38;5;214mhello[39m[38;5;214m'[39m[38;5;252m)[39m[38;5;252m)[39m
[38;5;252m    [39m[38;5;31mprint[39m[38;5;252m([39m[38;5;252mb[39m[38;5;252m.[39m[38;5;252mfly[39m[38;5;252m)[39m


[38;5;246;03m# And yet another class definition that inherits from Superhero and Bat[39;00m
[38;5;246;03m# superhero.py[39;00m
[38;5;70;01mfrom[39;00m[38;5;252m [39m[38;5;68;04msuperhero[39;00m[38;5;252m [39m[38;5;70;01mimport[39;00m[38;5;252m [39m[38;5;252mSuperhero[39m
[38;5;70;01mfrom[39;00m[38;5;252m [39m[38;5;68;04mbat[39;00m[38;5;252m [39m[38;5;70;01mimport[39;00m[38;5;252m [39m[38;5;252mBat[39m

[38;5;246;03m# Define Batman as a child that inherits from both Superhero and Bat[39;00m
[38;5;70;01mclass[39;00m[38;5;252m [39m[38;5;68;04mBatman[39;00m[38;5;252m([39m[38;5;252mSuperhero[39m[38;5;252m,[39m[38;5;252m [39m[38;5;252mBat[39m[38;5;252m)[39m[38;5;252m:[39m

[38;5;252m    [39m[38;5;70;01mdef[39;00m[38;5;252m [39m[38;5;68m__init__[39m[38;5;252m([39m[38;5;31mself[39m[38;5;252m,[39m[38;5;252m [39m[38;5;252m*[39m[38;5;252margs[39m[38;5;252m,[39m[38;5;252m [39m[38;5;252m*[39m[38;5;252m*[39m[38;5;252mkwargs[39m[38;5;252m)[39m[38;5;252m:[39m
[38;5;252m        [39m[38;5;246;03m# Typically to inherit attributes you have to call super:[39;00m
[38;5;252m        [39m[38;5;246;03m# super(Batman, self).__init__(*args, **kwargs)[39;00m
[38;5;252m        [39m[38;5;246;03m# However we are dealing with multiple inheritance here, and super()[39;00m
[38;5;252m        [39m[38;5;246;03m# only works with the next base class in the MRO list.[39;00m
[38;5;252m        [39m[38;5;246;03m# So instead we explicitly call __init__ for all ancestors.[39;00m
[38;5;252m        [39m[38;5;246;03m# The use of *args and **kwargs allows for a clean way to pass arguments,[39;00m
[38;5;252m        [39m[38;5;246;03m# with each parent "peeling a layer of the onion".[39;00m
[38;5;252m        [39m[38;5;252mSuperhero[39m[38;5;252m.[39m[38;5;68m__init__[39m[38;5;252m([39m[38;5;31mself[39m[38;5;252m,[39m[38;5;252m [39m[38;5;214m'[39m[38;5;214manonymous[39m[38;5;214m'[39m[38;5;252m,[39m[38;5;252m [39m[38;5;252mmovie[39m[38;5;252m=[39m[38;5;70;01mTrue[39;00m[38;5;252m,[39m
[38;5;252m                           [39m[38;5;252msuperpowers[39m[38;5;252m=[39m[38;5;252m[[39m[38;5;214m'[39m[38;5;214mWealthy[39m[38;5;214m'[39m[38;5;252m][39m[38;5;252m,[39m[38;5;252m [39m[38;5;252m*[39m[38;5;252margs[39m[38;5;252m,[39m[38;5;252m [39m[38;5;252m*[39m[38;5;252m*[39m[38;5;252mkwargs[39m[38;5;252m)[39m
[38;5;252m        [39m[38;5;252mBat[39m[38;5;252m.[39m[38;5;68m__init__[39m[38;5;252m([39m[38;5;31mself[39m[38;5;252m,[39m[38;5;252m [39m[38;5;252m*[39m[38;5;252margs[39m[38;5;252m,[39m[38;5;252m [39m[38;5;252mcan_fly[39m[38;5;252m=[39m[38;5;70;01mFalse[39;00m[38;5;252m,[39m[38;5;252m [39m[38;5;252m*[39m[38;5;252m*[39m[38;5;252mkwargs[39m[38;5;252m)[39m
[38;5;252m        [39m[38;5;246;03m# override the value for the name attribute[39;00m
[38;5;252m        [39m[38;5;31mself[39m[38;5;252m.[39m[38;5;252mname[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;214m'[39m[38;5;214mSad Affleck[39m[38;5;214m'[39m

[38;5;252m    [39m[38;5;70;01mdef[39;00m[38;5;252m [39m[38;5;68msing[39m[38;5;252m([39m[38;5;31mself[39m[38;5;252m)[39m[38;5;252m:[39m
[38;5;252m        [39m[38;5;70;01mreturn[39;00m[38;5;252m [39m[38;5;214m'[39m[38;5;214mnan nan nan nan nan batman![39m[38;5;214m'[39m


[38;5;70;01mif[39;00m[38;5;252m [39m[38;5;87m__name__[39m[38;5;252m [39m[38;5;252m==[39m[38;5;252m [39m[38;5;214m'[39m[38;5;214m__main__[39m[38;5;214m'[39m[38;5;252m:[39m
[38;5;252m    [39m[38;5;252msup[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252mBatman[39m[38;5;252m([39m[38;5;252m)[39m

[38;5;252m    [39m[38;5;246;03m# Get the Method Resolution search Order used by both getattr() and super().[39;00m
[38;5;252m    [39m[38;5;246;03m# This attribute is dynamic and can be updated[39;00m
[38;5;252m    [39m[38;5;31mprint[39m[38;5;252m([39m[38;5;252mBatman[39m[38;5;252m.[39m[38;5;87m__mro__[39m[38;5;252m)[39m[38;5;252m       [39m[38;5;246;03m# => (<class '__main__.Batman'>,[39;00m
[38;5;252m                                [39m[38;5;246;03m# => <class 'superhero.Superhero'>,[39;00m
[38;5;252m                                [39m[38;5;246;03m# => <class 'human.Human'>,[39;00m
[38;5;252m                                [39m[38;5;246;03m# => <class 'bat.Bat'>, <class 'object'>)[39;00m

[38;5;252m    [39m[38;5;246;03m# Calls parent method but uses its own class attribute[39;00m
[38;5;252m    [39m[38;5;31mprint[39m[38;5;252m([39m[38;5;252msup[39m[38;5;252m.[39m[38;5;252mget_species[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m)[39m[38;5;252m    [39m[38;5;246;03m# => Superhuman[39;00m

[38;5;252m    [39m[38;5;246;03m# Calls overridden method[39;00m
[38;5;252m    [39m[38;5;31mprint[39m[38;5;252m([39m[38;5;252msup[39m[38;5;252m.[39m[38;5;252msing[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m)[39m[38;5;252m           [39m[38;5;246;03m# => nan nan nan nan nan batman![39;00m

[38;5;252m    [39m[38;5;246;03m# Calls method from Human, because inheritance order matters[39;00m
[38;5;252m    [39m[38;5;252msup[39m[38;5;252m.[39m[38;5;252msay[39m[38;5;252m([39m[38;5;214m'[39m[38;5;214mI agree[39m[38;5;214m'[39m[38;5;252m)[39m[38;5;252m          [39m[38;5;246;03m# => Sad Affleck: I agree[39;00m

[38;5;252m    [39m[38;5;246;03m# Call method that exists only in 2nd ancestor[39;00m
[38;5;252m    [39m[38;5;31mprint[39m[38;5;252m([39m[38;5;252msup[39m[38;5;252m.[39m[38;5;252msonar[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m)[39m[38;5;252m          [39m[38;5;246;03m# => ))) ... ((([39;00m

[38;5;252m    [39m[38;5;246;03m# Inherited class attribute[39;00m
[38;5;252m    [39m[38;5;252msup[39m[38;5;252m.[39m[38;5;252mage[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;67m100[39m
[38;5;252m    [39m[38;5;31mprint[39m[38;5;252m([39m[38;5;252msup[39m[38;5;252m.[39m[38;5;252mage[39m[38;5;252m)[39m[38;5;252m              [39m[38;5;246;03m# => 100[39;00m

[38;5;252m    [39m[38;5;246;03m# Inherited attribute from 2nd ancestor whose default value was overridden.[39;00m
[38;5;252m    [39m[38;5;31mprint[39m[38;5;252m([39m[38;5;214m'[39m[38;5;214mCan I fly? [39m[38;5;214m'[39m[38;5;252m [39m[38;5;252m+[39m[38;5;252m [39m[38;5;31mstr[39m[38;5;252m([39m[38;5;252msup[39m[38;5;252m.[39m[38;5;252mfly[39m[38;5;252m)[39m[38;5;252m)[39m[38;5;252m [39m[38;5;246;03m# => Can I fly? False[39;00m



[38;5;246;03m####################################################[39;00m
[38;5;246;03m## 7. Advanced[39;00m
[38;5;246;03m####################################################[39;00m

[38;5;246;03m# Generators help you make lazy code.[39;00m
[38;5;70;01mdef[39;00m[38;5;252m [39m[38;5;68mdouble_numbers[39m[38;5;252m([39m[38;5;252miterable[39m[38;5;252m)[39m[38;5;252m:[39m
[38;5;252m    [39m[38;5;70;01mfor[39;00m[38;5;252m [39m[38;5;252mi[39m[38;5;252m [39m[38;5;70;01min[39;00m[38;5;252m [39m[38;5;252miterable[39m[38;5;252m:[39m
[38;5;252m        [39m[38;5;70;01myield[39;00m[38;5;252m [39m[38;5;252mi[39m[38;5;252m [39m[38;5;252m+[39m[38;5;252m [39m[38;5;252mi[39m

[38;5;246;03m# Generators are memory-efficient because they only load the data needed to[39;00m
[38;5;246;03m# process the next value in the iterable. This allows them to perform[39;00m
[38;5;246;03m# operations on otherwise prohibitively large value ranges.[39;00m
[38;5;246;03m# NOTE: `range` replaces `xrange` in Python 3.[39;00m
[38;5;70;01mfor[39;00m[38;5;252m [39m[38;5;252mi[39m[38;5;252m [39m[38;5;70;01min[39;00m[38;5;252m [39m[38;5;252mdouble_numbers[39m[38;5;252m([39m[38;5;31mrange[39m[38;5;252m([39m[38;5;67m1[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m900000000[39m[38;5;252m)[39m[38;5;252m)[39m[38;5;252m:[39m[38;5;252m  [39m[38;5;246;03m# `range` is a generator.[39;00m
[38;5;252m    [39m[38;5;31mprint[39m[38;5;252m([39m[38;5;252mi[39m[38;5;252m)[39m
[38;5;252m    [39m[38;5;70;01mif[39;00m[38;5;252m [39m[38;5;252mi[39m[38;5;252m [39m[38;5;252m>[39m[38;5;252m=[39m[38;5;252m [39m[38;5;67m30[39m[38;5;252m:[39m
[38;5;252m        [39m[38;5;70;01mbreak[39;00m

[38;5;246;03m# Just as you can create a list comprehension, you can create generator[39;00m
[38;5;246;03m# comprehensions as well.[39;00m
[38;5;252mvalues[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252m([39m[38;5;252m-[39m[38;5;252mx[39m[38;5;252m [39m[38;5;70;01mfor[39;00m[38;5;252m [39m[38;5;252mx[39m[38;5;252m [39m[38;5;70;01min[39;00m[38;5;252m [39m[38;5;252m[[39m[38;5;67m1[39m[38;5;252m,[39m[38;5;67m2[39m[38;5;252m,[39m[38;5;67m3[39m[38;5;252m,[39m[38;5;67m4[39m[38;5;252m,[39m[38;5;67m5[39m[38;5;252m][39m[38;5;252m)[39m
[38;5;70;01mfor[39;00m[38;5;252m [39m[38;5;252mx[39m[38;5;252m [39m[38;5;70;01min[39;00m[38;5;252m [39m[38;5;252mvalues[39m[38;5;252m:[39m
[38;5;252m    [39m[38;5;31mprint[39m[38;5;252m([39m[38;5;252mx[39m[38;5;252m)[39m[38;5;252m  [39m[38;5;246;03m# prints -1 -2 -3 -4 -5 to console/terminal[39;00m

[38;5;246;03m# You can also cast a generator comprehension directly to a list.[39;00m
[38;5;252mvalues[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252m([39m[38;5;252m-[39m[38;5;252mx[39m[38;5;252m [39m[38;5;70;01mfor[39;00m[38;5;252m [39m[38;5;252mx[39m[38;5;252m [39m[38;5;70;01min[39;00m[38;5;252m [39m[38;5;252m[[39m[38;5;67m1[39m[38;5;252m,[39m[38;5;67m2[39m[38;5;252m,[39m[38;5;67m3[39m[38;5;252m,[39m[38;5;67m4[39m[38;5;252m,[39m[38;5;67m5[39m[38;5;252m][39m[38;5;252m)[39m
[38;5;252mgen_to_list[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;31mlist[39m[38;5;252m([39m[38;5;252mvalues[39m[38;5;252m)[39m
[38;5;31mprint[39m[38;5;252m([39m[38;5;252mgen_to_list[39m[38;5;252m)[39m[38;5;252m  [39m[38;5;246;03m# => [-1, -2, -3, -4, -5][39;00m


[38;5;246;03m# Decorators[39;00m
[38;5;246;03m# In this example `beg` wraps `say`. If say_please is True then it[39;00m
[38;5;246;03m# will change the returned message.[39;00m
[38;5;70;01mfrom[39;00m[38;5;252m [39m[38;5;68;04mfunctools[39;00m[38;5;252m [39m[38;5;70;01mimport[39;00m[38;5;252m [39m[38;5;252mwraps[39m


[38;5;70;01mdef[39;00m[38;5;252m [39m[38;5;68mbeg[39m[38;5;252m([39m[38;5;252mtarget_function[39m[38;5;252m)[39m[38;5;252m:[39m
[38;5;252m    [39m[38;5;214m@wraps[39m[38;5;252m([39m[38;5;252mtarget_function[39m[38;5;252m)[39m
[38;5;252m    [39m[38;5;70;01mdef[39;00m[38;5;252m [39m[38;5;68mwrapper[39m[38;5;252m([39m[38;5;252m*[39m[38;5;252margs[39m[38;5;252m,[39m[38;5;252m [39m[38;5;252m*[39m[38;5;252m*[39m[38;5;252mkwargs[39m[38;5;252m)[39m[38;5;252m:[39m
[38;5;252m        [39m[38;5;252mmsg[39m[38;5;252m,[39m[38;5;252m [39m[38;5;252msay_please[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252mtarget_function[39m[38;5;252m([39m[38;5;252m*[39m[38;5;252margs[39m[38;5;252m,[39m[38;5;252m [39m[38;5;252m*[39m[38;5;252m*[39m[38;5;252mkwargs[39m[38;5;252m)[39m
[38;5;252m        [39m[38;5;70;01mif[39;00m[38;5;252m [39m[38;5;252msay_please[39m[38;5;252m:[39m
[38;5;252m            [39m[38;5;70;01mreturn[39;00m[38;5;252m [39m[38;5;214m"[39m[38;5;214m{}[39m[38;5;214m [39m[38;5;214m{}[39m[38;5;214m"[39m[38;5;252m.[39m[38;5;252mformat[39m[38;5;252m([39m[38;5;252mmsg[39m[38;5;252m,[39m[38;5;252m [39m[38;5;214m"[39m[38;5;214mPlease! I am poor :([39m[38;5;214m"[39m[38;5;252m)[39m
[38;5;252m        [39m[38;5;70;01mreturn[39;00m[38;5;252m [39m[38;5;252mmsg[39m

[38;5;252m    [39m[38;5;70;01mreturn[39;00m[38;5;252m [39m[38;5;252mwrapper[39m


[38;5;214m@beg[39m
[38;5;70;01mdef[39;00m[38;5;252m [39m[38;5;68msay[39m[38;5;252m([39m[38;5;252msay_please[39m[38;5;252m=[39m[38;5;70;01mFalse[39;00m[38;5;252m)[39m[38;5;252m:[39m
[38;5;252m    [39m[38;5;252mmsg[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;214m"[39m[38;5;214mCan you buy me a beer?[39m[38;5;214m"[39m
[38;5;252m    [39m[38;5;70;01mreturn[39;00m[38;5;252m [39m[38;5;252mmsg[39m[38;5;252m,[39m[38;5;252m [39m[38;5;252msay_please[39m


[38;5;31mprint[39m[38;5;252m([39m[38;5;252msay[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m)[39m[38;5;252m                 [39m[38;5;246;03m# Can you buy me a beer?[39;00m
[38;5;31mprint[39m[38;5;252m([39m[38;5;252msay[39m[38;5;252m([39m[38;5;252msay_please[39m[38;5;252m=[39m[38;5;70;01mTrue[39;00m[38;5;252m)[39m[38;5;252m)[39m[38;5;252m  [39m[38;5;246;03m# Can you buy me a beer? Please! I am poor :([39;00m
