# Python chat 2022-09-16 (Sarah / Jiawen) **Short term goal:** - write a table into Excel, export it as ".csv" file - read the ".csv" file using Python (into a Pandas Dataframe) - calculate the average of each column with Pandas - (optional) make some plots with Matplotlib ## Pre-requisites Download [Anaconda](https://www.anaconda.com/products/distribution): interpreter + package manager + tools - Type "python" in your terminal / anaconda console, enter "interactive mode". - Use any text editor to write a file, "*.py", write code inside it, and run `python <file_name>.py` ## Concepts syntax: variable, value (types), function, class (optional) control flow (if/elif/else, while, for) ```python= a = 1.0 # assignment, 1.0 (value) -> a (variable) b = 2 c = a + b # C = 3.0 def my_add_func(x, y): return x + y # my_add_func(x,y) = x+y d = my_add_func(a, b) # d = 3.0 my_return_value = my_add_func(3, 4) def my_abs_add_func(x, y): x_abs = abs(x) y_abs = abs(y) return x_abs + y_abs def my_weird_add_func(x, y): if x < 1: z = 2 elif x > 3: z = 1 else: z = 0 return z + y def my_addition(x): result = 0 for element in x: result = result + element return result my_list = [3,4,5,6] print(my_addition(my_list)) ``` ### Types inbuilt types: int (整数integer), float(小数), str(string,"hello"/'hello'), list (`[1, 2, 3]`), dict (`{"sarah": "female", "nancy": "female"}`; `{"blue": 20, "green": 10}`), etc. ## Data analysis pandas (data analysis), matplotlib (visualisation) ## Projects ## TODO - [ ] Download Anaconda - [ ] Make sure you roughly know the concepts of: - variable - [ ] name all the variables in code above - [ ] declare a new variable that has value "hello world" - value - [ ] name all the values in code above - an assignment statement - [ ] list all the assignment statements in code above - function - [ ] list all the functions in code above - [ ] write a function that calculates the difference between two numbers - if / elif / else (more exercise next time) - for loop (more exercise next time) - comments in Python - [ ] what does a comment start with? - [ ] Does comments have any effect in code execution? - Additionally, syntax for multi-line comments is like below ```python= """ Hello! I'm in a multi-line comment """ ``` ## Next ``` import pandas as pd df = pd.DataFrame({'num_legs': [2, 4], 'num_wings': [2, 0]}, index=['falcon', 'dog']) ``` Make sure Anaconda is installed correctly. Run the code above in three places: - Jupyter notebook - interactive mode - a file