In [13]:
pd.read_
In [ ]:
"../class2/"
In [ ]:
"data/Fatality.csv"
In [1]:
##Some code to run at the beginning of the file, to be able to show images in the notebook
##Don't worry about this cell
#Print the plots in this screen
%matplotlib inline
#Be able to plot images saved in the hard drive
from IPython.display import Image
#Make the notebook wider
from IPython.core.display import display, HTML
display(HTML("<style>.container { width:90% !important; }</style>"))
In [1]:
#Using the symbol "#" you write comments
#printing something to the screen is easy:
print("Hello World")
#Now click on the play button in the toolbar above (or click Ctrl + Enter)
In [9]:
Image("./images/dashboard_files_tab.png",width=500)
Out[9]:
In [10]:
Image("./images/dashboard_files_tab_new.png",width=200)
Out[10]:
In [11]:
Image("./images/dashboard_files_tab_btns.png",width=400)
Out[11]:
In [12]:
Image("./images/edit_mode.png")
Out[12]:
In [13]:
Image("./images/command_mode.png")
Out[13]:
RUN PYTHON Write some code in a code cell (the default one) and click the "play button" (shortcut Ctrl+Enter)
In [4]:
Image("./images/menubar_toolbar.png")
Out[4]:
In [5]:
#Let's say that a = 5, and ask jupyter with help with a. We'll see more on this later.
#Select this cell and run it (Ctrl + Enter)
a = 5.3
a?
In [16]:
## HOW TO IMPORT PACKAGES AND READ A CSV (we'll learn this in one hour)
#Standard mode
import pandas
spreadsheet = pandas.read_csv("data/class1_test_csv.csv")
#Standard mode with packages that have long names
import pandas as pd
spreadsheet = pd.read_csv("data/class1_test_csv.csv")
#Standard mode when you only want to import one function
from pandas import read_csv
spreadsheet = read_csv("data/class1_test_csv.csv")
#Import everything, DO NOT USE! It's against the Zen of Python (https://www.python.org/dev/peps/pep-0020/)
from pandas import *
spreadsheet = read_csv("data/class1_test_csv.csv")
To install new packages you can use pip. For example run the code cell below
In [17]:
#Let's install the package pandas, which is used to plot
!pip install pandas
Guido van Rossum
(graduated from UvA in 1982, created Python in 1989)easy
The Zen of Python
Beautiful is better than ugly.
Simple is better than complex.
Readability counts.
If the implementation is hard to explain, it's a bad idea.
There should be one -- and preferably only one -- obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Python uses variables and code.
Variables tell the computer to save something (a number, a string, a spreadsheet) with a name. For instance, if you write variable_name = 3, the computer knows that variable_name is 3.
You can do operations with them, such as multiplication for numbers, concatenation for strings, etc
In [18]:
print(type(3))
print(type(3.5))
print(type("I'm a string"))
print(type(False))
print(type(None))
In [19]:
##Using python as a calculator
print(5+2) #5+2
print(5*2) #5x2
print(5/2) #5/2
print(5**2) #5^2, 3 to the power of two
print(5%2) #This is called modulo, and gives you the remainder when you divide 5 by 2 (5/2 = 5*2 + 1)
In [20]:
##We can also "assign" the number to a "variable".
#The variable name can be whatever you want, but cannot start with a number and CANNOT spaces.
#Please use variable names that describe what they represent
#"grades_hw_1" is much better than "a14"
var1 = 5
var2 = 2
print(var1+var2) #5+2
print(var1*var2) #5x2
print(var1/var2) #5/2
print(var1**var2) #5^2, 3 to the power of two
print(var1%var2) #This is called modulo, and gives you the remainder when you divide 5 by 2 (5/2 = 5*2 + 1)
Strings are a series of characters, such as "eggs and bacon".
Beware of the encoding
In [21]:
Image(url="https://upload.wikimedia.org/wikipedia/commons/c/c4/Utf8webgrowth.svg")
Out[21]:
In [5]:
v = 5.321233258340857891
print("The value was {}".format(v))
In [3]:
print("eggs")
print("eggs" + "and" + "bacon") #concatenating strings
print("eggs" + " and " + "bacon") #concatenating strings with spaces
print("eggs and bacon".upper()) #upper case lower() for lower case
##String formatting. Each element inside format() is added in the place of each {}
print("{} {} and {} = diabetes".format(5,"sausages","bacon")) #used to format strings.
##Checking if string is contained
print("bacon" in "eggs and bacon") #checks if the string "bacon" is part of the string "eggs and bacon"
In [6]:
## We can also use variables
var1 = "eggs"
var2 = "bacon"
print(var1)
print(var1 + "and" + var2) #concatenating strings
print(var1 + " and " + var2) #concatenating strings with spaces
var_combined = var1 + " and " + var2
print(var_combined.upper()) #upper case lower() for lower case
##String formatting. Each element inside format() is added in the place of each {}
print("{} {} and {} = diabetes".format(5,var1,var2)) #used to format strings.
##Checking if string is contained
print("bacon" in var_combined) #checks if the string "bacon" is part of the string "eggs and bacon"
In [7]:
var_combined
Out[7]:
In [10]:
#lower and upper case are different characters
print("bacon" in var_combined)
Useful when you are comparing variables to data (variable1 == variable2)
Careful, True (capitalized) is a boolean, true (not capitalized) is nothing.
Common mistake
variable1 == variable2
asks the computer if variable1 is equal to variable2. The computer answers with a boolean (True/False)variable1 = variable2
, this tells the computer that variable 1 is equal to variable2
In [24]:
print("bacon in var_combined: ", "bacon" in var_combined)
print("bacon == var1: ","bacon" == var1) ##look at the == symol
print("bacon == var2: ", "bacon" == var2)
print("3 > 5: ", 3 > 5)
print("3 < 5: ", 3 < 5)
In [12]:
## OPERATIONS ON DATA TYPES
#Tells the computer that b = 3
b = 3
#Tells the computer that b = 5
b = 5
#Asks if b is equal to 3
print(b)
In [26]:
#The computer prints a (3)
a = 3
print(a)
In [27]:
#The computer doesn't print anything
a = 3
print()
But now we want to combine them, which is convenient when you have many variables. For instance, you may want to read all the numbers in a csv file and do not have thousands of variables. We combine them in DATA STRUCTURES (next notebook)