Intermediate Python ch3 Slides PDF
Intermediate Python ch3 Slides PDF
Comparison Operators
Intermediate Python for Data Science
Numpy Recap
In [1]: import numpy as np
In [2]: np_height = np.array([1.73, 1.68, 1.71, 1.89, 1.79])
In [3]: np_weight = np.array([65.4, 59.2, 63.6, 88.4, 68.7])
In [4]: bmi = np_weight / np_height ** 2
In [5]: bmi
Out[5]: array([ 21.852, 20.975, 21.75 , 24.747, 21.441])
Numeric Comparisons
In [8]: 2 < 3
Out[8]: True
In [9]: 2 == 3
Out[9]: False
In [10]: 2 <= 3
Out[10]: True
In [11]: 3 <= 3
Out[11]: True
In [12]: x = 2
In [13]: y = 3
In [14]: x < y
Out[14]: True
Intermediate Python for Data Science
Other Comparisons
In [15]: "carl" < "chris"
Out[15]: True
In [18]: bmi
Out[18]: array([ 21.852, 20.975, 21.75 , 24.747, 21.441])
Comparators
< strictly less than
== equal
!= not equal
INTERMEDIATE PYTHON FOR DATA SCIENCE
Let’s practice!
INTERMEDIATE PYTHON FOR DATA SCIENCE
Boolean Operators
Intermediate Python for Data Science
Boolean Operators
● and
● or
● not
Intermediate Python for Data Science
and
In [1]: True and True
Out[1]: True
In [5]: x = 12
True True
In [6]: x > 5 and x < 15
Out[6]: True
Intermediate Python for Data Science
or
In [7]: True or True
Out[7]: True
In [11]: y = 5
True False
In [12]: y < 7 or y > 13
Out[12]: True
Intermediate Python for Data Science
not
In [13]: not True
Out[13]: False
Numpy
In [19]: bmi # calculation of bmi left out
Out[19]: array([ 21.852, 20.975, 21.75 , 24.747, 21.441])
Numpy logical_and()
logical_or()
logical_not()
In [19]: bmi # calculation of bmi left out
Out[19]: array([ 21.852, 20.975, 21.75 , 24.747, 21.441])
Let’s practice!
INTERMEDIATE PYTHON FOR DATA SCIENCE
Overview
● Comparison Operators
● <, >, >=, <=, ==, !=
● Boolean Operators
● and, or, not
● Conditional Statements
● if, else, elif
Intermediate Python for Data Science
if if condition :
expression
"
! control.py
z = 4 True
if z % 2 == 0 :
print("z is even")
Output:
z is even
Intermediate Python for Data Science
if if condition :
expression
"
z = 4 True
if z % 2 == 0 :
print("z is even")
Output:
z is even
Intermediate Python for Data Science
if if condition :
expression
"
! control.py
z = 4
if z % 2 == 0 :
print("checking " + str(z))
print("z is even")
Output:
checking 4
z is even
Intermediate Python for Data Science
if if condition :
expression
"
! control.py
z = 5 False
if z % 2 == 0 :
print("checking " + str(z))
print("z is even") Not executed
Output:
Intermediate Python for Data Science
else if condition :
expression
"
else :
! control.py expression
z = 5 False
if z % 2 == 0 :
print("z is even")
else :
print("z is odd")
Output:
z is odd
Intermediate Python for Data Science
elif if condition :
expression
"
elif condition :
! control.py expression
else :
z = 3
expression
if z % 2 == 0 : False
print("z is divisible by 2")
elif z % 3 == 0 : True
print("z is divisible by 3")
else :
print("z is neither divisible by 2 nor by 3")
Output:
z is divisible by 3
Intermediate Python for Data Science
elif if condition :
expression
"
elif condition :
! control.py expression
else :
z = 6
expression
if z % 2 == 0 : True
print("z is divisible by 2")
elif z % 3 == 0 : Never reached
print("z is divisible by 3")
else :
print("z is neither divisible by 2 nor by 3")
Output:
z is divisible by 2
INTERMEDIATE PYTHON FOR DATA SCIENCE
Let’s practice!
INTERMEDIATE PYTHON FOR DATA SCIENCE
brics
In [1]: import pandas as pd
In [3]: brics
Out[3]:
country capital area population
BR Brazil Brasilia 8.516 200.40
RU Russia Moscow 17.100 143.50
IN India New Delhi 3.286 1252.00
CH China Beijing 9.597 1357.00
SA South Africa Pretoria 1.221 52.98
Intermediate Python for Data Science
Goal 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
● 3 steps
● Select the area column
● Do comparison on area column
● Use result to select countries
Intermediate Python for Data Science
Out[4]: Alternatives:
BR 8.516
RU 17.100 brics.loc[:,"area"]
IN 3.286
CH 9.597 brics.iloc[:,2]
SA 1.221
Name: area, dtype: float64
Need Pandas Series
Intermediate Python for Data Science
Step 2: Compare 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
In [4]: brics["area"] SA South Africa Pretoria 1.221 52.98
Out[4]:
BR 8.516
RU 17.100
IN 3.286
CH 9.597
SA 1.221
Name: area, dtype: float64
Step 3: Subset DF 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
In [7]: is_huge SA South Africa Pretoria 1.221 52.98
Out[7]:
BR True
RU True
IN False
CH True
SA False
Name: area, dtype: bool
In [8]: brics[is_huge]
Out[8]:
country capital area population
BR Brazil Brasilia 8.516 200.4
RU Russia Moscow 17.100 143.5
CH China Beijing 9.597 1357.0
Intermediate Python for Data Science
Summary 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
In [9]: is_huge = brics["area"] > 8 SA South Africa Pretoria 1.221 52.98
In [10]: brics[is_huge]
Out[10]:
country capital area population
BR Brazil Brasilia 8.516 200.4
RU Russia Moscow 17.100 143.5
CH China Beijing 9.597 1357.0
Boolean operators 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
In [12]: import numpy as np SA South Africa Pretoria 1.221 52.98
Let’s practice!