PV = 1e4 # Scientific notation for 10000
FV = 47123
t = 10
i = (FV / PV) ** (1 / 10) - 1
i
0.16767852509215087
NoneType
and bool
¶NoneType
¶my_var = None
type(my_var)
NoneType
# Nothing is displayed!
my_var
# But it can be printed.
print(my_var)
None
Pay attention to what happens here.
strange = print(15)
15
strange
print(strange)
None
bool
# Can display multiple values in one line
True, False
(True, False)
type(True), type(False)
(bool, bool)
int(True), int(False)
(1, 0)
# Equivalent to 3 + 1 - 0
3 + True - False
4
# Doesn't work
3 = 4
File "/tmp/ipykernel_113/679380462.py", line 2 3 = 4 ^ SyntaxError: cannot assign to literal
# Similarly, doesn't work
True = 14
File "/tmp/ipykernel_113/2825449733.py", line 2 True = 14 ^ SyntaxError: cannot assign to True
# Works, but don't do it!
# Don't uncomment this, because it will ruin the demos later on.
# bool = "breaking the rules"
# bool
A = str('None') + str('00')
B = True * 8 - float(False) * 15
C = int(None) - 1
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) /tmp/ipykernel_113/3143381484.py in <module> ----> 1 C = int(None) - 1 TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'
# is age at least age_limit?
age_limit = 21
age = 17
age >= age_limit
False
# is password_guess equal to true_password?
true_password = 'qwerty1093x!'
password_guess = 'QWERTY1093x!'
password_guess == true_password
False
3 == 3
True
'hello' != 'howdy'
True
-3 > -2
False
-3 < -2
True
'alpha' >= 'beta'
False
x = 5 # set x equal to 5
x == 5 # is x equal to 5?
True
y = x == 5
y
True
17 == '17'
False
'zebra' != True
True
True == 1.0
True
5 > True
True
'alpha' >= 'beta'
False
'alpha' >= 5
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) /tmp/ipykernel_113/1172788117.py in <module> ----> 1 'alpha' >= 5 TypeError: '>=' not supported between instances of 'str' and 'int'
0.1 * 2 == 0.2
True
0.1 * 6
0.6000000000000001
0.1 * 6 == 0.6
False
abs(0.1 * 6 - 0.6) < 0.0001
True
'berkeley' in 'uc berkeley'
True
'stanford' in 'uc berkeley'
False
'berkeley' in 'UC BERKELEY'
False
passed = .5 * 30 + .5 * 100 >= 65
#passed
not_passed = passed == False
not_passed
False
year = 'junior'
units = 125
year_check = year == 'senior'
year_check
False
units_check = units >= 120
units_check
True
ready_to_grad = year_check and units_check
almost_ready = year_check or units_check
ready_to_grad
False
units_check
True
n = 12
(n % 2 == 0) and (n % 4 == 0)
True
(n % 2 == 0) and not (n % 5 == 0)
True
(n % 3 != 0) and (n % 4 != 0)
False
True and False and True and True
False
True or False or True or True
True
3 < 4 <= 5
True
3 < 4 > 2 < 11 > -1
True
3 < 4 < 2 > 11 > -1
False
temp = 67
raining = bool(0)
wear_socks = (not not raining) and (temp < 60)
wear_jacket = (not wear_socks) or (temp > 65)
wear_jacket = wear_jacket and wear_socks
wear_socks
False
wear_jacket
False
The first cell contains code that's mostly copied from last lecture. Ignore it once again!
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
Rank | Country | Population | % | Date | Source |
---|---|---|---|---|---|
1 | china | 1405936040 | 17.9 | 27 Dec 2020 | National population clock[3] |
2 | india | 1371366679 | 17.5 | 27 Dec 2020 | National population clock[4] |
3 | united states | 330888778 | 4.22 | 27 Dec 2020 | National population clock[5] |
4 | indonesia | 269603400 | 3.44 | 1 Jul 2020 | National annual projection[6] |
5 | pakistan | 220892331 | 2.82 | 1 Jul 2020 | UN Projection[2] |
6 | brazil | 212523810 | 2.71 | 27 Dec 2020 | National population clock[7] |
7 | nigeria | 206139587 | 2.63 | 1 Jul 2020 | UN Projection[2] |
8 | bangladesh | 169885314 | 2.17 | 27 Dec 2020 | National population clock[8] |
9 | russia | 146748590 | 1.87 | 1 Jan 2020 | National annual estimate[9] |
10 | mexico | 127792286 | 1.63 | 1 Jul 2020 | National annual projection[10] |
... (232 rows omitted)
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
.
first_or_last = 'a'
relevant_countries = data.where(data.apply(
# Focus on this part!
lambda name: first_letter(name) == first_or_last and last_letter(name) == first_or_last
, 'Country')).sort('Population', descending = True)
relevant_countries
Rank | Country | Population | % | Date | Source |
---|---|---|---|---|---|
31 | argentina | 45376763 | 0.579 | 1 Jul 2020 | National annual projection[31] |
32 | algeria | 43900000 | 0.56 | 1 Jan 2020 | National annual projection[32] |
45 | angola | 31127674 | 0.397 | 30 Jun 2020 | National annual projection[45] |
53 | australia | 25720882 | 0.328 | 27 Dec 2020 | National population clock[51] |
97 | austria | 8935112 | 0.114 | 1 Oct 2020 | National quarterly estimate[94] |
134 | armenia | 2967900 | 0.0379 | 30 Sep 2020 | National quarterly estimate[129] |
135 | albania | 2845955 | 0.0363 | 1 Jan 2020 | National annual estimate[130] |
– | abkhazia | 245424 | 0.00313 | 1 Jan 2020 | National annual estimate[174] |
184 | antigua and barbuda | 97895 | 0.00125 | 1 Jul 2020 | National annual projection[186] |
185 | andorra | 77543 | 0.000989 | 31 Dec 2019 | National annual estimate[188] |
# 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);