Intermediate Python Ch4 Slides
Intermediate Python Ch4 Slides
Intermediate Python Ch4 Slides
while loop
Intermediate Python for Data Science
if-elif-else
! control.py
Goes through construct only once!
z = 6
if z % 2 == 0 : True
print("z is divisible by 2") Executed
elif z % 3 == 0 :
print("z is divisible by 3")
else :
print("z is neither divisible by 2 nor by 3")
... Moving on
While
while condition : "
expression
Example
Error starts at 50
! while_loop.py
error = 50.0
! while_loop.py
error = 50.0
50.0
while error > 1 : True
error = error / 4
print(error)
Output:
12.5
Intermediate Python for Data Science
! while_loop.py
error = 50.0
12.5
while error > 1 : True
error = error / 4
print(error)
Output:
12.5
3.125
Intermediate Python for Data Science
! while_loop.py
error = 50.0
3.125
while error > 1 : True
error = error / 4
print(error)
Output:
12.5
3.125
0.78125
Intermediate Python for Data Science
! while_loop.py
error = 50.0
0.78125
while error > 1 : False
error = error / 4 not executed
print(error)
Output:
12.5
3.125
0.78125
Intermediate Python for Data Science
! while_loop.py
error = 50.0
Output:
50
50
50
50 DataCamp: session disconnected
50 Local system: Control + C
50
50
...
INTERMEDIATE PYTHON FOR DATA SCIENCE
Lets practice!
INTERMEDIATE PYTHON FOR DATA SCIENCE
for loop
Intermediate Python for Data Science
for loop
for var in seq : !
expression
fam
" family.py
Output:
[1.73, 1.68, 1.71, 1.89]
Intermediate Python for Data Science
fam
" family.py
Output:
1.73
1.68
1.71
1.89
Intermediate Python for Data Science
" family.py
" family.py
Output:
1.73
Intermediate Python for Data Science
" family.py
Output:
1.73
1.68
Intermediate Python for Data Science
" family.py
Output:
1.73
1.68
1.71
1.89
Intermediate Python for Data Science
" family.py
???
Output:
1.73 index 0: 1.73
1.68 index 1: 1.68
1.71 index 2: 1.71
1.89 index 3: 1.89
Intermediate Python for Data Science
" family.py
Output:
index 0: 1.73
index 1: 1.68
index 2: 1.71
index 3: 1.89
Intermediate Python for Data Science
" strloop.py
for c in "family" :
print(c.capitalize())
Output:
F
A
M
I
L
Y
INTERMEDIATE PYTHON FOR DATA SCIENCE
Lets practice!
INTERMEDIATE PYTHON FOR DATA SCIENCE
" dictloop.py
world = { "afghanistan":30.55,
"albania":2.77,
"algeria":39.21 }
Output:
ValueError: too many values to unpack (expected 2)
Intermediate Python for Data Science
" dictloop.py
world = { "afghanistan":30.55,
"albania":2.77,
"algeria":39.21 }
Output:
algeria -- 39.21
afghanistan -- 30.55
albania -- 2.77
Intermediate Python for Data Science
" dictloop.py
world = { "afghanistan":30.55,
"albania":2.77,
"algeria":39.21 }
key value
for k, v in world.items() :
print(k + " -- " + str(v))
Output:
algeria -- 39.21
afghanistan -- 30.55
albania -- 2.77
Intermediate Python for Data Science
" nploop.py
import numpy as np
np_height = np.array([1.73, 1.68, 1.71, 1.89, 1.79])
np_weight = np.array([65.4, 59.2, 63.6, 88.4, 68.7])
bmi = np_weight / np_height ** 2
for val in bmi :
print(val)
Output:
21.852
20.975
21.750
24.747
21.441
Intermediate Python for Data Science
" np2dloop.py
import numpy as np
np_height = np.array([1.73, 1.68, 1.71, 1.89, 1.79])
np_weight = np.array([65.4, 59.2, 63.6, 88.4, 68.7])
meas = np.array([np_height, np_weight])
for val in meas :
print(val)
Output:
[ 1.73 1.68 1.71 1.89 1.79]
[ 65.4 59.2 63.6 88.4 68.7]
Intermediate Python for Data Science
" np2dloop.py
import numpy as np
np_height = np.array([1.73, 1.68, 1.71, 1.89, 1.79])
np_weight = np.array([65.4, 59.2, 63.6, 88.4, 68.7])
meas = np.array([np_height, np_weight])
for val in np.nditer(meas) :
print(val)
Recap
Dictionary
for key, val in my_dict.items() :
Numpy array
for val in np.nditer(my_array) :
INTERMEDIATE PYTHON FOR DATA SCIENCE
Lets practice!
INTERMEDIATE PYTHON FOR DATA SCIENCE
brics BR
RU
IN
country
Brazil
Russia
India
capital
Brasilia
Moscow
New Delhi
area
8.516
17.100
3.286
population
200.40
143.50
1252.00
CH China Beijing 9.597 1357.00
! dfloop.py SA South Africa Pretoria 1.221 52.98
import pandas as pd
brics = pd.read_csv("brics.csv", index_col = 0)
Intermediate Python for Data Science
import pandas as pd
brics = pd.read_csv("brics.csv", index_col = 0)
Output:
country
capital
area
population
Intermediate Python for Data Science
iterrows BR
RU
IN
country
Brazil
Russia
India
capital
Brasilia
Moscow
New Delhi
area
8.516
17.100
3.286
population
200.40
143.50
1252.00
CH China Beijing 9.597 1357.00
! dfloop.py SA South Africa Pretoria 1.221 52.98
import pandas as pd
brics = pd.read_csv("brics.csv", index_col = 0)
Selective print BR
RU
IN
country
Brazil
Russia
India
capital
Brasilia
Moscow
New Delhi
area
8.516
17.100
3.286
population
200.40
143.50
1252.00
CH China Beijing 9.597 1357.00
! dfloop.py SA South Africa Pretoria 1.221 52.98
import pandas as pd
brics = pd.read_csv("brics.csv", index_col = 0)
Output:
BR: Brasilia
RU: Moscow
IN: New Delhi
CH: Beijing
SA: Pretoria
Intermediate Python for Data Science
Add column BR
RU
IN
country
Brazil
Russia
India
capital
Brasilia
Moscow
New Delhi
area
8.516
17.100
3.286
population
200.40
143.50
1252.00
CH China Beijing 9.597 1357.00
! dfloop.py SA South Africa Pretoria 1.221 52.98
import pandas as pd
brics = pd.read_csv("brics.csv", index_col = 0)
Output:
country capital area population name_length
BR Brazil Brasilia 8.516 200.40 6
RU Russia Moscow 17.100 143.50 6
IN India New Delhi 3.286 1252.00 5
CH China Beijing 9.597 1357.00 5
SA South Africa Pretoria 1.221 52.98 12
Intermediate Python for Data Science
apply BR
RU
IN
country
Brazil
Russia
India
capital
Brasilia
Moscow
New Delhi
area
8.516
17.100
3.286
population
200.40
143.50
1252.00
CH China Beijing 9.597 1357.00
! dfloop.py SA South Africa Pretoria 1.221 52.98
import pandas as pd
brics = pd.read_csv("brics.csv", index_col = 0)
brics["name_length"] = brics["country"].apply(len)
print(brics) len()
Output:
country capital area population name_length
BR Brazil Brasilia 8.516 200.40 6
RU Russia Moscow 17.100 143.50 6
IN India New Delhi 3.286 1252.00 5
CH China Beijing 9.597 1357.00 5
SA South Africa Pretoria 1.221 52.98 12
INTERMEDIATE PYTHON FOR DATA SCIENCE
Lets practice!