class: center, middle, inverse, title-slide # Introduction to Python ### Ali Seyhun Saral ### 18 Nov 2021 --- <style type="text/css"> .remark-code, .remark-inline-code { font-family: 'Source Code Pro', 'Lucida Console', Monaco, monospace; color: blue; background-color: rgba(222,222,222,0.3); } .titletext { font-size: 400%; margin-top: 350px; font-weight: bold; display:inline-block; } </style> # What is Python? <img src="images/pythonlogo.svg" width="25%" style="display: block; margin: auto;" /> * `Python` is the one of the most popular general-purpose programming languages -- * Applications include -but not limited to- web development, software development, data analysis, automated tasks... -- * We will use `Python` as a **backend** in our `oTree` journey. -- * We will learn the fundementals, enought to kickstart `oTree`. --- # Hello World! * To print things in the console, one should use `print()` function. -- * Let's go ahead and print `Hello World`: ```python print("Hello World!") ``` ``` ## Hello World! ``` --- # How to interact with Python * Python is essentially a **command-line software**. -- 1- Python Interpreter: <img src="images/python_interpreter.png" width="75%" style="display: block; margin: auto;" /> --- # How to interact with Python 2- Running a Python script from command-line: * Python scripts have the extension `.py`. .pull-left[ <div class="figure" style="text-align: center"> <img src="images/python_notepad.png" alt="my_python_file.py" width="100%" /> <p class="caption">my_python_file.py</p> </div> ] -- .pull-right[ <img src="images/python_script.png" width="100%" style="display: block; margin: auto;" /> ] --- # How to interact with Python 3- Using a development environment * Some of them are `PyCharm`, `VSCode`, `RStudio` and so on. ### RStudio . .pull-left[ <div class="figure" style="text-align: center"> <img src="images/rstudio_python.png" alt="Writing Python code in RStudio" width="80%" /> <p class="caption">Writing Python code in RStudio</p> </div> ] .pull-right[ * Use `Shift + Enter` to send the command to shell ] --- # How to interact with Python 3- Using a development environment * Some of them are `PyCharm`, `VSCode`, `RStudio` and so on. ### VSCode . .pull-left[ <div class="figure" style="text-align: center"> <img src="images/vscode_python.png" alt="Writing Python code in Visual Studio Code" width="100%" /> <p class="caption">Writing Python code in Visual Studio Code</p> </div> ] .pull-right[ * Use `Shift + Enter` to send the command to shell ] --- # How to interact with Python 3- Using a development environment * Some of them are `PyCharm`, `VSCode`, `RStudio` and so on. ### PyCharm . .pull-left[ <div class="figure" style="text-align: center"> <img src="images/pycharm_python.png" alt="Writing Python code in PyCharm" width="100%" /> <p class="caption">Writing Python code in PyCharm</p> </div> ] .pull-right[ * Use `Alt + Shift + E` to send the command to shell ] --- # How to interact with Python * Using `python` as calculator: -- ```python 42 + 38 ``` ``` ## 80 ``` -- ```python 36 * 5 ``` ``` ## 180 ``` --- class: inverse .center[.titletext[Fundementals & Atomic Types]] --- # Arithmethic Operators in Python * `a + b` Addition * `a - b` Subtraction * `a * b` Mutiplication * `a / b` Division * `a ** b` Exponention (\\( a^b \\)) --- # Variables in Python * Links a value to a name * Case-sensitive * Can contain any alphanumeric characters and underscores (A-z, 0-9, _) * Cannot start with a number -- ```python height = 297 width = 210 area_mm2 = height * width print(area_mm2) ``` ``` ## 62370 ``` --- # Commenting in Python If you have the character `#` on a line, python will not read the rest -- ```python # Area of an A4 paper height = 297 # in mm width = 210 # in mm area_mm2 = height * width # in mm2 print(area_mm2) ``` ``` ## 62370 ``` --- # Types in Python ```python # Area of an A4 paper height = 297 # in mm type(height) ``` ``` ## <class 'int'> ``` -- ```python # Area of an A4 paper height = 297.0 # in mm type(height) ``` ``` ## <class 'float'> ``` --- # Types in Python We will deal with four main variable types: * Integers (`int`): `1`, `2`, `-5` ,`2910` -- * Float (`float`): `1.0`, `2.5`, `-5.3` -- ----> Decimal numbers -- * String (`str`): `"hi"`, `'good morning!'`, `'Wow! 😲'` -- ----> Text -- * Boolean (` bool`): `True`, `False` -- ----> Logical --- # Types in Python * Each type has its own set of rules ```python 3 + 5 ``` -- ``` ## 8 ``` -- ```python '3' + '5' ``` -- ``` ## '35' ``` -- ```python "Hello" + " World!" ``` ``` ## 'Hello World!' ``` --- class: inverse .center[.titletext[Lists]] --- # Lists in Python * Ordered collection of values -- * They can contain different types -- ```python a = [10,20,30] b = ["ali", "bob", "chiara"] c = ["ali", 2] ``` -- * Individual items can be accessed with an index ```python a[0] ``` ``` ## 10 ``` -- ```python b[1] ``` ``` ## 'bob' ``` -- * **Important:** Indices in Python start from 0 --- # Lists in Python * List are very flexible. They can also contain other lists. ```python p1 = ["ali", 1987] p2 = ["bob", 1953] people = [p1,p2] print(people) ``` ``` ## [['ali', 1987], ['bob', 1953]] ``` -- ```python print(people[0]) ``` ``` ## ['ali', 1987] ``` -- ```python print(people[0][0]) ``` ``` ## ali ``` --- # `list` type * Like variables, list have their type `list` and they have their own operations. ```python players1 = ["ali", "bob"] type(players1) ``` ``` ## <class 'list'> ``` -- ```python players1 = ["ali", "bob"] players2 = ["chiara", "dana"] print(players1 + players2) ``` ``` ## ['ali', 'bob', 'chiara', 'dana'] ``` --- # Subsetting lists * Getting one item with index (Remember to start from `0`) ```python players = ["ali","bob","chiara","dana"] players[2] ``` ``` ## 'chiara' ``` -- * Start from the end (The last item is -1) ```python players[-1] ``` ``` ## 'dana' ``` -- * Slice it ```python # list[start_index:stop_index(not included)] # start: index 1(second item,included), stop: index 3(not included) players[1:3] ``` ``` ## ['bob', 'chiara'] ``` -- --- # Subsetting lists * Slice from the beginning and from the end ```python players[:2] ``` ``` ## ['ali', 'bob'] ``` ```python players[2:] ``` ``` ## ['chiara', 'dana'] ``` --- # Be careful: Python variables are references ```python my_list = ["ali", "bob", "chiara"] another_list = my_list ``` -- ```python another_list[0] = "dana" ``` -- ```python print(my_list) ``` ``` ## ['dana', 'bob', 'chiara'] ``` --- # Solution: Copy the list ```python my_list = ["ali", "bob", "chiara"] another_list = my_list.copy() ``` -- ```python another_list[0] = "dana" ``` -- ```python print(another_list) ``` ``` ## ['dana', 'bob', 'chiara'] ``` ```python print(my_list) ``` ``` ## ['ali', 'bob', 'chiara'] ``` --- class: inverse .center[.titletext[Dictionaries]] --- # Dictionaries * Also a collection of objects like `lists` -- * Unlike lists, items have keys (in other words keywords) -- * Can be created with: * `{key1: value1, key2: value2}`, or * `dict(key1=value1, key2=value2)` ```python my_dictionary = {'ali': 1987, 'bob': 1953, 'chiara':1980} ``` -- ``` ## {'ali': 1987, 'bob': 1953, 'chiara': 1980} ``` -- ```python my_dictionary2 = dict(ali=1987, bob=1953, chiara=1980) ``` -- ``` ## {'ali': 1987, 'bob': 1953, 'chiara': 1980} ``` --- # Get an item from dictionary ```python birthyears = {"ali": 1987, "bob": 1953, "chiara":1980} ``` -- ```python birthyears["bob"] ``` ``` ## 1953 ``` --- # Adding item to dictionary ```python birthyears = {"ali": 1987, "bob": 1953, "chiara":1980} birthyears['dana'] = 1992 print(birthyears) ``` ``` ## {'ali': 1987, 'bob': 1953, 'chiara': 1980, 'dana': 1992} ``` --- class: inverse .center[.titletext[Conditional Statements]] --- # Logical Operators * We have two logical values: `True` and `False` * `and` and `or` and `not` are the logical operators * `and` means that both conditions must be true * `or` means that at least one condition must be true * `not` reverses the logical value ```python a = 5 b = 10 print(a == 5 and b == 10) ``` ``` ## True ``` ```python print(a == 5 and b == 5) ``` ``` ## False ``` ```python print(a == 1 or b == 10) ``` ``` ## True ``` ```python print(not a == 1) ``` ``` ## True ``` Logical Operators ```python a = 5 b = 10 print(a == 1 or b == 10) ``` ``` ## True ``` ```python print(not a == 1) ``` ``` ## True ``` --- # `if` statement ```python my_variable = 42 ``` --- ```python my_variable = 42 if my_variable < 50: print("the variable is smaller than 50") ``` ``` ## the variable is smaller than 50 ``` --- # Very important: Indentation ```python my_variable = 42 if my_variable < 50: print("the variable is smaller than 50") ``` ``` ## the variable is smaller than 50 ``` * Python doesn't have braces like `{ }` or `end` statements -- * The hierarchy/ownership of the statements are determined by indents. -- * `Four spaces` is the accepted convention but you can use `tab` or the another number of spaces as long as it is consistent. -- * Graphical user interfaces does it mostly automatic. --- # `else` statement ```python my_variable = 42 if my_variable < 50: print("the variable is smaller than 50") else: print("the variable is bigger than 50") ``` ``` ## the variable is smaller than 50 ``` --- # `elif` statement ```python my_variable = 42 if my_variable < 40: print("the variable is smaller than 40") elif my_variable < 50: print("the variable is between 40 and 50") else: print("the variable is bigger than 50") ``` ``` ## the variable is between 40 and 50 ``` --- class: inverse .center[.titletext[Functions, Methods, Classes]] --- # Functions in python * Python has many built-in functions ```python ages = [39,48,21,59] max(ages) ``` ``` ## 59 ``` ```python my_name = "ali" len(my_name) ``` ``` ## 3 ``` --- # We can build our own functions * Functions are defined with `def` keyword. ```python def min_max_difference(x): return(max(x) - min(x)) # Beware of indentation ages = [39,48,21,59] min_max_difference(ages) ``` ``` ## 38 ``` --- # Methods * Some functions are associated to objects. They are called methods. * The syntax for methods is `my_object.do_something()` ```python ages = [39,48,21,59] ages.index(21) # find the index of a given item ``` ``` ## 2 ``` --- class: inverse .center[.titletext[Week 2]] --- # For loop over list items * Lists are not just good for collecting items * Also for looping over them -- ```python my_list = [1,2,3,4] ``` ```python for x in my_list: print(x ** 2) ``` ``` ## 1 ## 4 ## 9 ## 16 ``` --- # Creating a new list using for loop * We can generate an empty list and add items recursively. -- * `list_name.append()` to add an item to a list -- Example: ```python my_list = [1,2,3,4] squares = [] for x in my_list: squares.append(x ** 2) ``` -- ```python print(squares) ``` ``` ## [1, 4, 9, 16] ``` --- # List Comprehension * List comprehension is a shorter syntax when you create a new list based on another list. ```python my_list = [1,2,3,4] ``` ```python squares = [] for x in my_list: squares.append(x ** 2) ``` -- we can write instead: ```python squares = [x ** 2 for x in my_list] ``` -- $$ S = \\{ x^2 | \text{ for all } x \in \text{my_list} \\} $$ --- # List Comprehension ```python my_list = [1,2,3,4] squares = [x ** 2 for x in my_list] print(squares) ``` ``` ## [1, 4, 9, 16] ``` -- * We can also add condition ```python my_list = [1,2,3,4] squares = [x ** 2 for x in my_list if x > 2] print(squares) ``` ``` ## [9, 16] ```