<jupyter_start><jupyter_text>Python 101 **Prepared By:**```<NAME>Email: <EMAIL>``` References- [0] [Preliminary Reading](https://docs.google.com/document/d/1jMcvpPM5a2NV-fWrqwHgdjxeB2UUSkDdvpWSUhGhI7s/edit?usp=sharing)- [1] [Python Overview](http://python-history.blogspot.com/2009/01/introduction-and-overview.html)- [2] [Python for Data Science](https://data36.com/python-for-data-science-python-basics-1/)- [3] [Mutable and Immutable Data Structures](https://medium.com/@meghamohan/mutable-and-immutable-side-of-python-c2145cf72747)- [4] [Official Python Documentation](https://docs.python.org/3/tutorial/index.html)- [CS231N course python tutorial](http://cs231n.github.io/python-numpy-tutorial/python) Introduction to Python**"Today, Python is used for everything from throw-away scripts to large scalable web servers that provide uninterrupted service 24x7. It is used for GUI and database programming, client- and server-side web programming, and application testing. It is used by scientists writing applications for the world's fastest supercomputers and by children first learning to program."** [1]~ *<NAME> Rossum, Creator of Python* `Python is a programming language that lets you work quickly and integrate systems more effectively.``Python is a general purpose programming language and it’s not only for Data Science. This means, that you don’t have to learn every part of it to be a great data scientist. ``Python is a high-level language. It means that in terms of CPU-time it’s not the most effective language on the planet. But on the other hand it was made to be simple, “user-friendly” and easy to interpret. thus what you might lose on CPU-time, you might win back on engineering time.` - Python has been there since 1991, active open-source community.- Python is fairly easy to interpret and learn.- Python handles different data structures very well.- Python has very powerful statistical and data visualization libraries eg. numpy, scipy, pandas, etc.- Python has many package as suitable for simpler Analytics projects (eg. simple statistical analysis, exploratory data analysis, etc.) as advanced Data Science projects (eg. building machine learning models)- Python has a simple but effective approach to object-oriented programming. - Python has elegant syntax, dynamic typing and is interpreted nature.- Interactive: Read, Evaluate, Print, Loop (REPL)- Ideal language for scripting and rapid application development in many areas on most platforms.`The Python interpreter and the extensive standard library are freely available in source or binary form for all major platforms and may be freely distributed. `<jupyter_code>import sys
help(sys)
help(help)<jupyter_output><empty_output><jupyter_text>Python vs C, Java, etc.  Python is elegant to write (has brace-less syntax ;)) C```if (a < b) {    max = b;} else {    max = a;}``` Python```if a < b:    max = belse:    max = a```  Python is a dynamically typed languageIn C, variables must always be explicitly declared (eg. specify type such as int or double) which is then used to perform static compile-time checks of the program as well as for allocating memory locations used for storing the variable’s value. In Python, variables are simply names that refer to objects. Variables do not need to be declared before they are assigned and they can even change type in the middle of a program. [1]<jupyter_code>help(id)
id(2)
# Everything is object in Python
x = 2
print (x)
print (id (x))
type(x)
x = "AI is cool"
print (x)
print(id (x))
type (x)
y = 2
id(y)
# Functions in python is also object
def hey_function():
    '''I am a doc string of hey_function(). I am a value of an attribute of a function object'''
    print ("Printing from hey_function()!")
# function call - calls an Callable Object in Python
hey_function()
hey_function
type(hey_function)
# attribute of a function object
hey_function.__doc__<jupyter_output><empty_output><jupyter_text>Mutable and Immutable Object types [3]**Mutable objects:**list, dict, set**Immutable objects**int, float, complex, string, tuple, frozen set [note: immutable version of set], bytes Immutable Example 1<jupyter_code>id()"ashish")
x = "ashish"
y = "ashish"
type(x)
id(x)
id(y)
x == y
x is y # or id(x) == id(y)<jupyter_output><empty_output><jupyter_text>Immutable Example 2<jupyter_code>id(2)
# We are creating a single integer object type 2
# identifier A is tagged to the integer object
# another B identifier is tagged to the same integer object via A
A = 2
B = 2
type(A)
help(id)
id(A)
id(B)
A == B # Do A and B have same values?
A is B # or id(A) == id(B) # Are the A and B the same object?
id(B) is id(A) # Are identity of object A and identiy of object B the same object?
isinstance(A, int)
id(2.0)<jupyter_output><empty_output><jupyter_text>Mutable Objects Example 1<jupyter_code>A_list = list([1, 2, 3])
B_list = A_list

