Intermediate Python Ch4 Slides

Download as pdf or txt
Download as pdf or txt
You are on page 1of 39
At a glance
Powered by AI
The key takeaways are that while loops allow repeated execution of a block of code as long as a condition is met, and for loops iterate over a sequence, executing an expression for each item.

While loops in Python are used to repeatedly execute a block of code as long as a condition is met. They are commonly used for tasks like numerical calculations that require repeating an action until a condition is reached.

A for loop in Python iterates over a sequence, executing an expression for each item. It takes the form 'for variable in sequence: expression' where the variable takes on each value from the sequence in turn and the expression is executed. This allows efficiently iterating through lists, tuples, dictionaries and other sequences.

INTERMEDIATE PYTHON FOR DATA SCIENCE

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 loop = repeated if statement


Intermediate Python For Data Science

While
while condition : "
expression

Numerically calculating model

"repeating action until condition is met

Example
Error starts at 50

Divide error by 4 on every run

Continue until error no longer > 1


Intermediate Python for Data Science

While Error starts at 50


Divide error by 4 on every run
Continue until error no longer > 1
while condition :
expression
"

! while_loop.py

error = 50.0

while error > 1 :


error = error / 4
print(error)
Intermediate Python for Data Science

While while condition :


expression
"

! 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 while condition :


expression
"

! 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 while condition :


expression
"

! 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 while condition :


expression
"

! 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 while condition :


expression
"

! while_loop.py

error = 50.0

while error > 1 : always True


# error = error / 4
print(error)

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

"for each var in seq, execute expression"


Intermediate Python for Data Science

fam
" family.py

fam = [1.73, 1.68, 1.71, 1.89]


print(fam)

Output:
[1.73, 1.68, 1.71, 1.89]
Intermediate Python for Data Science

fam
" family.py

fam = [1.73, 1.68, 1.71, 1.89]


print(fam[0])
print(fam[1])
print(fam[2])
print(fam[3])

Output:
1.73
1.68
1.71
1.89
Intermediate Python for Data Science

for loop for var in seq :


expression
!

" family.py

fam = [1.73, 1.68, 1.71, 1.89]

for height in fam :


print(height)
Intermediate Python for Data Science

for loop for var in seq :


expression
!

" family.py

fam = [1.73, 1.68, 1.71, 1.89]

for height in fam :


print(height) first iteration
height = 1.73

Output:
1.73
Intermediate Python for Data Science

for loop for var in seq :


expression
!

" family.py

fam = [1.73, 1.68, 1.71, 1.89]

for height in fam :


print(height) second iteration
height = 1.68

Output:
1.73
1.68
Intermediate Python for Data Science

for loop for var in seq :


expression
!

" family.py

fam = [1.73, 1.68, 1.71, 1.89]

for height in fam :


print(height)
no access to indexes

Output:
1.73
1.68
1.71
1.89
Intermediate Python for Data Science

for loop for var in seq :


expression
!

" family.py

fam = [1.73, 1.68, 1.71, 1.89]

???

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

enumerate for var in seq :


expression
!

" family.py

fam = [1.73, 1.68, 1.71, 1.89]

for index, height in enumerate(fam) :


print("index " + str(index) + ": " + str(height))

Output:
index 0: 1.73
index 1: 1.68
index 2: 1.71
index 3: 1.89
Intermediate Python for Data Science

Loop over string for var in seq :


expression
!

" 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

Loop Data Structures


Part 1
Intermediate Python for Data Science

Dictionary for var in seq :


expression
!

" dictloop.py

world = { "afghanistan":30.55,
"albania":2.77,
"algeria":39.21 }

for key, value in world :


print(key + " -- " + str(value))

Output:
ValueError: too many values to unpack (expected 2)
Intermediate Python for Data Science

Dictionary for var in seq :


expression
!

" dictloop.py

world = { "afghanistan":30.55,
"albania":2.77,
"algeria":39.21 }

for key, value in world.items() :


print(key + " -- " + str(value))

Output:
algeria -- 39.21
afghanistan -- 30.55
albania -- 2.77
Intermediate Python for Data Science

Dictionary for var in seq :


expression
!

" 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

Numpy Arrays for var in seq :


expression
!

" 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

2D Numpy Arrays for var in seq :


expression
!

" 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

2D Numpy Arrays for var in seq :


expression
!

" 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)

Output: Output (cont):


1.73 65.4
1.68 59.2
1.71 63.6
1.89 88.4
1.79 68.7
Intermediate Python for Data Science

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

Loop Data Structures


Part 2
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

for, first try 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)

for val in brics :


print(val)

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)

for lab, row in brics.iterrows() :


print(lab)
print(row)

Output: Output (cont):


BR RU
country Brazil country Russia
capital Brasilia capital Moscow
area 8.516 area 17.1
population 200.4 population 143.5
Name: BR, dtype: object Name: RU, dtype: object
... IN ...
Intermediate Python for Data Science

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)

for lab, row in brics.iterrows() :


print(lab + ": " + row["capital"])

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)

for lab, row in brics.iterrows() :


brics.loc[lab, "name_length"] = len(row["country"])
print(brics)
Creating Series on every iteration

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!

You might also like