<jupyter_start><jupyter_text>Lesson 01: Introduction to Python Part 1```~ Professor Chapkowski~ Copyright AJC 2020```  Lesson Topics- Data Types- Arithmetic Operators- Comparison Operators- Conditionals- Loops- IO (reading/writing files)- functions  Data Types  Variables- represents a value of a given `type` stored in fixed memory<jupyter_code>b = 2
print(b)
b = b * 2.0
print(b)<jupyter_output>2
4.0<jupyter_text>- b is an `integer` at first- after multiplication b is a `float` type  Data Types  Strings- a sequence of characters enclosed by a single (') or double quotes (")<jupyter_code>print("this is a string")
print('this is also a string')<jupyter_output>this is a string
this is also a string<jupyter_text>Data Types  String Operations- Rich string manipulation and management library- The `str` type operations can be found here: https://docs.python.org/3.6/library/stdtypes.html?highlight=upperstr.capitalize- String are *immutable*, meaning they cannot be modified with assignment statements<jupyter_code>s = "spam,eggs,bacon"
s.upper()
s.split(",")<jupyter_output><empty_output><jupyter_text>String Immutable Example<jupyter_code>s
s[0] = 'c'<jupyter_output><empty_output><jupyter_text>Data Types  Tuples- a sequence of arbitrary objects seperated by commas and enclosed by parantheses.- *Immutable* like strings<jupyter_code>t1 = (2,)
t2 = ('dog', 'cat', -14, 5.6, t1)
print(t2)
t2[0] = 'fish'<jupyter_output><empty_output><jupyter_text>A Note on Index Values- Python usings `0` based indexing. - The first item in a list is **0** not **1**Recall **t2**.  The fist item is **dog**, but if I put in `t2[1]`, the return value will be **cat**<jupyter_code>print("%s should be %s" % (t2[1], t2[0]))<jupyter_output>cat should be dog<jupyter_text>Data Types  Lists- a sequence of arbitrary objects seperated by commas and enclosed by brackets.- *Mutable*, meaning we can edit the object<jupyter_code>l = ['a', 'b', 1, 2.3, None]
l[0] = 'c'
l
l[1] = 'dog'
l<jupyter_output><empty_output><jupyter_text>Slicing Lists- Getting Sections of a list- Syntax is ```obj[ :  : ]```<jupyter_code>l[2:5]
# Using the Step Size
l[::2]<jupyter_output><empty_output><jupyter_text>Here we started at the first index, `0`, then said go to the end, but take every second item.More information on lists can be found here: https://docs.python.org/3.6/library/stdtypes.html?highlight=upperlists  Arithmetic Operators- Supports the common operations:|operation|description||| ------------- |:-------------:| -----:|| +      | Addition | || -      | Subtraction ||| * | Multiplication| || / | Division| || ** | Exponentiation| || % | Modular Division | |- Operations can extend beyond numbers<jupyter_code>s1 = 'Hello '
s2 = 'World'
s3 = s1 + s2
print(s3)
a = [1,2,3]
b = [4,5,6, 7, 8, -15.0]
c = a + b
print(c)<jupyter_output>[1, 2, 3, 4, 5, 6, 7, 8, -15.0]<jupyter_text>Augmented Assignment Operators- shorthand operations |Syntax| Explination||| ------------- |:-------------:| -----:|| `a += b`      | `a = a + b` | || `a -= b`      | `a = a - b` | || `a *= b`      | `a = a * b` | || `a /= b`      | `a = a / b` | || `a **= b`      | `a = a**b` | || `a %= b`      | `a = a % b` | |<jupyter_code>a = 10000
for i in range(5):
    a += i
    print(a)<jupyter_output>10000
10001
10003
10006
10010<jupyter_text>Comparison Operators- These operations return boolean values (True/False)| .| .| || ------------- |:-------------:| -----:|| < | Less than | || >      | Greater than ||| <= | Less than or equal to | || >= | Greater than or equal to| || == | Equal to| || != | Not equal to | |<jupyter_code>a = 2.99
b = 1.75
c = 'dog'
print(a>b)
b == c<jupyter_output><empty_output><jupyter_text>Conditionals- uses the `if` construct```   if condition:      'do some logic'   else:      'do something different'```<jupyter_code>def my_sign(a):
    if a < 0:
        return '-'
    elif a > 0:
        return '+'
    else:
        return '0'
print(my_sign(0))<jupyter_output>0<jupyter_text>Loops- Execute a block of code based on a condition or for a given number of times. While Loops```   while *condition*:      'do some code'```<jupyter_code>a = []
m = 0
n = 3
while m < n:
    a.append(m/3)
    m += 1
print(a)<jupyter_output>[0.0, 0.3333333333333333, 0.6666666666666666]<jupyter_text>For Loops- Loops over a given block of code based on a sequence```   for *var* in *sequence*:      'do some code'```<jupyter_code>b = 0
for i in range(10):
    print(i)
    b += i
b
a = ""
for i in ['a', 'dog', 'jumped']:
    a += i + " "
a<jupyter_output><empty_output><jupyter_text>IO (Reading/Writing Data) Opening and Closing Files- `file objects` allow for the reading/writing of data```      fo = open(, )```|.|.|.|| ------------- |:-------------:| -----:|| r      | read | || w      | write ||| a | append to end of file | || r+ | read to and write from an existing file | || w+ | same as `r+`, but filename is created if it doesn't exist | || a+ | same as `w+`, but data is append to the end of the file | |- `r` and `w` are the most common actions  IO (Reading/Writing Data) Reading Data from a File 3 Reading Methods1. ```fo.read(n)``` - read `n` number of characters2. ```fo.readline(n)``` reads `n` characters from the current line3. ```fo.readlines()``` reads all the lines in a file Extracting all lines one by one:```   with open(, 'r') as fo:        for line in fo:            print(line)```  IO (Reading/Writing Data) Writing Data from a File- ```fo.write()``` to write a single string- ```fo.writelines(['a','b','c'])``` to write a list of data to a file Writing to File<jupyter_code>fo = open('test.txt', 'w')
for k in range(100,111):
    fo.write("{:4d} {:6d}".format(k,k**2))
    fo.write("\n")
fo.close()<jupyter_output><empty_output><jupyter_text>Reading a file<jupyter_code>fo = open('test.txt', 'r').read()
fo<jupyter_output><empty_output><jupyter_text>Functions - is a defined collection of code normally to perform a single task or bit of logic**Syntax of a Function**```   def (, ,..., ):      """"""            return  (optional)```- A function has at least 3 parts:1. function name2. parameters3. return statement (if not written function returns `None`)- Function should always have documention strings that describe what is going on<jupyter_code>def first_method(a, b, c):
    """
    Takes an integer, string and float and returns a new string
    
    :a: string - any string
    :b: integer - any integer
    :c: float - any float value
    
    :returns: string
    """
    return "{a}, {b}, {c}".format(a=a,
                                  b=b, 
                                  c=c)
first_method("a", 1, 1.2)<jupyter_output><empty_output><jupyter_text>Lambda Statements- short one line function- useful for quick expression that do not require a whole function definition.**Syntax**```   func_name = lambda , ,... : ```<jupyter_code>area = lambda l,w : l * w
area(10,10)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1690793283149971, 'fraction_numerical': 0.026780544296467864, 'mean_word_length': 3.509791122715405, 'pattern_counts': {'":': 0, '<': 70, '<?xml version=': 0, '>': 70, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '18', 'dir': 'jupyter-structured-clean-dedup', 'id': '164352', 'license': 'Apache-2.0', 'n_tokens_mistral': 2519, 'n_tokens_neox': 2356, 'n_words': 775, 'path': 'lecture/Lesson 01/Lesson 01 - Review of Python Part 1.ipynb', 'repo_name': 'shaheen19/Adv_Py_Scripting_for_GIS_Course', 'size': '6908'}