Instructor: Haidy Giratallah
Lesson repeat from: UofT coder lessons (https://github.com/UofTCoders/studyGroup/blob/gh-pages/lessons/python/intro/IntroPython-AH.ipynb)
Python is a general-purpose language with a readable syntax. In summary: "Python is powerful... and fast; plays well with others; runs everywhere; is friendly & easy to learn; is Open." https://www.python.org/
Common question: Python vs R?
R is mainly used for statistical analysis while Python provides a more general approach to data science. R and Python are state of the art in terms of programming language oriented towards data science. Learning both of them is, of course, the ideal solution. ... (https://www.guru99.com/r-vs-python.html)
In [0]:
In [0]:
In [0]:
In [0]:
# % is the modulus operator
# x % y -> return the remainder of x / y
In [0]:
In [0]:
In [0]:
In [0]:
In [0]:
In [0]:
In [0]:
In [0]:
In [0]:
In [0]:
In [0]:
In [0]:
In [0]:
In [0]:
# indexing in python begins at 0!
In [0]:
# slicing multiple things
# start : end (exclusive!)
# recall that fruits = ['apple', 'orange', 'mango']
In [0]:
# reassigning item in list
In [0]:
# slicing and indexing strings
In [0]:
# strings do not support reassignment
# try running this: my_string[0] = 't'
In [0]:
# dictionaries allow us to store key value pairs
In [0]:
# keys are 'looked up' using square brackets
In [0]:
# additional keys can be added after the fact
In [0]:
# python can check whether certain statements are true or false
In [0]:
# use == to test for equality
In [0]:
In [0]:
# with these expressions, we can construct if statements
# if statements allow our scripts to encode more complex instructions
In [0]:
# if-else
In [0]:
# if-elif-else
# useful if we have multiple conditions to test
In [0]:
# for loops allow us to automate repetitive operations
# how do we check which values in this list are even?
# could check them individually?
In [0]:
# for loops simplify this
# here, 'number' is a placeholder variable for each of the items in the list
In [0]:
# we can also loop over the contents of a string
vowels = 'aeiou'
In [0]:
# functions allow us to generalize operations
# what is the sum of squares of two numbers?
In [0]:
# def is the keyword to define functions
# each function typically ends with a return statement
In [0]:
# our operation from above
# works with any values we want!
In [0]:
# checking on our docstring
In [0]:
# use a package by importing it
# these can be given a shorter alias
import numpy as np
In [0]:
# packages provide all sorts of useful functionality
# numpy allows for efficient numerical calculations in python
np_array = np.arange(15)
list_array = list(range(15))
print(np_array)
print(type(np_array))
print(list_array)
print(type(list_array))
In [0]:
# numpy arrays also allow for vectorized operations
print(np_array * 2)
print(list_array * 2)
In [0]:
# numpy arrays also have helpful 'methods'
# a method is a special function 'attached' to an object, to be used on the object itself
# what's the mean of our array?
print(np_array.mean())
In [0]:
# the max value in our array?
print(np_array.max())
In [0]:
import pandas as pd
import seaborn as sns # we will use this for plotting
%matplotlib inline
iris = sns.load_dataset('iris')
In [0]:
iris.head()
In [0]:
iris.columns
In [0]:
# pull out specific rows with the .loc method
iris.loc[0:2]
In [0]:
# or rows AND columns
# use a list for multiple columns!
iris.loc[0:2, 'petal_length']
In [0]:
iris.loc[0:2, ['petal_length', 'species']]
In [0]:
sns.relplot(x='petal_length', y='petal_width', data=iris)
In [0]:
sns.relplot(x='petal_length', y='petal_width', hue='species', data=iris)