\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0msin\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mNameError\u001b[0m: name 'sin' is not defined"
]
}
],
"source": [
"sin(0)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"\n",
"\n",
"
Good news: There are external libraries (modules) suited for almost any task. \n",
"\n",
"\n",
"
\n",
"
"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"import math"
]
},
{
"cell_type": "code",
"execution_count": 66,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['__doc__',\n",
" '__file__',\n",
" '__loader__',\n",
" '__name__',\n",
" '__package__',\n",
" '__spec__',\n",
" 'acos',\n",
" 'acosh',\n",
" 'asin',\n",
" 'asinh',\n",
" 'atan',\n",
" 'atan2',\n",
" 'atanh',\n",
" 'ceil',\n",
" 'copysign',\n",
" 'cos',\n",
" 'cosh',\n",
" 'degrees',\n",
" 'e',\n",
" 'erf',\n",
" 'erfc',\n",
" 'exp',\n",
" 'expm1',\n",
" 'fabs',\n",
" 'factorial',\n",
" 'floor',\n",
" 'fmod',\n",
" 'frexp',\n",
" 'fsum',\n",
" 'gamma',\n",
" 'gcd',\n",
" 'hypot',\n",
" 'inf',\n",
" 'isclose',\n",
" 'isfinite',\n",
" 'isinf',\n",
" 'isnan',\n",
" 'ldexp',\n",
" 'lgamma',\n",
" 'log',\n",
" 'log10',\n",
" 'log1p',\n",
" 'log2',\n",
" 'modf',\n",
" 'nan',\n",
" 'pi',\n",
" 'pow',\n",
" 'radians',\n",
" 'remainder',\n",
" 'sin',\n",
" 'sinh',\n",
" 'sqrt',\n",
" 'tan',\n",
" 'tanh',\n",
" 'tau',\n",
" 'trunc']"
]
},
"execution_count": 66,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dir(math)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Help on module math:\n",
"\n",
"NAME\n",
" math\n",
"\n",
"MODULE REFERENCE\n",
" https://docs.python.org/3.7/library/math\n",
" \n",
" The following documentation is automatically generated from the Python\n",
" source files. It may be incomplete, incorrect or include features that\n",
" are considered implementation detail and may vary between Python\n",
" implementations. When in doubt, consult the module reference at the\n",
" location listed above.\n",
"\n",
"DESCRIPTION\n",
" This module is always available. It provides access to the\n",
" mathematical functions defined by the C standard.\n",
"\n",
"FUNCTIONS\n",
" acos(x, /)\n",
" Return the arc cosine (measured in radians) of x.\n",
" \n",
" acosh(x, /)\n",
" Return the inverse hyperbolic cosine of x.\n",
" \n",
" asin(x, /)\n",
" Return the arc sine (measured in radians) of x.\n",
" \n",
" asinh(x, /)\n",
" Return the inverse hyperbolic sine of x.\n",
" \n",
" atan(x, /)\n",
" Return the arc tangent (measured in radians) of x.\n",
" \n",
" atan2(y, x, /)\n",
" Return the arc tangent (measured in radians) of y/x.\n",
" \n",
" Unlike atan(y/x), the signs of both x and y are considered.\n",
" \n",
" atanh(x, /)\n",
" Return the inverse hyperbolic tangent of x.\n",
" \n",
" ceil(x, /)\n",
" Return the ceiling of x as an Integral.\n",
" \n",
" This is the smallest integer >= x.\n",
" \n",
" copysign(x, y, /)\n",
" Return a float with the magnitude (absolute value) of x but the sign of y.\n",
" \n",
" On platforms that support signed zeros, copysign(1.0, -0.0)\n",
" returns -1.0.\n",
" \n",
" cos(x, /)\n",
" Return the cosine of x (measured in radians).\n",
" \n",
" cosh(x, /)\n",
" Return the hyperbolic cosine of x.\n",
" \n",
" degrees(x, /)\n",
" Convert angle x from radians to degrees.\n",
" \n",
" erf(x, /)\n",
" Error function at x.\n",
" \n",
" erfc(x, /)\n",
" Complementary error function at x.\n",
" \n",
" exp(x, /)\n",
" Return e raised to the power of x.\n",
" \n",
" expm1(x, /)\n",
" Return exp(x)-1.\n",
" \n",
" This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.\n",
" \n",
" fabs(x, /)\n",
" Return the absolute value of the float x.\n",
" \n",
" factorial(x, /)\n",
" Find x!.\n",
" \n",
" Raise a ValueError if x is negative or non-integral.\n",
" \n",
" floor(x, /)\n",
" Return the floor of x as an Integral.\n",
" \n",
" This is the largest integer <= x.\n",
" \n",
" fmod(x, y, /)\n",
" Return fmod(x, y), according to platform C.\n",
" \n",
" x % y may differ.\n",
" \n",
" frexp(x, /)\n",
" Return the mantissa and exponent of x, as pair (m, e).\n",
" \n",
" m is a float and e is an int, such that x = m * 2.**e.\n",
" If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.\n",
" \n",
" fsum(seq, /)\n",
" Return an accurate floating point sum of values in the iterable seq.\n",
" \n",
" Assumes IEEE-754 floating point arithmetic.\n",
" \n",
" gamma(x, /)\n",
" Gamma function at x.\n",
" \n",
" gcd(x, y, /)\n",
" greatest common divisor of x and y\n",
" \n",
" hypot(x, y, /)\n",
" Return the Euclidean distance, sqrt(x*x + y*y).\n",
" \n",
" isclose(a, b, *, rel_tol=1e-09, abs_tol=0.0)\n",
" Determine whether two floating point numbers are close in value.\n",
" \n",
" rel_tol\n",
" maximum difference for being considered \"close\", relative to the\n",
" magnitude of the input values\n",
" abs_tol\n",
" maximum difference for being considered \"close\", regardless of the\n",
" magnitude of the input values\n",
" \n",
" Return True if a is close in value to b, and False otherwise.\n",
" \n",
" For the values to be considered close, the difference between them\n",
" must be smaller than at least one of the tolerances.\n",
" \n",
" -inf, inf and NaN behave similarly to the IEEE 754 Standard. That\n",
" is, NaN is not close to anything, even itself. inf and -inf are\n",
" only close to themselves.\n",
" \n",
" isfinite(x, /)\n",
" Return True if x is neither an infinity nor a NaN, and False otherwise.\n",
" \n",
" isinf(x, /)\n",
" Return True if x is a positive or negative infinity, and False otherwise.\n",
" \n",
" isnan(x, /)\n",
" Return True if x is a NaN (not a number), and False otherwise.\n",
" \n",
" ldexp(x, i, /)\n",
" Return x * (2**i).\n",
" \n",
" This is essentially the inverse of frexp().\n",
" \n",
" lgamma(x, /)\n",
" Natural logarithm of absolute value of Gamma function at x.\n",
" \n",
" log(...)\n",
" log(x, [base=math.e])\n",
" Return the logarithm of x to the given base.\n",
" \n",
" If the base not specified, returns the natural logarithm (base e) of x.\n",
" \n",
" log10(x, /)\n",
" Return the base 10 logarithm of x.\n",
" \n",
" log1p(x, /)\n",
" Return the natural logarithm of 1+x (base e).\n",
" \n",
" The result is computed in a way which is accurate for x near zero.\n",
" \n",
" log2(x, /)\n",
" Return the base 2 logarithm of x.\n",
" \n",
" modf(x, /)\n",
" Return the fractional and integer parts of x.\n",
" \n",
" Both results carry the sign of x and are floats.\n",
" \n",
" pow(x, y, /)\n",
" Return x**y (x to the power of y).\n",
" \n",
" radians(x, /)\n",
" Convert angle x from degrees to radians.\n",
" \n",
" remainder(x, y, /)\n",
" Difference between x and the closest integer multiple of y.\n",
" \n",
" Return x - n*y where n*y is the closest integer multiple of y.\n",
" In the case where x is exactly halfway between two multiples of\n",
" y, the nearest even value of n is used. The result is always exact.\n",
" \n",
" sin(x, /)\n",
" Return the sine of x (measured in radians).\n",
" \n",
" sinh(x, /)\n",
" Return the hyperbolic sine of x.\n",
" \n",
" sqrt(x, /)\n",
" Return the square root of x.\n",
" \n",
" tan(x, /)\n",
" Return the tangent of x (measured in radians).\n",
" \n",
" tanh(x, /)\n",
" Return the hyperbolic tangent of x.\n",
" \n",
" trunc(x, /)\n",
" Truncates the Real x to the nearest Integral toward 0.\n",
" \n",
" Uses the __trunc__ magic method.\n",
"\n",
"DATA\n",
" e = 2.718281828459045\n",
" inf = inf\n",
" nan = nan\n",
" pi = 3.141592653589793\n",
" tau = 6.283185307179586\n",
"\n",
"FILE\n",
" /anaconda3/lib/python3.7/lib-dynload/math.cpython-37m-darwin.so\n",
"\n",
"\n"
]
}
],
"source": [
"help(math)"
]
},
{
"cell_type": "code",
"execution_count": 67,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3.141592653589793"
]
},
"execution_count": 67,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"math.pi"
]
},
{
"cell_type": "code",
"execution_count": 69,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1.2246467991473532e-16"
]
},
"execution_count": 69,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"math.sin(math.pi)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"\n",
"
\n",
"More convenient ways to import:\n",
"
\n",
"\n",
"
\n",
"\n",
"1. Give a module a nickname:\n",
"\n",
""
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"import math as m"
]
},
{
"cell_type": "code",
"execution_count": 71,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1.2246467991473532e-16"
]
},
"execution_count": 71,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"m.sin(m.pi)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"2. Import some module content directly:\n",
"
"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"from math import sin, pi"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1.2246467991473532e-16"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sin(pi)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"\n",
"3. Import the whole module content directly (not recommended):\n",
"\n",
"
\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"from math import *"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"-1.0"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cos(pi)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1.1447298858494002"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"log(pi)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"
go top\n",
"
\n",
"
\n",
"3.4 Text Strings\n",
"
\n",
"\n",
"
\n",
"\n",
"
\n",
"- In either single or double quotes.
\n",
"- In triple quotes for multiline strings.
\n",
"
\n",
"\n",
"
"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello\n"
]
}
],
"source": [
"a = 'Hello'\n",
"print(a)"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"world!\n"
]
}
],
"source": [
"b = \"world!\"\n",
"print(b)"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"more than \n",
"one line\n"
]
}
],
"source": [
"c = '''more than \n",
"one line'''\n",
"print(c)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"String concatenation:\n",
"
"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Helloworld!\n"
]
}
],
"source": [
"c = a + b\n",
"print(c)"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"HelloHelloHelloHelloHelloHelloHelloHelloHelloHello\n"
]
}
],
"source": [
"print(10*a)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"f-strings:\n",
"
"
]
},
{
"cell_type": "code",
"execution_count": 85,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1 plus 2 equals 3\n"
]
}
],
"source": [
"a = 1\n",
"b = 2\n",
"c = a + b\n",
"\n",
"s = f\"{a} plus {b} equals {c}\"\n",
"print(s)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"
go top\n",
"
\n",
"
\n",
"3.5 if/then/else\n",
"
\n",
"\n",
"
\n",
"\n",
"
\n",
"if condition: \n",
" do this \n",
" do this\n",
" do this\n",
"else: \n",
" do this \n",
" do this\n",
"
\n",
"\n",
"
\n",
"\n",
"
Note: In Python code blocks are defined by their indentation.\n",
"\n",
"
"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"big number\n",
"Done!\n"
]
}
],
"source": [
"x = 101\n",
"\n",
"if x > 100:\n",
" print('big number')\n",
"print('Done!')"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"small number\n",
"Done!\n"
]
}
],
"source": [
"x = 0\n",
"\n",
"if x > 100:\n",
" print(\"big number\")\n",
"else:\n",
" print(\"small number\")\n",
"print(\"Done!\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"
go top\n",
"
\n",
"
\n",
"3.6 Booleans\n",
"
\n",
"\n",
"\n",
"
"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"10 > 5"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"10 == 5"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"10 != 5"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(10 > 5) or (10 < 5) "
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(10 > 5) and (10 < 5) "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"\n",
"
\n",
"Note:\n",
"\n",
"\n",
"
\n",
"- Non-zero numbers and non-empty strings are interpreted as
True
. \n",
"- Zero and an empty string give
False
. \n",
"
\n",
"\n",
"
\n"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Yes!\n"
]
}
],
"source": [
"x = 7\n",
"\n",
"if x:\n",
" print(\"Yes!\")\n",
"else:\n",
" print(\"No!\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"
go top\n",
"
\n",
"
\n",
"3.7 \"while\" loops\n",
"
\n",
"\n",
"
\n",
"while condition: \n",
" do this \n",
" do this\n",
" do this\n",
"
\n",
"\n",
"
"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"10\n",
"20\n",
"30\n",
"40\n",
"Done!\n"
]
}
],
"source": [
"i = 1\n",
"while i < 5:\n",
" print(i*10)\n",
" i += 1\n",
"print(\"Done!\")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Delete file? (yes/no): y\n",
"invalid option: \"y\"\n",
"Delete file? (yes/no): n\n",
"invalid option: \"n\"\n",
"Delete file? (yes/no): yes\n",
"File deleted\n"
]
}
],
"source": [
"r = input(\"Delete file? (yes/no): \")\n",
"\n",
"while True:\n",
" if r == 'no':\n",
" print(\"Cancelled\")\n",
" break\n",
" elif r == 'yes': \n",
" print(\"File deleted\")\n",
" break\n",
" else:\n",
" print(f'invalid option: \"{r}\"')\n",
" r = input(\"Delete file? (yes/no): \")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"
go top\n",
"
\n",
"
\n",
"3.8 Lists and \"for\" Loops\n",
"
\n",
"\n",
"
\n",
"\n",
"
\n",
"for item in list_of_items: \n",
" do this \n",
" do this\n",
" do this\n",
"
\n",
"
"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"\n",
"
\n",
"- A list is an ordered collection of objects of arbitrary types.
\n",
"- It is created by enclosing objects in square brackets: \n",
"
\n",
"
\n",
"
\n",
"mylist = [a, b, c] \n",
"
\n",
"\n",
"\n",
"
"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[3, 5.1, 'Hello', True]"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mylist = [3, 5.1, \"Hello\", True]\n",
"mylist"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"6\n",
"10.2\n",
"HelloHello\n",
"2\n"
]
}
],
"source": [
"for x in mylist:\n",
" print(2*x)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"\n",
"
Note: \n",
"
For all practical purposes True
and False
are the same as 1 and 0:
\n",
"\n",
"\n",
"
\n"
]
},
{
"cell_type": "code",
"execution_count": 70,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 70,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"True == 1"
]
},
{
"cell_type": "code",
"execution_count": 71,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 71,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"False == 0"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"\n",
"
Accessing list items (indexing starts at 0):
\n",
"\n",
"\n",
"
\n"
]
},
{
"cell_type": "code",
"execution_count": 245,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[3, 5.1, 'Hello', True]"
]
},
"execution_count": 245,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mylist"
]
},
{
"cell_type": "code",
"execution_count": 246,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 246,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mylist[0]"
]
},
{
"cell_type": "code",
"execution_count": 247,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Hello'"
]
},
"execution_count": 247,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mylist[2]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"\n",
"
Negative indexing counts from the end:
\n",
"\n",
"
\n"
]
},
{
"cell_type": "code",
"execution_count": 254,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 254,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mylist[-1]"
]
},
{
"cell_type": "code",
"execution_count": 255,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Hello'"
]
},
"execution_count": 255,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mylist[-2]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"\n",
"
List concatenation:
\n",
"\n",
"
"
]
},
{
"cell_type": "code",
"execution_count": 283,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 2, 3, 'a', 'b', 'c']"
]
},
"execution_count": 283,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = [1, 2, 3] + ['a', 'b', 'c']\n",
"x"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"\n",
"
Appending an element to a list:
\n",
"\n",
"
"
]
},
{
"cell_type": "code",
"execution_count": 284,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 2, 3, 'a', 'b', 'c']"
]
},
"execution_count": 284,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x"
]
},
{
"cell_type": "code",
"execution_count": 285,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 2, 3, 'a', 'b', 'c', 'Hello']"
]
},
"execution_count": 285,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x.append('Hello')\n",
"x"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"
go top\n",
"
\n",
"
\n",
"3.9 Lists Slicing\n",
"
\n",
"\n",
"
\n",
"\n",
"
\n",
" - Creates a sublist of a list.
\n",
"
\n",
"\n",
"
\n",
"mylist[start : stop]\n",
"mylist[start : stop : step]\n",
"
\n",
"\n",
"
"
]
},
{
"cell_type": "code",
"execution_count": 259,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]"
]
},
"execution_count": 259,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"numbers = list(range(20))\n",
"numbers"
]
},
{
"cell_type": "code",
"execution_count": 261,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[5, 6, 7, 8, 9, 10, 11, 12, 13, 14]"
]
},
"execution_count": 261,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"numbers[5:15]"
]
},
{
"cell_type": "code",
"execution_count": 262,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[5, 8, 11, 14]"
]
},
"execution_count": 262,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"numbers[5:15:3]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"Strings can be sliced too:\n",
"
"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"s = \"University at Buffalo\""
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'ersity at Bu'"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s[4:16]"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Uiest tBfao'"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s[::2]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"
go top\n",
"
\n",
"
\n",
"3.10 Lists Comprehensions\n",
"
\n",
"\n",
"
\n",
"\n",
"
\n",
" - Creates a new list from a given list.
\n",
"
\n",
"\n",
"
\n",
"[f(x) for x in mylist]\n",
"[f(x) for x in mylist if condition]\n",
"
\n",
"\n",
"
"
]
},
{
"cell_type": "code",
"execution_count": 271,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]"
]
},
"execution_count": 271,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"numbers = list(range(10))\n",
"numbers"
]
},
{
"cell_type": "code",
"execution_count": 272,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]"
]
},
"execution_count": 272,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"squares = [x**2 for x in numbers]\n",
"squares"
]
},
{
"cell_type": "code",
"execution_count": 274,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[0, 4, 16, 36, 64]"
]
},
"execution_count": 274,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"even_squares = [x**2 for x in numbers if x%2 == 0]\n",
"even_squares"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The same with the \"for\" loop:"
]
},
{
"cell_type": "code",
"execution_count": 276,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[0, 4, 16, 36, 64]"
]
},
"execution_count": 276,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"even_squares2 = []\n",
"for x in numbers:\n",
" if x%2 == 0:\n",
" even_squares2.append(x**2)\n",
"\n",
"even_squares2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"
Example: \n",
"FizzBuzz\n",
"
\n",
"\n",
" \n",
"\n",
"
\n",
"Write a program that prints the numbers from 1 to 100. But: \n",
" \n",
" - for multiples of 3 print “Fizz” instead of the number
\n",
" - for the multiples of 5 print “Buzz”
\n",
" - for multiples of both 3 and 5 print “FizzBuzz”.
\n",
"
\n",
"\n",
"
"
]
},
{
"cell_type": "code",
"execution_count": 315,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"2\n",
"Fizz\n",
"4\n",
"Buzz\n",
"Fizz\n",
"7\n",
"8\n",
"Fizz\n",
"Buzz\n",
"11\n",
"Fizz\n",
"13\n",
"14\n",
"FizzBuzz\n",
"16\n",
"17\n",
"Fizz\n",
"19\n"
]
}
],
"source": [
"fb = [(\"Fizz\" if x%3==0 else \"\") + (\"Buzz\" if x%5==0 else \"\") or x for x in range(1, 20) ]\n",
"\n",
"for i in fb: \n",
" print(i)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"''"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = 5\n",
"(\"Fizz\" if x%3==0 else \"\")"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'FizzBuzz'"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = 15\n",
"(\"Fizz\" if x%3==0 else \"\") + (\"Buzz\" if x%5==0 else \"\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Note:** `or` and `and` return the value of the last expression they evaluate:"
]
},
{
"cell_type": "code",
"execution_count": 300,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'hi'"
]
},
"execution_count": 300,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = \"hi\"\n",
"b = \"hello\"\n",
"\n",
"a or b"
]
},
{
"cell_type": "code",
"execution_count": 97,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"10"
]
},
"execution_count": 97,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = 10\n",
"x or 2/0"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'FizzBuzz'"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = 15\n",
"(\"Fizz\" if x%3==0 else \"\") + (\"Buzz\" if x%5==0 else \"\") or x"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"
go top\n",
"
\n",
"
\n",
"3.11 Dictionaries\n",
"
\n",
"\n",
"
\n",
"\n",
"
\n",
" - Collections of values indexed by keys:
\n",
"
\n",
"\n",
"
\n",
"my_dictionary = {key1:value1, key2:value2, ...}
\n",
"\n",
"
"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [],
"source": [
"game_scores = {}"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'John': 100, 'Ann': 120, 'Tom': 70}"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"game_scores[\"John\"] = 100\n",
"game_scores[\"Ann\"] = 120 \n",
"game_scores[\"Tom\"] = 70\n",
"game_scores"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"Values can be retrieved and modified by key:\n",
"
"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"120"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"game_scores[\"Ann\"]"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'John': 100, 'Ann': 130, 'Tom': 70}"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"game_scores[\"Ann\"] = 130\n",
"game_scores"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Adding new records:"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'John': 100, 'Ann': 130, 'Tom': 70, 'Beth': 0}"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"game_scores[\"Beth\"] = 0\n",
"game_scores"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"Iteration on keys:\n",
"
"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"John 100\n",
"Ann 130\n",
"Tom 70\n",
"Beth 0\n"
]
}
],
"source": [
"for name in game_scores:\n",
" print(name, game_scores[name])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"
go top\n",
"
\n",
"
\n",
"3.12 Functions\n",
"
\n",
"\n",
"
\n",
"\n",
"\n",
"
\n",
"def foo(arg1 , arg2, ...):\n",
" do this \n",
" do this\n",
" return this\n",
"
\n",
"\n",
"
"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [],
"source": [
"def add(x, y):\n",
" z = x+y\n",
" return z"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"add(2, 3)"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'hello world!'"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"add(\"hello \", \"world!\")"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {},
"outputs": [],
"source": [
"from time import sleep\n",
"\n",
"def timer(seconds, message = \"Time's up!\"):\n",
" for t in range(seconds,0,-1):\n",
" print(t)\n",
" sleep(1)\n",
" print(message)"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"10\n",
"9\n",
"8\n",
"7\n",
"6\n",
"5\n",
"4\n",
"3\n",
"2\n",
"1\n",
"Time's up!\n"
]
}
],
"source": [
"timer(10)"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5\n",
"4\n",
"3\n",
"2\n",
"1\n",
"Boom!\n"
]
}
],
"source": [
"timer(5, message = \"Boom!\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"
go top\n",
"
\n",
"
\n",
"3.13 Classes and Objects\n",
"
\n",
"\n",
"
\n",
"\n",
"\n",
"
\n",
"class MyClass:\n",
" statement\n",
" statement\n",
" statement\n",
" \n",
"x = MyClass()\n",
"
\n",
"\n",
"\n",
"\n",
"
"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"class Player:\n",
"\n",
" name = None\n",
" score = 0"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"class Player:\n",
"\n",
" name = None\n",
" score = 0\n",
" \n",
" def bonus(self, percent):\n",
" self.score = int(self.score * (1 + percent/100))\n",
" \n",
" def __str__(self):\n",
" return f\"Name: {self.name}\\nScore: {self.score}\""
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Name: John\n",
"Score: 10\n"
]
}
],
"source": [
"p = Player()\n",
"p.name = \"John\"\n",
"p.score = 10\n",
"\n",
"print(p)"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [],
"source": [
"p.bonus(50)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Name: John\n",
"Score: 10\n"
]
}
],
"source": [
"print(p)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Name: None\n",
"Score: 0\n"
]
}
],
"source": [
"q = Player()\n",
"print(q)"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [],
"source": [
"q.name = \"Beth\"\n",
"q.score = 20"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Name: Beth\n",
"Score: 20\n"
]
}
],
"source": [
"print(q)"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['__class__',\n",
" '__delattr__',\n",
" '__dict__',\n",
" '__dir__',\n",
" '__doc__',\n",
" '__eq__',\n",
" '__format__',\n",
" '__ge__',\n",
" '__getattribute__',\n",
" '__gt__',\n",
" '__hash__',\n",
" '__init__',\n",
" '__init_subclass__',\n",
" '__le__',\n",
" '__lt__',\n",
" '__module__',\n",
" '__ne__',\n",
" '__new__',\n",
" '__reduce__',\n",
" '__reduce_ex__',\n",
" '__repr__',\n",
" '__setattr__',\n",
" '__sizeof__',\n",
" '__str__',\n",
" '__subclasshook__',\n",
" '__weakref__',\n",
" 'bonus',\n",
" 'name',\n",
" 'score']"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dir(q)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"Note: In Python everything is an object.\n",
"
"
]
},
{
"cell_type": "code",
"execution_count": 91,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"list"
]
},
"execution_count": 91,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[1, 2, 3].__class__"
]
},
{
"cell_type": "code",
"execution_count": 78,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['__add__',\n",
" '__class__',\n",
" '__contains__',\n",
" '__delattr__',\n",
" '__delitem__',\n",
" '__dir__',\n",
" '__doc__',\n",
" '__eq__',\n",
" '__format__',\n",
" '__ge__',\n",
" '__getattribute__',\n",
" '__getitem__',\n",
" '__gt__',\n",
" '__hash__',\n",
" '__iadd__',\n",
" '__imul__',\n",
" '__init__',\n",
" '__init_subclass__',\n",
" '__iter__',\n",
" '__le__',\n",
" '__len__',\n",
" '__lt__',\n",
" '__mul__',\n",
" '__ne__',\n",
" '__new__',\n",
" '__reduce__',\n",
" '__reduce_ex__',\n",
" '__repr__',\n",
" '__reversed__',\n",
" '__rmul__',\n",
" '__setattr__',\n",
" '__setitem__',\n",
" '__sizeof__',\n",
" '__str__',\n",
" '__subclasshook__',\n",
" 'append',\n",
" 'clear',\n",
" 'copy',\n",
" 'count',\n",
" 'extend',\n",
" 'index',\n",
" 'insert',\n",
" 'pop',\n",
" 'remove',\n",
" 'reverse',\n",
" 'sort']"
]
},
"execution_count": 78,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dir([1, 2, 3])"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 2, 3, 4, 5]"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[1, 2, 3].__add__([4,5])"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false
},
"source": [
""
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}