<jupyter_start><jupyter_text>Lecture 4 – Comparisons and Boolean Operators Data 94, Spring 2021  Review exercise – growth rates $$\text{FV} = \text{PV} (1 + i)^t$$<jupyter_code>PV = 1e4            # Scientific notation for 10000
FV = 47123
t = 10<jupyter_output><empty_output><jupyter_text>`NoneType` and `bool`  `NoneType`<jupyter_code>my_var = None
type(my_var)
# Nothing is displayed!
my_var
# But it can be printed.
print(my_var)<jupyter_output><empty_output><jupyter_text>Pay attention to what happens here.<jupyter_code>strange = print(15)
strange
print(strange)<jupyter_output><empty_output><jupyter_text>`bool`<jupyter_code># Can display multiple values in one line
True, False
type(True), type(False)
int(True), int(False)
# Equivalent to 3 + 1 - 0
3 + True - False<jupyter_output><empty_output><jupyter_text>Keywords<jupyter_code># Doesn't work
3 = 4
# Similarly, doesn't work
True = 14
# Works, but don't do it!
# Don't uncomment this, because it will ruin the demos later on.
# bool = "breaking the rules"
# bool<jupyter_output><empty_output><jupyter_text>Quick Check 1<jupyter_code># A = str('None') + str('00')
# B = True * 8 - float(False) * 15
# C = int(None) - 1<jupyter_output><empty_output><jupyter_text>Comparisons<jupyter_code># is age at least age_limit?
age_limit = 21
age = 17

age >= age_limit
# is password_guess equal to true_password?
true_password = '<PASSWORD>!'
password_guess = '<PASSWORD>!'

password_guess == true_password
3 == 3
'hello' != 'howdy'
-3 > -2
-3 < -2
'alpha' >= 'beta'
x = 5         # set x equal to 5
x == 5        # is x equal to 5?
y = x == 5
y<jupyter_output><empty_output><jupyter_text>Comparing different types<jupyter_code>17 == '17'
'zebra' != True
True == 1.0
5 > True
'alpha' >= 'beta'
'alpha' >= 5<jupyter_output><empty_output><jupyter_text>Floating point issues, revisited<jupyter_code>0.1 * 2 == 0.2
0.1 * 6
0.1 * 6 == 0.6
abs(0.1 * 6 - 0.6) < 0.0001<jupyter_output><empty_output><jupyter_text>String containment<jupyter_code>'berkeley' in 'uc berkeley'
'stanford' in 'uc berkeley'
'berkeley' in 'UC BERKELEY'<jupyter_output><empty_output><jupyter_text>Quick Check 2  Boolean operators<jupyter_code>year = 'junior'
units = 125
year_check = year == 'senior'
year_check
units_check = units >= 120
units_check
ready_to_grad = year_check and units_check
almost_ready = year_check or units_check
ready_to_grad
units_check
n = 12
(n % 2 == 0) and (n % 4 == 0)
(n % 2 == 0) and not (n % 5 == 0)
(n % 3 != 0) and (n % 4 != 0)
True and False and True and True
True or False or True or True
3 < 4 <= 5
3 < 4 > 2 < 11 > -1
3 < 4 < 2 > 11 > -1<jupyter_output><empty_output><jupyter_text>Quick Check 3  DemoThe first cell contains code that's mostly copied from last lecture. Ignore it once again!<jupyter_code>from datascience import *
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns
import numpy as np

data = Table.read_table('data/countries.csv')
data = data.relabeled('Country(or dependent territory)', 'Country') \
           .relabeled('% of world', '%') \
           .relabeled('Source(official or UN)', 'Source')
data = data.with_columns(
    'Country', data.apply(lambda s: s[:s.index('[')].lower() if '[' in s else s.lower(), 'Country'),
    'Population', data.apply(lambda i: int(i.replace(',', '')), 'Population'),
    '%', data.apply(lambda f: float(f.replace('%', '')), '%')
)

def first_letter(s):
    return s[0]

def last_letter(s):
    return s[-1]
data<jupyter_output><empty_output><jupyter_text>Below, assign `first_or_last` to a string containing a single lowercase letter.We'll look at the distribution of populations of countries whose names either begin or end with `first_or_last`.<jupyter_code>first_or_last = 'a'
relevant_countries = data.where(data.apply(
    
              # Focus on this part!
    lambda name: first_letter(name) == first_or_last or last_letter(name) == first_or_last
    
    
, 'Country')).sort('Population', descending = True)
relevant_countries
# Ignore everything except the last line!
plt.figure(figsize = (10, 7))
names = relevant_countries.column('Country')
pops = relevant_countries.column('Population')

if relevant_countries.num_rows > 15:
    names = names[:15]
    pops = pops[:15]

sns.barplot(x = pops, y = names, orient = 'h')

# Focus on this part!
plt.title('Populations of countries starting or ending with ' + first_or_last);<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13668410279736184, 'fraction_numerical': 0.030247896292926995, 'mean_word_length': 4.9033557046979865, 'pattern_counts': {'":': 0, '<': 63, '<?xml version=': 0, '>': 67, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '619207', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1717, 'n_tokens_neox': 1588, 'n_words': 439, 'path': 'lecture/lec04/lec04.ipynb', 'repo_name': 'surajrampure/data-94-sp21', 'size': '4399'}