In [1]:
2 + 2
Out[1]:
4
In [8]:
myvar = "this is a string"
In [10]:
print(myvar)
this is not a string
In [9]:
myvar = "this is not a string"
In [12]:
myvar2 = 8
In [13]:
print(myvar2)
8
In [15]:
print("this is not a string")
this is not a string
In [16]:
print(myvar)
this is not a string
In [18]:
print(this is not a string)
  File "<ipython-input-18-fa8ece6e3fbd>", line 1
    print(this is not a string)
                             ^
SyntaxError: invalid syntax
In [23]:
temp_C = 50
temp_C * 9 / 5 + 32
temp_C + 20
# Prints only last line result
Out[23]:
70
In [25]:
temp_C = 50
temp_F = temp_C * 9 / 5 + 32
In [27]:
print(temp_F)  # Print a value that we previously assigned
122.0
In [30]:
# Checkpoint 1

x = 1
y = 2

# Swap the values of x and y (hint: use a third variable)

#x, y = y, x
z = x
a = y
x = a
y = z

print(x)
print(y)
2
1
In [31]:
chemist = "Linus Pauling"
In [32]:
print(chemist)
Linus Pauling
In [33]:
chemist[0:5]
Out[33]:
'Linus'
In [35]:
chemist[0]
Out[35]:
'L'
In [36]:
chemist[1]
Out[36]:
'i'
In [38]:
first_value = "initial"
second_value = first_value[1:5]
first_value = second_value[2]
print(first_value)
t
In [39]:
physicist = "Max Planck"
len(physicist)
Out[39]:
10
In [41]:
length = len(physicist)
physicist[length - 1]
Out[41]:
'k'
In [43]:
physicist[2:length]
Out[43]:
'x Planck'
In [44]:
physicist[2:]
Out[44]:
'x Planck'
In [45]:
physicist[:3]
Out[45]:
'Max'
In [46]:
type(temp_C)
Out[46]:
int
In [47]:
type(physicist)
Out[47]:
str
In [48]:
chemist + physicist
Out[48]:
'Linus PaulingMax Planck'
In [49]:
chemist + temp_C
------------------------------------------------------------
TypeError                  Traceback (most recent call last)
<ipython-input-49-dd27f9da2696> in <module>()
----> 1 chemist + temp_C

TypeError: must be str, not int
In [52]:
8 / 3
Out[52]:
2.6666666666666665
In [51]:
type(8 / 3)
Out[51]:
float
In [53]:
temp_C + (8 / 3)
Out[53]:
52.666666666666664
In [56]:
type(temp_F)
Out[56]:
float
In [57]:
"(" + str(temp_F) + ")"
Out[57]:
'(122.0)'
In [58]:
type(10)
Out[58]:
int
In [59]:
float(10)
Out[59]:
10.0
In [60]:
int(10.999)
Out[60]:
10
In [ ]:
# print, len, type, str, int, float
In [61]:
print()

In [62]:
print
Out[62]:
<function print>
In [63]:
max(1, 5, 3)
Out[63]:
5
In [64]:
help(max)
Help on built-in function max in module builtins:

max(...)
    max(iterable, *[, default=obj, key=func]) -> value
    max(arg1, arg2, *args, *[, key=func]) -> value
    
    With a single iterable argument, return its biggest item. The
    default keyword-only argument specifies an object to return if
    the provided iterable is empty.
    With two or more arguments, return the largest argument.

In [66]:
planck_length = len(physicist)
In [67]:
output = print("a string")
a string
In [68]:
print(output)
None
In [69]:
type(output)
Out[69]:
NoneType
In [70]:
mean(1, 4, 5)
------------------------------------------------------------
NameError                  Traceback (most recent call last)
<ipython-input-70-f7159d372e47> in <module>()
----> 1 mean(1, 4, 5)

NameError: name 'mean' is not defined
In [73]:
import math
In [74]:
dir(math)
Out[74]:
['__doc__',
 '__file__',
 '__loader__',
 '__name__',
 '__package__',
 '__spec__',
 'acos',
 'acosh',
 'asin',
 'asinh',
 'atan',
 'atan2',
 'atanh',
 'ceil',
 'copysign',
 'cos',
 'cosh',
 'degrees',
 'e',
 'erf',
 'erfc',
 'exp',
 'expm1',
 'fabs',
 'factorial',
 'floor',
 'fmod',
 'frexp',
 'fsum',
 'gamma',
 'gcd',
 'hypot',
 'inf',
 'isclose',
 'isfinite',
 'isinf',
 'isnan',
 'ldexp',
 'lgamma',
 'log',
 'log10',
 'log1p',
 'log2',
 'modf',
 'nan',
 'pi',
 'pow',
 'radians',
 'sin',
 'sinh',
 'sqrt',
 'tan',
 'tanh',
 'tau',
 'trunc']