# We are creating a single object type (list([1,2,3]))
# identifier A_list is tagged to the list object
# another B_list identifier is tagged to the same object via A_list
id(A_list) == id(B_list)
A_list is B_list
A_list.pop()
A_list
id(A_list) == id(B_list)
A_list is B_list<jupyter_output><empty_output><jupyter_text>Python Implementation- Python is typically implemented using a combination of a bytecode compiler and interpreter. - Compilation is implicitly performed as modules are loaded, and several language primitives require the compiler to be available at run-time.  Other implementations- CPython - de-facto Python implementation and widely adopted- PyPy - JIT compilation (optimizing Python compiler/interpreter written in Python) - Jython (seamless Java integration)- IronPython - IronPython is an implementation of the Python programming language targeting the .NET Framework and Mono.- Stackless Python (variant of the C implementation - reduces reliance on the C stack for function/method calls, to allow co-routines, continuations, and microthreads) Cpython vs Cython`CPython is a Python Interpreter written in Python. Cython is a C extension of Python. Cython is really a different programming language, and is a superset of both C and Python`<jupyter_code>import platform
def show_platform_detail():
    print ("Python Implementation: %s" % platform.python_implementation())
    print ("Machine: %s" % platform.machine())
    print ("Uname: ", platform.uname())
    print ("Architecture:", platform.architecture())
    print ("System: %s" % platform.system())
    print ("Node: %s" % platform.node())
    print ("Release: %s" % platform.release())
    print ("Version: %s" % platform.version())
    print ("Processor: %s" % platform.processor())
show_platform_detail()<jupyter_output><empty_output><jupyter_text>Built-in functions- The Python interpreter has a number of functions and types built into it that are always available.abs()dict(), help(), min(), id(), object(), sorted()enumerate(), bin(), eval(), int(), open(), str()bool(), isinstance(), sum()bytearray(), issubclass(), pow(), super()bytes(), float(), iter(), print(), tuple()format(), len(), type()list(), range()zip()hasattr(), max(), round()	 set()<jupyter_code>sorted([1,6,3,6,7,2,3,5])
hasattr(str, "count")
for i in range(5):
    print (i)
sum([1,2])<jupyter_output><empty_output><jupyter_text>Python Installation  Python Interpreter$ which python` /usr/local/bin/python3.6 `  Argument Passing (Scripts)<jupyter_code>import sys
sys.argv[0]<jupyter_output><empty_output><jupyter_text>String [5]<jupyter_code>'spam eggs'  # single quotes
'doesn\'t'  # use \' to escape the single quote...
"doesn't"  # ...or use double quotes instead
'"Yes," he said.'
"\"Yes,\" he said."
'"Isn\'t," she said.'
print('C:\some\name')  # here \n means newline!
# raw string
print(r'C:\some\name')  # note the r before the quote
# unicode
print(u'C:\some\name')  # note the r before the quote
print("""\
Usage: thingy [OPTIONS]
     -h                        Display this usage message
     -H hostname               Hostname to connect to
""")
# Concatenation with + and *
3 * 'un' + 'ium'
'AI Developers' + ' Boise'
"AI Developers" + " Boise"<jupyter_output><empty_output><jupyter_text>- Strings can be indexed (subscripted), with the first character having index 0. There is no separate character type; a character is simply a string of size one<jupyter_code>word = 'Python'
print("word", word)
print("word[0]", word[0])
print("word[4]", word[4])
print("len(word)", len(word))
print("type(word)", type(word))
print("type(word[0])", type(word[0]))<jupyter_output><empty_output><jupyter_text>- Slicing and Indexing<jupyter_code>word[0:2]
# indexing from last
word[-1], word[-2]<jupyter_output><empty_output><jupyter_text>- Python strings cannot be changed — they are immutable. Therefore, assigning to an indexed position in the string results in an error.<jupyter_code>word[0]  = 'A'<jupyter_output><empty_output><jupyter_text>List [5]<jupyter_code># dynamic typed
ll = [2018, 5, 12, "AI Developers, Boise", "AI Saturdays"]
ll
type(ll[2])
type(ll[3])
# assigning to an indexed position of list is allowed
# - mutable data type
ll[3] = 12
print(ll)
print(id(ll))
new_sliced_list = ll[-3:] # slicing returns a new list
print(new_sliced_list)
print(id(new_sliced_list))
ll[:]
# list concatenation
ll1 = [3,4,5]
appended_list = ll1 + ll
appended_list
appended_list.append(15)
appended_list
# add the whole list to another list
list_to_be_extended = ["list", "to", "be", "extended"]
appended_list.extend(list_to_be_extended)
appended_list
id(appended_list)
# size of the list
len(appended_list)<jupyter_output><empty_output><jupyter_text>Simple Programming with Python<jupyter_code>import time<jupyter_output><empty_output><jupyter_text>Simple Function<jupyter_code>def display_execution_time(start, end, label):
    print("Execution time ({}): {}".format(label, end-start))
# Fibonacci series: 1
start = time.time()

