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

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

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

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

[38;5;246m# Math is what you would expect[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;246m# => 2[39m
[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;246m# => 7[39m
[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;246m# => 20[39m
[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;246m# => 7[39m

[38;5;246m# Division is a bit tricky. It is integer division and floors the results[39m
[38;5;246m# automatically.[39m
[38;5;67m5[39m[38;5;252m [39m[38;5;252m/[39m[38;5;252m [39m[38;5;67m2[39m[38;5;252m  [39m[38;5;246m# => 2[39m

[38;5;246m# To fix division we need to learn about floats.[39m
[38;5;67m2.0[39m[38;5;252m  [39m[38;5;246m# This is a float[39m
[38;5;67m11.0[39m[38;5;252m [39m[38;5;252m/[39m[38;5;252m [39m[38;5;67m4.0[39m[38;5;252m  [39m[38;5;246m# => 2.75 ahhh...much better[39m

[38;5;246m# Result of integer division truncated down both for positive and negative.[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;246m# => 1[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;246m# => 1.0 # works on floats too[39m
[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;246m# => -2[39m
[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;246m# => -2.0[39m

[38;5;246m# Note that we can also import division module(Section 6 Modules)[39m
[38;5;246m# to carry out normal division with just one '/'.[39m
[38;5;70;01mfrom[39;00m[38;5;252m [39m[38;5;68;04m__future__[39;00m[38;5;252m [39m[38;5;70;01mimport[39;00m[38;5;252m [39m[38;5;252mdivision[39m

[38;5;67m11[39m[38;5;252m [39m[38;5;252m/[39m[38;5;252m [39m[38;5;67m4[39m[38;5;252m  [39m[38;5;246m# => 2.75  ...normal division[39m
[38;5;67m11[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;246m# => 2 ...floored division[39m

[38;5;246m# Modulo operation[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;246m# => 1[39m

[38;5;246m# Exponentiation (x to the yth power)[39m
[38;5;67m2[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;246m# => 16[39m

[38;5;246m# Enforce precedence with parentheses[39m
[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;246m# => 8[39m

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

[38;5;246m# Note using Bool operators with ints[39m
[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;246m# => 0[39m
[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;246m# => -5[39m
[38;5;67m0[39m[38;5;252m [39m[38;5;252m==[39m[38;5;252m [39m[38;5;31mFalse[39m[38;5;252m  [39m[38;5;246m# => True[39m
[38;5;67m2[39m[38;5;252m [39m[38;5;252m==[39m[38;5;252m [39m[38;5;31mTrue[39m[38;5;252m  [39m[38;5;246m# => False[39m
[38;5;67m1[39m[38;5;252m [39m[38;5;252m==[39m[38;5;252m [39m[38;5;31mTrue[39m[38;5;252m  [39m[38;5;246m# => True[39m

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

[38;5;246m# Equality is ==[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;246m# => True[39m
[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;246m# => False[39m

[38;5;246m# Inequality is !=[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;246m# => False[39m
[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;246m# => True[39m

[38;5;246m# More comparisons[39m
[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;246m# => True[39m
[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;246m# => False[39m
[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;246m# => True[39m
[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;246m# => True[39m

[38;5;246m# Comparisons can be chained![39m
[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;246m# => True[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;252m<[39m[38;5;252m [39m[38;5;67m2[39m[38;5;252m  [39m[38;5;246m# => False[39m

[38;5;246m# Strings are created with " or '[39m
[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;246m# Strings can be added too![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;214m"[39m[38;5;214mworld![39m[38;5;214m"[39m[38;5;252m  [39m[38;5;246m# => "Hello world!"[39m
[38;5;246m# Strings can be added without using '+'[39m
[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;246m# => "Hello world!"[39m

[38;5;246m# ... or multiplied[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;67m3[39m[38;5;252m  [39m[38;5;246m# => "HelloHelloHello"[39m

[38;5;246m# A string can be treated like a list of characters[39m
[38;5;214m"[39m[38;5;214mThis is a string[39m[38;5;214m"[39m[38;5;252m[[39m[38;5;67m0[39m[38;5;252m][39m[38;5;252m  [39m[38;5;246m# => 'T'[39m

[38;5;246m# You can find the length of a string[39m
[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;246m# => 16[39m

[38;5;246m# String formatting with %[39m
[38;5;246m# Even though the % string operator will be deprecated on Python 3.1 and removed[39m
[38;5;246m# later at some time, it may still be good to know how it works.[39m
[38;5;252mx[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;214m'[39m[38;5;214mapple[39m[38;5;214m'[39m
[38;5;252my[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;214m'[39m[38;5;214mlemon[39m[38;5;214m'[39m
[38;5;252mz[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;214m"[39m[38;5;214mThe items in the basket are [39m[38;5;214m%s[39m[38;5;214m and [39m[38;5;214m%s[39m[38;5;214m"[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;252m [39m[38;5;252my[39m[38;5;252m)[39m

[38;5;246m# A newer way to format strings is the format method.[39m
[38;5;246m# This method is the preferred way[39m
[38;5;214m"[39m[38;5;214m{} is a {}[39m[38;5;214m"[39m[38;5;252m.[39m[38;5;252mformat[39m[38;5;252m([39m[38;5;214m"[39m[38;5;214mThis[39m[38;5;214m"[39m[38;5;252m,[39m[38;5;252m [39m[38;5;214m"[39m[38;5;214mplaceholder[39m[38;5;214m"[39m[38;5;252m)[39m
[38;5;214m"[39m[38;5;214m{0} can be {1}[39m[38;5;214m"[39m[38;5;252m.[39m[38;5;252mformat[39m[38;5;252m([39m[38;5;214m"[39m[38;5;214mstrings[39m[38;5;214m"[39m[38;5;252m,[39m[38;5;252m [39m[38;5;214m"[39m[38;5;214mformatted[39m[38;5;214m"[39m[38;5;252m)[39m
[38;5;246m# You can use keywords if you don't want to count.[39m
[38;5;214m"[39m[38;5;214m{name} wants to eat {food}[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;214m"[39m[38;5;214mBob[39m[38;5;214m"[39m[38;5;252m,[39m[38;5;252m [39m[38;5;252mfood[39m[38;5;252m=[39m[38;5;214m"[39m[38;5;214mlasagna[39m[38;5;214m"[39m[38;5;252m)[39m

[38;5;246m# None is an object[39m
[38;5;31mNone[39m[38;5;252m  [39m[38;5;246m# => None[39m

[38;5;246m# Don't use the equality "==" symbol to compare objects to None[39m
[38;5;246m# Use "is" instead[39m
[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;31mNone[39m[38;5;252m  [39m[38;5;246m# => False[39m
[38;5;31mNone[39m[38;5;252m [39m[38;5;70;01mis[39;00m[38;5;252m [39m[38;5;31mNone[39m[38;5;252m  [39m[38;5;246m# => True[39m

[38;5;246m# The 'is' operator tests for object identity. This isn't[39m
[38;5;246m# very useful when dealing with primitive values, but is[39m
[38;5;246m# very useful when dealing with objects.[39m

[38;5;246m# Any object can be used in a Boolean context.[39m
[38;5;246m# The following values are considered falsey:[39m
[38;5;246m#    - None[39m
[38;5;246m#    - zero of any numeric type (e.g., 0, 0L, 0.0, 0j)[39m
[38;5;246m#    - empty sequences (e.g., '', (), [])[39m
[38;5;246m#    - empty containers (e.g., {}, set())[39m
[38;5;246m#    - instances of user-defined classes meeting certain conditions[39m
[38;5;246m#      see: https://docs.python.org/2/reference/datamodel.html#object.__nonzero__[39m
[38;5;246m#[39m
[38;5;246m# All other values are truthy (using the bool() function on them returns True).[39m
[38;5;31mbool[39m[38;5;252m([39m[38;5;67m0[39m[38;5;252m)[39m[38;5;252m  [39m[38;5;246m# => False[39m
[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;246m# => False[39m


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

[38;5;246m# Python has a print statement[39m
[38;5;70;01mprint[39;00m[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;246m# => I'm Python. Nice to meet you![39m

[38;5;246m# Simple way to get input data from console[39m
[38;5;252minput_string_var[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;31mraw_input[39m[38;5;252m([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;246m# Returns the data as a string[39m
[38;5;252minput_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;246m# Evaluates the data as python code[39m
[38;5;246m# Warning: Caution is recommended for input() method usage[39m
[38;5;246m# Note: In python 3, input() is deprecated and raw_input() is renamed to input()[39m

[38;5;246m# No need to declare variables before assigning to them.[39m
[38;5;252msome_var[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;67m5[39m[38;5;252m  [39m[38;5;246m# Convention is to use lower_case_with_underscores[39m
[38;5;252msome_var[39m[38;5;252m  [39m[38;5;246m# => 5[39m

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

[38;5;246m# if can be used as an expression[39m
[38;5;246m# Equivalent of C's '?:' ternary operator[39m
[38;5;214m"[39m[38;5;214myahoo![39m[38;5;214m"[39m[38;5;252m [39m[38;5;70;01mif[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;70;01melse[39;00m[38;5;252m [39m[38;5;67m2[39m[38;5;252m  [39m[38;5;246m# => "yahoo!"[39m

[38;5;246m# Lists store sequences[39m
[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;246m# You can start with a prefilled list[39m
[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;246m# Add stuff to the end of a list with append[39m
[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;246m# li is now [1][39m
[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;246m# li is now [1, 2][39m
[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;246m# li is now [1, 2, 4][39m
[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;246m# li is now [1, 2, 4, 3][39m
[38;5;246m# Remove from the end with pop[39m
[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;246m# => 3 and li is now [1, 2, 4][39m
[38;5;246m# Let's put it back[39m
[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;246m# li is now [1, 2, 4, 3] again.[39m

[38;5;246m# Access a list like you would any array[39m
[38;5;252mli[39m[38;5;252m[[39m[38;5;67m0[39m[38;5;252m][39m[38;5;252m  [39m[38;5;246m# => 1[39m
[38;5;246m# Assign new values to indexes that have already been initialized with =[39m
[38;5;252mli[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;67m42[39m
[38;5;252mli[39m[38;5;252m[[39m[38;5;67m0[39m[38;5;252m][39m[38;5;252m  [39m[38;5;246m# => 42[39m
[38;5;252mli[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;67m1[39m[38;5;252m  [39m[38;5;246m# Note: setting it back to the original value[39m
[38;5;246m# Look at the last element[39m
[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;246m# => 3[39m

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

[38;5;246m# You can look at ranges with slice syntax.[39m
[38;5;246m# (It's a closed/open range for you mathy types.)[39m
[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;246m# => [2, 4][39m
[38;5;246m# Omit the beginning[39m
[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;246m# => [4, 3][39m
[38;5;246m# Omit the end[39m
[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;246m# => [1, 2, 4][39m
[38;5;246m# Select every second entry[39m
[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;246m# =>[1, 4][39m
[38;5;246m# Reverse a copy of the list[39m
[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;246m# => [3, 4, 2, 1][39m
[38;5;246m# Use any combination of these to make advanced slices[39m
[38;5;246m# li[start:end:step][39m

[38;5;246m# Remove arbitrary elements from a list with "del"[39m
[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;246m# li is now [1, 2, 3][39m

[38;5;246m# You can add lists[39m
[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;246m# => [1, 2, 3, 4, 5, 6][39m
[38;5;246m# Note: values for li and for other_li are not modified.[39m

[38;5;246m# Concatenate lists with "extend()"[39m
[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;246m# Now li is [1, 2, 3, 4, 5, 6][39m

[38;5;246m# Remove first occurrence of a value[39m
[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;246m# li is now [1, 3, 4, 5, 6][39m
[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;246m# Raises a ValueError as 2 is not in the list[39m

[38;5;246m# Insert an element at a specific index[39m
[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;246m# li is now [1, 2, 3, 4, 5, 6] again[39m

[38;5;246m# Get the index of the first item found[39m
[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;246m# => 1[39m
[38;5;252mli[39m[38;5;252m.[39m[38;5;252mindex[39m[38;5;252m([39m[38;5;67m7[39m[38;5;252m)[39m[38;5;252m  [39m[38;5;246m# Raises a ValueError as 7 is not in the list[39m

[38;5;246m# Check for existence in a list with "in"[39m
[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;246m# => True[39m

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

[38;5;246m# Tuples are like lists but are immutable.[39m
[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;246m# => 1[39m
[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;246m# Raises a TypeError[39m

[38;5;246m# You can do all those list thingies on tuples too[39m
[38;5;31mlen[39m[38;5;252m([39m[38;5;252mtup[39m[38;5;252m)[39m[38;5;252m  [39m[38;5;246m# => 3[39m
[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;246m# => (1, 2, 3, 4, 5, 6)[39m
[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;246m# => (1, 2)[39m
[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;246m# => True[39m

[38;5;246m# You can unpack tuples (or lists) into variables[39m
[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;246m# a is now 1, b is now 2 and c is now 3[39m
[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;246m# you can leave out the parentheses[39m
[38;5;246m# Tuples are created by default if you leave out the parentheses[39m
[38;5;252mg[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;246m# => (4, 5, 6)[39m
[38;5;246m# Now look how easy it is to swap two values[39m
[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;246m# d is now 5 and e is now 4[39m

[38;5;246m# Dictionaries store mappings[39m
[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;246m# Here is a prefilled dictionary[39m
[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;246m# Look up values with [][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;246m# => 1[39m

[38;5;246m# Get all keys as a list with "keys()"[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;246m# => ["three", "two", "one"][39m
[38;5;246m# Note - Dictionary key ordering is not guaranteed.[39m
[38;5;246m# Your results might not match this exactly.[39m

[38;5;246m# Get all values as a list with "values()"[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;246m# => [3, 2, 1][39m
[38;5;246m# Note - Same as above regarding key ordering.[39m

[38;5;246m# Get all key-value pairs as a list of tuples with "items()"[39m
[38;5;252mfilled_dict[39m[38;5;252m.[39m[38;5;252mitems[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m  [39m[38;5;246m# => [("one", 1), ("two", 2), ("three", 3)][39m

[38;5;246m# Check for existence of keys in a dictionary with "in"[39m
[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;246m# => True[39m
[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;246m# => False[39m

[38;5;246m# Looking up a non-existing key is a KeyError[39m
[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;246m# KeyError[39m

[38;5;246m# Use "get()" method to avoid the KeyError[39m
[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;246m# => 1[39m
[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;246m# => None[39m
[38;5;246m# The get method supports a default argument when the value is missing[39m
[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;246m# => 1[39m
[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;246m# => 4[39m
[38;5;246m# note that filled_dict.get("four") is still => None[39m
[38;5;246m# (get doesn't set the value in the dictionary)[39m

[38;5;246m# set the value of a key with a syntax similar to lists[39m
[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;246m# now, filled_dict["four"] => 4[39m

[38;5;246m# "setdefault()" inserts into a dictionary only if the given key isn't present[39m
[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;246m# filled_dict["five"] is set to 5[39m
[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;246m# filled_dict["five"] is still 5[39m

[38;5;246m# Sets store ... well sets (which are like lists but can contain no duplicates)[39m
[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;246m# Initialize a "set()" with a bunch of values[39m
[38;5;252msome_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;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;252m  [39m[38;5;246m# some_set is now set([1, 2, 3, 4])[39m

[38;5;246m# order is not guaranteed, even though it may sometimes look sorted[39m
[38;5;252manother_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;67m4[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m3[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;252m  [39m[38;5;246m# another_set is now set([1, 2, 3, 4])[39m

[38;5;246m# Since Python 2.7, {} can be used to declare a set[39m
[38;5;252mfilled_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;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;246m# => {1, 2, 3, 4}[39m

[38;5;246m# Add more items to a 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;246m# filled_set is now {1, 2, 3, 4, 5}[39m

[38;5;246m# Do set intersection with &[39m
[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;246m# => {3, 4, 5}[39m

[38;5;246m# Do set union with |[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;246m# => {1, 2, 3, 4, 5, 6}[39m

[38;5;246m# Do set difference with -[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;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;246m# => {1, 4}[39m

[38;5;246m# Do set symmetric difference with ^[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;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;246m# => {1, 4, 5}[39m

[38;5;246m# Check if set on the left is a superset of set on the right[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;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;246m# => False[39m

[38;5;246m# Check if set on the left is a subset of set on the right[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;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;246m# => True[39m

[38;5;246m# Check for existence in a set with in[39m
[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;246m# => True[39m
[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;246m# => False[39m
[38;5;67m10[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;252mfilled_set[39m[38;5;252m [39m[38;5;246m# => True[39m

[38;5;246m# Check data type of variable[39m
[38;5;31mtype[39m[38;5;252m([39m[38;5;252mli[39m[38;5;252m)[39m[38;5;252m   [39m[38;5;246m# => list[39m
[38;5;31mtype[39m[38;5;252m([39m[38;5;252mfilled_dict[39m[38;5;252m)[39m[38;5;252m   [39m[38;5;246m# => dict[39m
[38;5;31mtype[39m[38;5;252m([39m[38;5;67m5[39m[38;5;252m)[39m[38;5;252m   [39m[38;5;246m# => int[39m


[38;5;246m####################################################[39m
[38;5;246m#  3. Control Flow[39m
[38;5;246m####################################################[39m

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

[38;5;246m# Here is an if statement. Indentation is significant in python![39m
[38;5;246m# prints "some_var is smaller than 10"[39m
[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;70;01mprint[39;00m[38;5;252m [39m[38;5;214m"[39m[38;5;214msome_var is totally bigger than 10.[39m[38;5;214m"[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;246m# This elif clause is optional.[39m
[38;5;252m    [39m[38;5;70;01mprint[39;00m[38;5;252m [39m[38;5;214m"[39m[38;5;214msome_var is smaller than 10.[39m[38;5;214m"[39m
[38;5;70;01melse[39;00m[38;5;252m:[39m[38;5;252m  [39m[38;5;246m# This is optional too.[39m
[38;5;252m    [39m[38;5;70;01mprint[39;00m[38;5;252m [39m[38;5;214m"[39m[38;5;214msome_var is indeed 10.[39m[38;5;214m"[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;246m# You can use {0} to interpolate formatted strings. (See above.)[39m
[38;5;252m    [39m[38;5;70;01mprint[39;00m[38;5;252m [39m[38;5;214m"[39m[38;5;214m{0} 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;214m"""[39m
[38;5;214m"range(number)" returns a list 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;70;01mprint[39;00m[38;5;252m [39m[38;5;252mi[39m

[38;5;214m"""[39m
[38;5;214m"range(lower, upper)" returns a list 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;70;01mprint[39;00m[38;5;252m [39m[38;5;252mi[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;70;01mprint[39;00m[38;5;252m [39m[38;5;252mx[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;246m# Shorthand for x = x + 1[39m

[38;5;246m# Handle exceptions with a try/except block[39m

[38;5;246m# Works on Python 2.6 and up:[39m
[38;5;70;01mtry[39;00m[38;5;252m:[39m
[38;5;252m    [39m[38;5;246m# Use "raise" to raise an error[39m
[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;246m# Pass is just a no-op. Usually you would do recovery here.[39m
[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;246m# Multiple exceptions can be handled together, if required.[39m
[38;5;70;01melse[39;00m[38;5;252m:[39m[38;5;252m  [39m[38;5;246m# Optional clause to the try/except block. Must follow all except blocks[39m
[38;5;252m    [39m[38;5;70;01mprint[39;00m[38;5;252m [39m[38;5;214m"[39m[38;5;214mAll good![39m[38;5;214m"[39m[38;5;252m  [39m[38;5;246m# Runs only if the code in try raises no exceptions[39m
[38;5;70;01mfinally[39;00m[38;5;252m:[39m[38;5;252m  [39m[38;5;246m# Execute under all circumstances[39m
[38;5;252m    [39m[38;5;70;01mprint[39;00m[38;5;252m [39m[38;5;214m"[39m[38;5;214mWe can clean up resources here[39m[38;5;214m"[39m

[38;5;246m# Instead of try/finally to cleanup resources you can use a with statement[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;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;70;01mprint[39;00m[38;5;252m [39m[38;5;252mline[39m


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

[38;5;246m# Use "def" to create new functions[39m
[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;70;01mprint[39;00m[38;5;252m [39m[38;5;214m"[39m[38;5;214mx is {0} and y is {1}[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;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;246m# Return values with a return statement[39m


[38;5;246m# Calling functions with parameters[39m
[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;246m# => prints out "x is 5 and y is 6" and returns 11[39m

[38;5;246m# Another way to call functions is with keyword arguments[39m
[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;246m# Keyword arguments can arrive in any order.[39m


[38;5;246m# You can define functions that take a variable number of[39m
[38;5;246m# positional args, which will be interpreted as a tuple by using *[39m
[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;246m# => (1, 2, 3)[39m


[38;5;246m# You can define functions that take a variable number of[39m
[38;5;246m# keyword args, as well, which will be interpreted as a dict by using **[39m
[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;246m# Let's call it to see what happens[39m
[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;246m# => {"big": "foot", "loch": "ness"}[39m


[38;5;246m# You can do both at once, if you like[39m
[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;70;01mprint[39;00m[38;5;252m [39m[38;5;252margs[39m
[38;5;252m    [39m[38;5;70;01mprint[39;00m[38;5;252m [39m[38;5;252mkwargs[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;246m# When calling functions, you can do the opposite of args/kwargs![39m
[38;5;246m# Use * to expand positional args and use ** to expand keyword args.[39m
[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;246m# equivalent to all_the_args(1, 2, 3, 4)[39m
[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;246m# equivalent to all_the_args(a=3, b=4)[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;252m*[39m[38;5;252m*[39m[38;5;252mkwargs[39m[38;5;252m)[39m[38;5;252m  [39m[38;5;246m# equivalent to all_the_args(1, 2, 3, 4, a=3, b=4)[39m


[38;5;246m# you can pass args and kwargs along to other functions that take args/kwargs[39m
[38;5;246m# by expanding them with * and ** respectively[39m
[38;5;70;01mdef[39;00m[38;5;252m [39m[38;5;68mpass_all_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;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;70;01mprint[39;00m[38;5;252m [39m[38;5;252mvarargs[39m[38;5;252m([39m[38;5;252m*[39m[38;5;252margs[39m[38;5;252m)[39m
[38;5;252m    [39m[38;5;70;01mprint[39;00m[38;5;252m [39m[38;5;252mkeyword_args[39m[38;5;252m([39m[38;5;252m*[39m[38;5;252m*[39m[38;5;252mkwargs[39m[38;5;252m)[39m


[38;5;246m# Function Scope[39m
[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;246m# Local var x not the same as global variable x[39m
[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;246m# => 43[39m
[38;5;252m    [39m[38;5;70;01mprint[39;00m[38;5;252m [39m[38;5;252mx[39m[38;5;252m  [39m[38;5;246m# => 43[39m


[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;70;01mprint[39;00m[38;5;252m [39m[38;5;252mx[39m[38;5;252m  [39m[38;5;246m# => 5[39m
[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;246m# global var x is now set to 6[39m
[38;5;252m    [39m[38;5;70;01mprint[39;00m[38;5;252m [39m[38;5;252mx[39m[38;5;252m  [39m[38;5;246m# => 6[39m


[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;246m# Python has first class functions[39m
[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;246m# => 13[39m

[38;5;246m# There are also anonymous functions[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;67m2[39m[38;5;252m)[39m[38;5;252m([39m[38;5;67m3[39m[38;5;252m)[39m[38;5;252m  [39m[38;5;246m# => True[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;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;246m# => 5[39m

[38;5;246m# There are built-in higher order functions[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;246m# => [11, 12, 13][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;246m# => [4, 2, 3][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;246m# => [6, 7][39m

[38;5;246m# We can use list comprehensions for nice maps and filters[39m
[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;246m# => [11, 12, 13][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;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;246m# => [6, 7][39m

[38;5;246m# You can construct set and dict comprehensions as well.[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;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;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;246m# => {'a', 'b', 'c'}[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;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;246m# => {0: 0, 1: 1, 2: 4, 3: 9, 4: 16}[39m


[38;5;246m####################################################[39m
[38;5;246m# 5. Classes[39m
[38;5;246m####################################################[39m

[38;5;246m# We subclass from object to get a class.[39m
[38;5;70;01mclass[39;00m[38;5;252m [39m[38;5;68;04mHuman[39;00m[38;5;252m([39m[38;5;31mobject[39m[38;5;252m)[39m[38;5;252m:[39m
[38;5;252m    [39m[38;5;246m# A class attribute. It is shared by all instances of this class[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. sapiens[39m[38;5;214m"[39m

[38;5;252m    [39m[38;5;246m# Basic initializer, this is called when this class is instantiated.[39m
[38;5;252m    [39m[38;5;246m# Note that the double leading and trailing underscores denote objects[39m
[38;5;252m    [39m[38;5;246m# or attributes that are used by python but that live in user-controlled[39m
[38;5;252m    [39m[38;5;246m# namespaces. You should not invent such names on your own.[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;252mname[39m[38;5;252m)[39m[38;5;252m:[39m
[38;5;252m        [39m[38;5;246m# Assign the argument to the instance's name attribute[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;252m [39m[38;5;252mname[39m

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

[38;5;252m    [39m[38;5;246m# An instance method. All methods take "self" as the first argument[39m
[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;70;01mreturn[39;00m[38;5;252m [39m[38;5;214m"[39m[38;5;214m{0}: {1}[39m[38;5;214m"[39m[38;5;252m.[39m[38;5;252mformat[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;252mmsg[39m[38;5;252m)[39m

[38;5;252m    [39m[38;5;246m# A class method is shared among all instances[39m
[38;5;252m    [39m[38;5;246m# They are called with the calling class as the first argument[39m
[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;246m# A static method is called without a class or instance reference[39m
[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;246m# A property is just like a getter.[39m
[38;5;252m    [39m[38;5;246m# It turns the method age() into an read-only attribute[39m
[38;5;252m    [39m[38;5;246m# of the same name.[39m
[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;246m# This allows the property to be set[39m
[38;5;252m    [39m[38;5;214m@age.setter[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;246m# This allows the property to be deleted[39m
[38;5;252m    [39m[38;5;214m@age.deleter[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;246m# Instantiate a class[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;70;01mprint[39;00m[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;246m# prints out "Ian: hi"[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;70;01mprint[39;00m[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;246m# prints out "Joel: hello"[39m

[38;5;246m# Call our class method[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;246m# => "H. sapiens"[39m

[38;5;246m# Change the shared attribute[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;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;246m# => "H. neanderthalensis"[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;246m# => "H. neanderthalensis"[39m

[38;5;246m# Call the static method[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;246m# => "*grunt*"[39m

[38;5;246m# Update the property[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;246m# Get the property[39m
[38;5;252mi[39m[38;5;252m.[39m[38;5;252mage[39m[38;5;252m  [39m[38;5;246m# => 42[39m

[38;5;246m# Delete the property[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;252mi[39m[38;5;252m.[39m[38;5;252mage[39m[38;5;252m  [39m[38;5;246m# => raises an AttributeError[39m

[38;5;246m####################################################[39m
[38;5;246m# 6. Modules[39m
[38;5;246m####################################################[39m

[38;5;246m# You can import modules[39m
[38;5;70;01mimport[39;00m[38;5;252m [39m[38;5;68;04mmath[39;00m

[38;5;70;01mprint[39;00m[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;246m# => 4[39m

[38;5;246m# You can get specific functions from a module[39m
[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;70;01mprint[39;00m[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;246m# => 4.0[39m
[38;5;70;01mprint[39;00m[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;246m# => 3.0[39m

[38;5;246m# You can import all functions from a module.[39m
[38;5;246m# Warning: this is not recommended[39m
[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;246m# You can shorten module names[39m
[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;246m# => True[39m
[38;5;246m# you can also test that the functions are equivalent[39m
[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;252msqrt[39m

[38;5;252mmath[39m[38;5;252m.[39m[38;5;252msqrt[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;252m==[39m[38;5;252m [39m[38;5;252msqrt[39m[38;5;252m  [39m[38;5;246m# => True[39m

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

[38;5;246m# You can find out which functions and attributes[39m
[38;5;246m# defines a module.[39m
[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;246m# If you have a Python script named math.py in the same[39m
[38;5;246m# folder as your current script, the file math.py will[39m
[38;5;246m# be loaded instead of the built-in Python module.[39m
[38;5;246m# This happens because the local folder has priority[39m
[38;5;246m# over Python's built-in libraries.[39m


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

[38;5;246m# Generators[39m
[38;5;246m# A generator "generates" values as they are requested instead of storing[39m
[38;5;246m# everything up front[39m

[38;5;246m# The following method (*NOT* a generator) will double all values and store it[39m
[38;5;246m# in `double_arr`. For large size of iterables, that might get huge![39m
[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;252mdouble_arr[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;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;252mdouble_arr[39m[38;5;252m.[39m[38;5;252mappend[39m[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;252m)[39m
[38;5;252m    [39m[38;5;70;01mreturn[39;00m[38;5;252m [39m[38;5;252mdouble_arr[39m


[38;5;246m# Running the following would mean we'll double all values first and return all[39m
[38;5;246m# of them back to be checked by our condition[39m
[38;5;70;01mfor[39;00m[38;5;252m [39m[38;5;252mvalue[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;67m1000000[39m[38;5;252m)[39m[38;5;252m)[39m[38;5;252m:[39m[38;5;252m  [39m[38;5;246m# `test_non_generator`[39m
[38;5;252m    [39m[38;5;70;01mprint[39;00m[38;5;252m [39m[38;5;252mvalue[39m
[38;5;252m    [39m[38;5;70;01mif[39;00m[38;5;252m [39m[38;5;252mvalue[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;70;01mbreak[39;00m


[38;5;246m# We could instead use a generator to "generate" the doubled value as the item[39m
[38;5;246m# is being requested[39m
[38;5;70;01mdef[39;00m[38;5;252m [39m[38;5;68mdouble_numbers_generator[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;246m# Running the same code as before, but with a generator, now allows us to iterate[39m
[38;5;246m# over the values and doubling them one by one as they are being consumed by[39m
[38;5;246m# our logic. Hence as soon as we see a value > 5, we break out of the[39m
[38;5;246m# loop and don't need to double most of the values sent in (MUCH FASTER!)[39m
[38;5;70;01mfor[39;00m[38;5;252m [39m[38;5;252mvalue[39m[38;5;252m [39m[38;5;70;01min[39;00m[38;5;252m [39m[38;5;252mdouble_numbers_generator[39m[38;5;252m([39m[38;5;31mxrange[39m[38;5;252m([39m[38;5;67m1000000[39m[38;5;252m)[39m[38;5;252m)[39m[38;5;252m:[39m[38;5;252m  [39m[38;5;246m# `test_generator`[39m
[38;5;252m    [39m[38;5;70;01mprint[39;00m[38;5;252m [39m[38;5;252mvalue[39m
[38;5;252m    [39m[38;5;70;01mif[39;00m[38;5;252m [39m[38;5;252mvalue[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;70;01mbreak[39;00m

[38;5;246m# BTW: did you notice the use of `range` in `test_non_generator` and `xrange` in `test_generator`?[39m
[38;5;246m# Just as `double_numbers_generator` is the generator version of `double_numbers`[39m
[38;5;246m# We have `xrange` as the generator version of `range`[39m
[38;5;246m# `range` would return back and array with 1000000 values for us to use[39m
[38;5;246m# `xrange` would generate 1000000 values for us as we request / iterate over those items[39m

[38;5;246m# Just as you can create a list comprehension, you can create generator[39m
[38;5;246m# comprehensions as well.[39m
[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;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;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;70;01mprint[39;00m[38;5;252m([39m[38;5;252mx[39m[38;5;252m)[39m[38;5;252m  [39m[38;5;246m# prints -1 -2 -3 -4 -5 to console/terminal[39m

[38;5;246m# You can also cast a generator comprehension directly to a list.[39m
[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;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;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;70;01mprint[39;00m[38;5;252m([39m[38;5;252mgen_to_list[39m[38;5;252m)[39m[38;5;252m  [39m[38;5;246m# => [-1, -2, -3, -4, -5][39m

[38;5;246m# Decorators[39m
[38;5;246m# A decorator is a higher order function, which accepts and returns a function.[39m
[38;5;246m# Simple usage example – add_apples decorator will add 'Apple' element into[39m
[38;5;246m# fruits list returned by get_fruits target function.[39m
[38;5;70;01mdef[39;00m[38;5;252m [39m[38;5;68madd_apples[39m[38;5;252m([39m[38;5;252mfunc[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;68mget_fruits[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m:[39m
[38;5;252m        [39m[38;5;252mfruits[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252mfunc[39m[38;5;252m([39m[38;5;252m)[39m
[38;5;252m        [39m[38;5;252mfruits[39m[38;5;252m.[39m[38;5;252mappend[39m[38;5;252m([39m[38;5;214m'[39m[38;5;214mApple[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;252mfruits[39m
[38;5;252m    [39m[38;5;70;01mreturn[39;00m[38;5;252m [39m[38;5;252mget_fruits[39m

[38;5;214m@add_apples[39m
[38;5;70;01mdef[39;00m[38;5;252m [39m[38;5;68mget_fruits[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;252m[[39m[38;5;214m'[39m[38;5;214mBanana[39m[38;5;214m'[39m[38;5;252m,[39m[38;5;252m [39m[38;5;214m'[39m[38;5;214mMango[39m[38;5;214m'[39m[38;5;252m,[39m[38;5;252m [39m[38;5;214m'[39m[38;5;214mOrange[39m[38;5;214m'[39m[38;5;252m][39m

[38;5;246m# Prints out the list of fruits with 'Apple' element in it:[39m
[38;5;246m# Banana, Mango, Orange, Apple[39m
[38;5;70;01mprint[39;00m[38;5;252m [39m[38;5;214m'[39m[38;5;214m, [39m[38;5;214m'[39m[38;5;252m.[39m[38;5;252mjoin[39m[38;5;252m([39m[38;5;252mget_fruits[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m)[39m

[38;5;246m# in this example beg wraps say[39m
[38;5;246m# Beg will call say. If say_please is True then it will change the returned[39m
[38;5;246m# message[39m
[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;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;31mFalse[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;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;70;01mprint[39;00m[38;5;252m [39m[38;5;252msay[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m  [39m[38;5;246m# Can you buy me a beer?[39m
[38;5;70;01mprint[39;00m[38;5;252m [39m[38;5;252msay[39m[38;5;252m([39m[38;5;252msay_please[39m[38;5;252m=[39m[38;5;31mTrue[39m[38;5;252m)[39m[38;5;252m  [39m[38;5;246m# Can you buy me a beer? Please! I am poor :([39m