In [76]:
import stat
In [77]:
htam = math
In [78]:
# Checkpoint

radiance = 1.0
radiance = max(2.1, 2.0 + min(radiance, radiance - 0.5))

# What is the order of operations?
# What is the final value of radiance?
print(radiance)
2.5
In [82]:
import pandas
In [84]:
europe = pandas.read_csv("gapminder_gdp_europe.csv")
In [85]:
europe.mean()
Out[85]:
gdpPercap_1952     5661.057435
gdpPercap_1957     6963.012816
gdpPercap_1962     8365.486814
gdpPercap_1967    10143.823757
gdpPercap_1972    12479.575246
gdpPercap_1977    14283.979110
gdpPercap_1982    15617.896551
gdpPercap_1987    17214.310727
gdpPercap_1992    17061.568084
gdpPercap_1997    19076.781802
gdpPercap_2002    21711.732422
gdpPercap_2007    25054.481636
dtype: float64
In [86]:
europe.std()
Out[86]:
gdpPercap_1952     3114.060493
gdpPercap_1957     3677.950146
gdpPercap_1962     4199.193906
gdpPercap_1967     4724.983889
gdpPercap_1972     5509.691411
gdpPercap_1977     5874.464896
gdpPercap_1982     6453.234827
gdpPercap_1987     7482.957960
gdpPercap_1992     9109.804361
gdpPercap_1997    10065.457716
gdpPercap_2002    11197.355517
gdpPercap_2007    11800.339811
dtype: float64
In [ ]:
pandas.read_csv
In [88]:
europe = pandas.read_csv("gapminder_gdp_europe.csv",
                         index_col="country")
europe.head()
Out[88]:
gdpPercap_1952 gdpPercap_1957 gdpPercap_1962 gdpPercap_1967 gdpPercap_1972 gdpPercap_1977 gdpPercap_1982 gdpPercap_1987 gdpPercap_1992 gdpPercap_1997 gdpPercap_2002 gdpPercap_2007
country
Albania 1601.056136 1942.284244 2312.888958 2760.196931 3313.422188 3533.003910 3630.880722 3738.932735 2497.437901 3193.054604 4604.211737 5937.029526
Austria 6137.076492 8842.598030 10750.721110 12834.602400 16661.625600 19749.422300 21597.083620 23687.826070 27042.018680 29095.920660 32417.607690 36126.492700
Belgium 8343.105127 9714.960623 10991.206760 13149.041190 16672.143560 19117.974480 20979.845890 22525.563080 25575.570690 27561.196630 30485.883750 33692.605080
Bosnia and Herzegovina 973.533195 1353.989176 1709.683679 2172.352423 2860.169750 3528.481305 4126.613157 4314.114757 2546.781445 4766.355904 6018.975239 7446.298803
Bulgaria 2444.286648 3008.670727 4254.337839 5577.002800 6597.494398 7612.240438 8224.191647 8239.854824 6302.623438 5970.388760 7696.777725 10680.792820
In [90]:
europe.gdpPercap_1952.Austria
Out[90]:
6137.0764920000001
In [94]:
Austria
------------------------------------------------------------
NameError                  Traceback (most recent call last)
<ipython-input-94-9da0dddce247> in <module>()
----> 1 Austria

NameError: name 'Austria' is not defined
In [96]:
europe.loc['Austria']
Out[96]:
gdpPercap_1952     6137.076492
gdpPercap_1957     8842.598030
gdpPercap_1962    10750.721110
gdpPercap_1967    12834.602400
gdpPercap_1972    16661.625600
gdpPercap_1977    19749.422300
gdpPercap_1982    21597.083620
gdpPercap_1987    23687.826070
gdpPercap_1992    27042.018680
gdpPercap_1997    29095.920660
gdpPercap_2002    32417.607690
gdpPercap_2007    36126.492700
Name: Austria, dtype: float64
In [97]:
europe.loc['Austria'].mean()
Out[97]:
20411.916279333334
In [98]:
europe.columns
Out[98]:
Index(['gdpPercap_1952', 'gdpPercap_1957', 'gdpPercap_1962', 'gdpPercap_1967',
       'gdpPercap_1972', 'gdpPercap_1977', 'gdpPercap_1982', 'gdpPercap_1987',
       'gdpPercap_1992', 'gdpPercap_1997', 'gdpPercap_2002', 'gdpPercap_2007'],
      dtype='object')