a,b = 0, 1 # multiple assignment

while b < 10: # WHILE loop
    # print (b, end=", ") # replaces default /n by ", "
    print (b) # replaces default /n by ", "
    
    # expressions on the right-hand side are all evaluated
    # first before any of the assignments take place.
    a, b = b, a+b

    end = time.time()

display_execution_time(start, end, "fibonacci while")
# Fibonacci series: 2
def fib(n): # FUNCTION DEFINITION
    "display first n fibonacci series" 
    if n <= 2: # IF condition
        return 1 # RETURN statement
    res = fib(n-1) + fib(n-2) # RECURSIVE FUNCTION CALL
    return res # RETURN statement

start = time.time()
res_fib = fib(6) # FUNCTION call
print (res_fib)
end = time.time()
display_execution_time(start, end, "fibonacci recursion")<jupyter_output><empty_output><jupyter_text>Control Flow if-elif-else statements- There can be zero or more elif parts, and the else part is optional.- An if … elif … elif … sequence is a substitute for the switch or case statements found in other languages.<jupyter_code>def if_else_example(n):
    if type(n) is not int:
        return "not a number"
    if n < 0:
        return "less than zero"
    elif n == 0:
        return "equal to zero"
    else:
        return "greater than zero"
if_else_example("a")<jupyter_output><empty_output><jupyter_text>for loops<jupyter_code>words = ['cat', 'window', 'defenestrate']
for w in words:
    print (w, len(w))<jupyter_output><empty_output><jupyter_text>Use Case: If you need to manipulate a list and then add/remove elements in the same list<jupyter_code>words = ['cat', 'window', 'defenestrate']
for w in words[:]:  # Loop over a slice copy of the entire list.
    if len(w) > 6:
        words.insert(0, w)
words<jupyter_output><empty_output><jupyter_text>range() Function<jupyter_code>for i in range(5): # if only one argument, starts from 0, with interval of 1
    print (i, end =", ")
print()

for i in range(1,6): # starting is closed, ending is open
    print (i, end =", ")
print()

for i in range(1,11, 2): # specifying interval
    print (i, end =", ")
print()<jupyter_output><empty_output><jupyter_text>iterate over indices of list<jupyter_code>a = ['Mary', 'had', 'a', 'little', 'lamb']
for i in range(len(a)):
    print(i, a[i])<jupyter_output><empty_output><jupyter_text>or use enumerate<jupyter_code>for index, value in enumerate(a):
    print (index, value)<jupyter_output><empty_output><jupyter_text>or use zip and enumerate to loop multiple list<jupyter_code>b = [10,20,30,40,50]
for a_i, b_i in zip(a,b):
    print(a_i, b_i)<jupyter_output><empty_output><jupyter_text>Iterate in reverse<jupyter_code>for i in reversed(range(1, 10, 2)):
    print(i)<jupyter_output><empty_output><jupyter_text>iterate in sorted order<jupyter_code>list_unordered = [3,4,37,2,1,8,552,6]
for i in sorted(list_unordered):
    print(i)<jupyter_output><empty_output><jupyter_text>break, continue and pass statements<jupyter_code>for i in range(5):
    if i == 3:
        break
    print (i)

print()
for i in range(5):
    if i == 3:
        continue
    print(i)
    
# pass statement does nothing
# generally used when a statement is required syntactically but
# the program requires no action
i = 0
while i > 0:
    pass<jupyter_output><empty_output><jupyter_text>Defining Functions- function definition- default argument values- keyword arguments-<jupyter_code>def f(a, L=[]): # optional arguments are assigned once i.e L
    L.append(a)
    return L

print(f(1))
print(f(2))
print(f(3))<jupyter_output><empty_output><jupyter_text>Types of arguments<jupyter_code>def cheeseshop(kind, *arguments, **keywords):
    print("-- Do you have any", kind, "?")
    print("-- I'm sorry, we're all out of", kind)
    for arg in arguments:
        print(arg)
    print("-" * 40)
    for kw in keywords:
        print(kw, ":", keywords[kw])
cheeseshop("Limburger", "It's very runny, sir.",
           "It's really very, VERY runny, sir.",
           shopkeeper="<NAME>",
           client="<NAME>",
           sketch="Cheese Shop Sketch")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11831896551724137, 'fraction_numerical': 0.01257183908045977, 'mean_word_length': 4.901229334463756, 'pattern_counts': {'":': 1, '<': 119, '<?xml version=': 0, '>': 116, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '27', 'dir': 'jupyter-structured-clean-dedup', 'id': '452296', 'license': 'MIT', 'n_tokens_mistral': 4571, 'n_tokens_neox': 4357, 'n_words': 1566, 'path': 'Introduction to Python.ipynb', 'repo_name': 'aidevelopersboise/week1', 'size': '13955'}