This jupyter notebook is adapted from the FREE Edx course of the same title.
height = 1.79
weight = 68.7
print(height)
1.79
height = 1.79
weight = 68.7
bmi = weight / height ** 2
print(bmi)
21.4412783621
# changing height or weight does not alter bmi
height = 1.34
print(bmi)
21.4412783621
str = "abc"
str
'abc'
type(bmi)
float
day_of_week = 5
type(day_of_week)
int
x = "body mass index"
type(x)
str
y = 'this works too'
type(y)
str
z = False
type(z)
bool
print("this is a variable", str)
('this is a variable', 'abc')
List
¶float
- real numbersint
- integer numbersstr
- string, textbool
- True, FalseIn [3]: height1 = 1.73
In [4]: height2 = 1.68
In [5]: height3 = 1.71
In [6]: height4 = 1.89
List
¶# Basic list
fam = [1.73, 1.68, 1.71, 1.89]
type(fam)
list
# List with multiple types
fam = ["liz", 1.73, "emma", 1.68, "mom", 1.71, "dad", 1.89]
type(fam)
list
# List of lists
fam2 = [["liz", 1.73], ["emma", 1.68], ["mom", 1.71], ["dad", 1.89]]
type(fam)
list
Zero-based indexing:
fam[3]
1.68
fam[6]
'dad'
fam[-1]
1.89
fam[-2]
'dad'
[ start (inclusive) : end (exclusive) ]
fam[3:5]
[1.68, 'mom']
fam[1:4]
[1.73, 'emma', 1.68]
fam[:4]
['liz', 1.73, 'emma', 1.68]
fam[5:]
[1.71, 'dad', 1.89]
fam[7] = 1.86
fam
['liz', 1.73, 'emma', 1.68, 'mom', 1.71, 'dad', 1.86]
fam[0:2] = ["lisa", 1.74]
fam
['lisa', 1.74, 'emma', 1.68, 'mom', 1.71, 'dad', 1.86]
fam_ext = fam + ["me", 1.79]
fam_ext
['lisa', 1.74, 'emma', 1.68, 'mom', 1.71, 'dad', 1.86, 'me', 1.79]
del(fam[2])
fam
['lisa', 1.74, 1.68, 'mom', 1.71, 'dad', 1.86]
del(fam[2])
fam
['lisa', 1.74, 'mom', 1.71, 'dad', 1.86]
fam.append('test')
fam
['liz', 1.73, 'emma', 1.68, 'mom', 1.71, 'dad', 1.89, 'test']
# Copy by reference (change the copy, change the original)
x = ["a", "b", "c"]
y = x
y[1] = "z"
print(x)
print(y)
['a', 'z', 'c'] ['a', 'z', 'c']
# Copy by value (change the copy, no change to the original)
x = ["a", "b", "c"]
y = x[:] # or y = list(x)
y[1] = "z"
print(x)
print(y)
['a', 'b', 'c'] ['a', 'z', 'c']
fam = [1.73, 1.68, 1.71, 1.89]
tallest = max(fam)
print(tallest)
str_l = [1.73, 1.68, 1.71, 1.89,'ab','e']
print(max(str_l))
1.89 e
Round floating point number.
help(round)
Help on built-in function round in module __builtin__: round(...) round(number[, ndigits]) -> floating point number Round a number to a given precision in decimal digits (default 0 digits). This always returns a floating point number. Precision may be negative.
round(1.68,1)
1.7
round(1.68)
2.0
fam = ['liz', 1.73, 'emma', 1.68, 'mom', 1.71, 'dad', 1.89]
fam.index("mom")
4
fam.count(1.73)
1
str
Methods¶sister = "liz "
sister
'liz '
sister.capitalize()
'Liz '
sister.replace("z", "sa")
'lisa'
sister.index("z")
2
fam.append("me")
fam
['liz', 1.73, 'emma', 1.68, 'mom', 1.71, 'dad', 1.89, 'me']
fam.append("1.79")
fam
['liz', 1.73, 'emma', 1.68, 'mom', 1.71, 'dad', 1.89, 'me', '1.79']
type(fam)
fam.index("dad")
import numpy as np
np.array([1, 2, 3])
array([1, 2, 3])
numpy.array([1, 2, 3])
array([1, 2, 3])
import numpy as np
np.array([1, 2, 3])
array([1, 2, 3])
from numpy import array
array([1, 2, 3])
array([1, 2, 3])
height = [1.73, 1.68, 1.71, 1.89, 1.79]
height
[1.73, 1.68, 1.71, 1.89, 1.79]
weight = [65.4, 59.2, 63.6, 88.4, 68.7]
weight
[65.4, 59.2, 63.6, 88.4, 68.7]
weight / height ** 2
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-110-6a4c0c70e3b9> in <module>() ----> 1 weight / height ** 2 TypeError: unsupported operand type(s) for ** or pow(): 'list' and 'int'
import numpy as np
np_height = np.array(height)
np_height
array([1.73, 1.68, 1.71, 1.89, 1.79])
np_weight = np.array(weight)
np_weight
array([65.4, 59.2, 63.6, 88.4, 68.7])
# Element-wise calculations
bmi = np_weight / np_height ** 2
bmi
array([21.85171573, 20.97505669, 21.75028214, 24.7473475 , 21.44127836])
np.array([1.0, "is", True])
array(['1.0', 'is', 'True'], dtype='|S32')
python_list = [1, 2, 3]
python_list + python_list
[1, 2, 3, 1, 2, 3]
numpy_array = np.array([1, 2, 3])
numpy_array + numpy_array
array([2, 4, 6])
np.add(numpy_array,numpy_array)
array([2, 4, 6])
bmi
array([21.85171573, 20.97505669, 21.75028214, 24.7473475 , 21.44127836])
bmi[1]
20.97505668934241
bmi > 23
array([False, False, False, True, False])
bmi[bmi > 23]
array([24.7473475])
import numpy as np
np_height = np.array([1.73, 1.68, 1.71, 1.89, 1.79])
np_weight = np.array([65.4, 59.2, 63.6, 88.4, 68.7])
type(np_height)
numpy.ndarray
type(np_weight)
numpy.ndarray
np_2d = np.array([[1.73, 1.68, 1.71, 1.89, 1.79],[65.4, 59.2, 63.6, 88.4, 68.7]])
np_2d
array([[ 1.73, 1.68, 1.71, 1.89, 1.79], [65.4 , 59.2 , 63.6 , 88.4 , 68.7 ]])
# 2 rows, 5 columns
np_2d.shape
(2, 5)
# numpy arrays must have a single type
np.array([[1.73, 1.68, 1.71, 1.89, 1.79],
[65.4, 59.2, 63.6, 88.4, "68.7"]])
array([['1.73', '1.68', '1.71', '1.89', '1.79'], ['65.4', '59.2', '63.6', '88.4', '68.7']], dtype='|S32')
# Defining
diag = np.diag([1,1,1])
print(diag)
[[1 0 0] [0 1 0] [0 0 1]]
# Get the first row
np_2d[0]
array([1.73, 1.68, 1.71, 1.89, 1.79])
# First row, third column
np_2d[0][2]
1.71
# First row, third column
In [12]: np_2d[0,2]
1.71
# All rows, first through second columns
np_2d[:,1:3]
array([[ 1.68, 1.71], [59.2 , 63.6 ]])
# Second row, all columns
np_2d[1,:]
array([65.4, 59.2, 63.6, 88.4, 68.7])
import numpy as np
# Generatate 5000 normally distributed random variables
height = np.round(np.random.normal(1.75, 0.20, 5000), 2) # mean = 1.75, std dev = 0.2
weight = np.round(np.random.normal(60.32, 15, 5000), 2) # mean = 60.32, std dev = 15
np_city = np.column_stack((height, weight))
np_city
array([[ 1.6 , 35.86], [ 1.61, 54.38], [ 2.04, 76.64], ..., [ 1.65, 51.07], [ 2.1 , 57.08], [ 1.67, 58.33]])
np.mean(np_city[:,0])
1.7477559999999999
np.median(np_city[:,0])
1.74
np.std(np_city[:,0])
0.1981012984914536
import matplotlib.pyplot as plt
year = [1950, 1970, 1990, 2010]
pop = [2.519, 3.692, 5.263, 6.972]
plt.plot(year, pop)
plt.show()
plt.scatter(year, pop)
plt.show()
import matplotlib.pyplot as plt
help(plt.hist)
Help on function hist in module matplotlib.pyplot: hist(x, bins=None, range=None, density=None, weights=None, cumulative=False, bottom=None, histtype=u'bar', align=u'mid', orientation=u'vertical', rwidth=None, log=False, color=None, label=None, stacked=False, normed=None, hold=None, data=None, **kwargs) Plot a histogram. Compute and draw the histogram of *x*. The return value is a tuple (*n*, *bins*, *patches*) or ([*n0*, *n1*, ...], *bins*, [*patches0*, *patches1*,...]) if the input contains multiple data. Multiple data can be provided via *x* as a list of datasets of potentially different length ([*x0*, *x1*, ...]), or as a 2-D ndarray in which each column is a dataset. Note that the ndarray form is transposed relative to the list form. Masked arrays are not supported at present. Parameters ---------- x : (n,) array or sequence of (n,) arrays Input values, this takes either a single array or a sequency of arrays which are not required to be of the same length bins : integer or array_like or 'auto', optional If an integer is given, ``bins + 1`` bin edges are returned, consistently with :func:`numpy.histogram` for numpy version >= 1.3. Unequally spaced bins are supported if *bins* is a sequence. If Numpy 1.11 is installed, may also be ``'auto'``. Default is taken from the rcParam ``hist.bins``. range : tuple or None, optional The lower and upper range of the bins. Lower and upper outliers are ignored. If not provided, *range* is ``(x.min(), x.max())``. Range has no effect if *bins* is a sequence. If *bins* is a sequence or *range* is specified, autoscaling is based on the specified bin range instead of the range of x. Default is ``None`` density : boolean, optional If ``True``, the first element of the return tuple will be the counts normalized to form a probability density, i.e., the area (or integral) under the histogram will sum to 1. This is achieved by dividing the count by the number of observations times the bin width and not dividing by the total number of observations. If *stacked* is also ``True``, the sum of the histograms is normalized to 1. Default is ``None`` for both *normed* and *density*. If either is set, then that value will be used. If neither are set, then the args will be treated as ``False``. If both *density* and *normed* are set an error is raised. weights : (n, ) array_like or None, optional An array of weights, of the same shape as *x*. Each value in *x* only contributes its associated weight towards the bin count (instead of 1). If *normed* or *density* is ``True``, the weights are normalized, so that the integral of the density over the range remains 1. Default is ``None`` cumulative : boolean, optional If ``True``, then a histogram is computed where each bin gives the counts in that bin plus all bins for smaller values. The last bin gives the total number of datapoints. If *normed* or *density* is also ``True`` then the histogram is normalized such that the last bin equals 1. If *cumulative* evaluates to less than 0 (e.g., -1), the direction of accumulation is reversed. In this case, if *normed* and/or *density* is also ``True``, then the histogram is normalized such that the first bin equals 1. Default is ``False`` bottom : array_like, scalar, or None Location of the bottom baseline of each bin. If a scalar, the base line for each bin is shifted by the same amount. If an array, each bin is shifted independently and the length of bottom must match the number of bins. If None, defaults to 0. Default is ``None`` histtype : {'bar', 'barstacked', 'step', 'stepfilled'}, optional The type of histogram to draw. - 'bar' is a traditional bar-type histogram. If multiple data are given the bars are aranged side by side. - 'barstacked' is a bar-type histogram where multiple data are stacked on top of each other. - 'step' generates a lineplot that is by default unfilled. - 'stepfilled' generates a lineplot that is by default filled. Default is 'bar' align : {'left', 'mid', 'right'}, optional Controls how the histogram is plotted. - 'left': bars are centered on the left bin edges. - 'mid': bars are centered between the bin edges. - 'right': bars are centered on the right bin edges. Default is 'mid' orientation : {'horizontal', 'vertical'}, optional If 'horizontal', `~matplotlib.pyplot.barh` will be used for bar-type histograms and the *bottom* kwarg will be the left edges. rwidth : scalar or None, optional The relative width of the bars as a fraction of the bin width. If ``None``, automatically compute the width. Ignored if *histtype* is 'step' or 'stepfilled'. Default is ``None`` log : boolean, optional If ``True``, the histogram axis will be set to a log scale. If *log* is ``True`` and *x* is a 1D array, empty bins will be filtered out and only the non-empty ``(n, bins, patches)`` will be returned. Default is ``False`` color : color or array_like of colors or None, optional Color spec or sequence of color specs, one per dataset. Default (``None``) uses the standard line color sequence. Default is ``None`` label : string or None, optional String, or sequence of strings to match multiple datasets. Bar charts yield multiple patches per dataset, but only the first gets the label, so that the legend command will work as expected. default is ``None`` stacked : boolean, optional If ``True``, multiple data are stacked on top of each other If ``False`` multiple data are aranged side by side if histtype is 'bar' or on top of each other if histtype is 'step' Default is ``False`` Returns ------- n : array or list of arrays The values of the histogram bins. See *normed* or *density* and *weights* for a description of the possible semantics. If input *x* is an array, then this is an array of length *nbins*. If input is a sequence arrays ``[data1, data2,..]``, then this is a list of arrays with the values of the histograms for each of the arrays in the same order. bins : array The edges of the bins. Length nbins + 1 (nbins left edges and right edge of last bin). Always a single array even when multiple data sets are passed in. patches : list or list of lists Silent list of individual patches used to create the histogram or list of such list if multiple input datasets. Other Parameters ---------------- **kwargs : `~matplotlib.patches.Patch` properties See also -------- hist2d : 2D histograms Notes ----- Until numpy release 1.5, the underlying numpy histogram function was incorrect with ``normed=True`` if bin sizes were unequal. MPL inherited that error. It is now corrected within MPL when using earlier numpy versions. .. note:: In addition to the above described arguments, this function can take a **data** keyword argument. If such a **data** argument is given, the following arguments are replaced by **data[<arg>]**: * All arguments with the following names: 'weights', 'x'.
values = [0,0.6,1.4,1.6,2.2,2.5,2.6,3.2,3.5,3.9,4.2,6]
plt.hist(values, bins = 3)
plt.show()
year = np.linspace(1950.,2100.,num=50)
K = 11.;
P0 = 2.6;
r = 0.03;
population = K*P0*np.exp(r*(year-year[0])) / (K + P0*(np.exp(r*(year-year[0]))-1.))
plt.plot(year, population)
plt.show()
plt.plot(year, population)
plt.xlabel('Year')
plt.ylabel('Population')
plt.title('World Population Projections')
plt.yticks([0,2,4,6,8,10])
plt.xlim(1950, 2100)
plt.ylim(0, 11)
plt.show()
plt.plot(year, population)
plt.xlabel('Year')
plt.ylabel('Population')
plt.title('World Population Projections')
plt.yticks([0,2,4,6,8,10],['0','2B','4B','6B','8B','10B'])
plt.xlim(1950, 2100)
plt.ylim(0, 11)
plt.show()
2 < 3
True
2 == 3
False
x = 2
y = 3
x < y
True
x == y
False
operator | meaning |
---|---|
< | strictly less than |
<= | less than or equal |
> | strictly greater than |
>= | greater than or equal |
== | equal |
!= | not equal |
print(True and True)
print(True and False)
print(False and True)
print(False and False)
True False False False
print(True or True)
print(True or False)
print(False or True)
print(False or False)
True True True False
print(not True)
print(not False)
False True
if condition :
expression
Note the indentation of expression and the colon after the condition.
z = 4
if z % 2 == 0 :
print("z is even")
z is even
z = 5
if z % 2 == 0 :
print("z is even")
else :
print("z is odd")
z is odd
z = 3
if z % 2 == 0 :
print("z is divisible by 2")
elif z % 3 == 0 :
print("z is divisible by 3")
else :
print("z is neither divisible by 2 nor by 3")
z is divisible by 3