In [99]:
europe.gdpPercap_1952 + europe.gdpPercap_1957
Out[99]:
country
Albania                    3543.340380
Austria                   14979.674522
Belgium                   18058.065750
Bosnia and Herzegovina     2327.522371
Bulgaria                   5452.957375
Croatia                    7457.468137
Czech Republic            15132.484168
Denmark                   20792.044595
Finland                   13969.934457
France                    15692.644225
Germany                   17331.941043
Greece                     8446.989956
Hungary                   11303.853827
Iceland                   16511.689840
Ireland                   10809.358200
Italy                     11180.060387
Montenegro                 6329.845504
Netherlands               20217.765298
Norway                    21749.394760
Poland                     8763.582718
Portugal                   6842.891610
Romania                    7087.983411
Serbia                     8562.550339
Slovak Republic           11167.922084
Slovenia                  10077.318370
Spain                      8398.837152
Sweden                    18439.722888
Switzerland               32643.722480
Turkey                     4187.855237
United Kingdom            21262.686437
dtype: float64
In [100]:
gdp_1952_and_1957 = europe['gdpPercap_1952'] + europe['gdpPercap_1957']
In [102]:
gdp_1952_and_1957.mean()
Out[102]:
12624.070250693332
In [105]:
import matplotlib.pyplot
In [107]:
%matplotlib inline
In [108]:
matplotlib.pyplot.hist(europe.gdpPercap_1952)
Out[108]:
(array([ 3.,  7.,  5.,  4.,  4.,  3.,  3.,  0.,  0.,  1.]),
 array([   973.5331948 ,   2349.60315032,   3725.67310584,   5101.74306136,
          6477.81301688,   7853.8829724 ,   9229.95292792,  10606.02288344,
         11982.09283896,  13358.16279448,  14734.23275   ]),
 <a list of 10 Patch objects>)
In [110]:
matplotlib.pyplot.hist(europe.gdpPercap_1952, bins=20)
Out[110]:
(array([ 2.,  1.,  2.,  5.,  3.,  2.,  2.,  2.,  3.,  1.,  2.,  1.,  1.,
         2.,  0.,  0.,  0.,  0.,  0.,  1.]),
 array([   973.5331948 ,   1661.56817256,   2349.60315032,   3037.63812808,
          3725.67310584,   4413.7080836 ,   5101.74306136,   5789.77803912,
          6477.81301688,   7165.84799464,   7853.8829724 ,   8541.91795016,
          9229.95292792,   9917.98790568,  10606.02288344,  11294.0578612 ,
         11982.09283896,  12670.12781672,  13358.16279448,  14046.19777224,
         14734.23275   ]),
 <a list of 20 Patch objects>)
In [111]:
europe.loc['Switzerland']
Out[111]:
gdpPercap_1952    14734.23275
gdpPercap_1957    17909.48973
gdpPercap_1962    20431.09270
gdpPercap_1967    22966.14432
gdpPercap_1972    27195.11304
gdpPercap_1977    26982.29052
gdpPercap_1982    28397.71512
gdpPercap_1987    30281.70459
gdpPercap_1992    31871.53030
gdpPercap_1997    32135.32301
gdpPercap_2002    34480.95771
gdpPercap_2007    37506.41907
Name: Switzerland, dtype: float64
In [112]:
import matplotlib.pyplot as plt
In [119]:
plt.hist(europe.gdpPercap_1952, bins=20, alpha=0.5, label='1952')
plt.hist(europe.gdpPercap_1957, bins=20, alpha=0.5, label='1957')
plt.xlabel('GDP Per-capita (US$2007)')
plt.ylabel('Count')
plt.legend()
plt.title("Economic shifts in post-war Europe")
print()

In [120]:
europe.loc['Switzerland']
Out[120]:
gdpPercap_1952    14734.23275
gdpPercap_1957    17909.48973
gdpPercap_1962    20431.09270
gdpPercap_1967    22966.14432
gdpPercap_1972    27195.11304
gdpPercap_1977    26982.29052
gdpPercap_1982    28397.71512
gdpPercap_1987    30281.70459
gdpPercap_1992    31871.53030
gdpPercap_1997    32135.32301
gdpPercap_2002    34480.95771
gdpPercap_2007    37506.41907
Name: Switzerland, dtype: float64
In [126]:
#years = [1952, 1957, 1962, 1967,
#         1972, 1977, 1982, 1987,
#         1992, 1997, 2002, 2007]

years = [int(year) for year in europe.columns.str[-4:]]
In [128]:
plt.scatter(years, europe.loc['Switzerland'])
Out[128]:
<matplotlib.collections.PathCollection at 0x10c2d9a20>
In [133]:
plt.plot(years, europe.T, label='Switzerland')
#plt.plot(years, europe.loc['France'], label='France')
print()