In [2]:
# the = symbol indicates that what is on the right is assigned to the variable name on the left
name = 'Lina'
year = 2016
In [3]:
# we can then see what our variable is holding using the print() function
print(name)
# we can check the type of our variable using the type(variable_name) function
print(type(year))
In [4]:
# you must assign a variable before you call it, otherwise an error will occur
print(age)
In [6]:
fruits = ['apple', 'banana', 'mango', 'lychee']
In [7]:
print(fruits)
In [10]:
fruits.append('orange')
In [11]:
print(fruits)
In [13]:
# lists don't need to comprise of all the same type
misc = [29, 'dog', fruits]
In [14]:
print(misc)
In [15]:
#indexing in Python starts at 0
print(fruits[0])
In [ ]:
print(fruits[1])
In [16]:
s = 'This is a string.'
In [17]:
print(s[0])
In [18]:
# use -1 to get the last element
print(fruits[-1])
In [19]:
print(fruits[-2])
In [20]:
# to get a slice of the string use the : symbol
print(s[0:4])
In [21]:
print(s[:4])
In [22]:
print(s[4:7])
In [23]:
print(s[7:])
print(s[7:len(s)])
In [24]:
s2 = [19034, 23]
# You will always need to start with an 'if' line
# You do not need the elif or else statements
# You can have as many elif statements as needed
if type(s2) == str:
print('s2 is a string')
elif type(s2) == int:
print('s2 is an integer')
elif type(s2) == float:
print('s2 is a float')
else:
print('s2 is not a string or integer')
In [25]:
nums = [23, 56, 1, 10, 15, 0]
In [26]:
# in this case, 'n' is a dummy variable that will be used by the for loop
# you do not need to assign it ahead of time
for n in nums:
if n%2 == 0:
print('even')
else:
print('odd')
In [27]:
# for loops can iterate over strings as well
vowels = 'aeiou'
for vowel in vowels:
print(vowel)
In [30]:
# always use descriptive naming for functions, variables, arguments etc.
def sum_of_squares(num1, num2):
ss = num1**2 + num2**2
return(ss)
In [31]:
print(sum_of_squares(4,2))
In [32]:
# the return statement in a function allows us to store the output of a function call in a variable for later use
ss1 = sum_of_squares(5,5)
In [33]:
print(ss1)
In [34]:
# use a package by importing it, you can also give it a shorter alias, in this case 'np'
import numpy as np
In [38]:
array = np.arange(15)
lst = list(range(15))
In [39]:
print(array)
print(lst)
In [40]:
print(type(array))
print(type(lst))
In [41]:
# numpy arrays allow for vectorized calculations
print(array*2)
print(lst*2)
In [42]:
array = array.reshape([5,3])
print(array)
In [43]:
# we can get the mean over all rows (using axis=1)
array.mean(axis=1)
Out[43]:
In [44]:
# max value in each column
array.max(axis=0)
Out[44]:
In [4]:
import pandas as pd
In [5]:
# this will read in a csv file into a pandas DataFrame
# this csv has data of country spending on healthcare
data = pd.read_csv('health.csv', header=0, index_col=0, encoding="ISO-8859-1")
In [6]:
# the .head() function will allow us to look at first few lines of the dataframe
data.head()
Out[6]:
In [7]:
# by default, rows are indicated first, followed by the column: [row, column]
data.loc['Canada', '2008']
Out[7]:
In [8]:
# you can also slice a dataframe
data.loc['Canada':'Denmark', '1999':'2001']
Out[8]:
In [9]:
%matplotlib inline
import matplotlib.pyplot as plt
In [10]:
# the .plot() function will create a simple graph for you to quickly visualize your data
data.loc['Denmark'].plot()
data.loc['Canada'].plot()
data.loc['India'].plot()
plt.legend(loc='best')
Out[10]: