PV = 1e4 # Scientific notation for 10000
FV = 47123
t = 10
NoneType
and bool
¶NoneType
¶my_var = None
type(my_var)
# Nothing is displayed!
my_var
# But it can be printed.
print(my_var)
Pay attention to what happens here.
strange = print(15)
strange
print(strange)
bool
# 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
# 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
# A = str('None') + str('00')
# B = True * 8 - float(False) * 15
# C = int(None) - 1
# is age at least age_limit?
age_limit = 21
age = 17
age >= age_limit
# is password_guess equal to true_password?
true_password = 'qwerty1093x!'
password_guess = 'QWERTY1093x!'
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
17 == '17'
'zebra' != True
True == 1.0
5 > True
'alpha' >= 'beta'
'alpha' >= 5
0.1 * 2 == 0.2
0.1 * 6
0.1 * 6 == 0.6
abs(0.1 * 6 - 0.6) < 0.0001
'berkeley' in 'uc berkeley'
'stanford' in 'uc berkeley'
'berkeley' in 'UC BERKELEY'
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
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
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 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);