{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n" ], "text/plain": [ "" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from IPython.display import HTML\n", "import requests\n", "HTML(requests.get(\"https://git.io/fh5WI\").text)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

Python and Jupyter for Programmers

\n", "

Part 1: Jupyter and Python Basics

\n", "
\n", "\n", "\n", "\n", "
\n", "

Contents

\n", "
\n", "
\n", "
    \n", "
  1. Jupyter Notebook and JupterLab
  2. \n", "
  3. Notebook Basics\n", "
      \n", "
    1. Markdown Cells
    2. \n", "
    3. Code Cells
    4. \n", "
    \n", "
  4. \n", "
  5. Python Basics\n", "
      \n", "
    1. Numbers and arithmetics
    2. \n", "
    3. Variable Assignments
    4. \n", "
    5. Importing Modules
    6. \n", "
    7. Text Strings
    8. \n", "
    9. if/then/else
    10. \n", "
    11. Booleans
    12. \n", "
    13. \"while\" Loops
    14. \n", "
    15. Lists and \"for\" Loops
    16. \n", "
    17. Lists Slicing
    18. \n", "
    19. Lists Comprehensions
    20. \n", "
    21. Dictionaries
    22. \n", "
    23. Functions
    24. \n", "
    25. Classes and Objects
    26. \n", "
    \n", "
  6. \n", "
      \n", "
\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "go top\n", "\n", "

\n", "1. Jupyter Notebook and JupyterLab\n", "

\n", "\n", "
\n", "\n", "

Jupyter Notebook (used in this workshop)

\n", "\n", "\n", "\n", "\n", "

Jupyter Lab

\n", "\n", "\n", "\n", "\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "go top\n", "\n", "

\n", "2. Notebook Basics\n", "

\n", "\n", "\n", "
\n", "\n", "\n", "

\n", "2.1 Markdown Cells\n", "

\n", "\n", "

Shift-Enter to render.

\n", "\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is a markdown cell.\n", "\n", "# Header\n", "## Subheader\n", "## Subsubheader\n", "\n", "\n", "*italics* or **boldface**\n", "\n", "Lists:\n", "\n", "- item 1\n", "- item 2\n", "- item 3\n", "\n", "[This is a link](http://www.buffalo.edu)\n", "\n", "Some mathematics (uses LaTeX syntax):\n", "\n", "$$\\int x^2 dx = \\frac{1}{3} x^3 + C$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "go top\n", "\n", "

\n", "3.2 Code Cells \n", "

\n", "\n", "

Shift-Enter to execute.

\n", "
" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "15" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3*5" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hello!\n" ] } ], "source": [ "print(\"hello!\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "go top\n", "\n", "

\n", "3. Python Basics\n", "

\n", "\n", "
\n", "\n", "\n", "

\n", "3.1 Numbers and Arithmetics\n", "

\n", "\n", "
" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2+3" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2*3" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "8" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2**3" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6.0" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2.0*3.0 " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "Integers can be of an arbitrary size:\n", "
" ] }, { "cell_type": "code", "execution_count": 122, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "19950631168807583848837421626835850838234968318861924548520089498529438830221946631919961684036194597899331129423209124271556491349413781117593785932096323957855730046793794526765246551266059895520550086918193311542508608460618104685509074866089624888090489894838009253941633257850621568309473902556912388065225096643874441046759871626985453222868538161694315775629640762836880760732228535091641476183956381458969463899410840960536267821064621427333394036525565649530603142680234969400335934316651459297773279665775606172582031407994198179607378245683762280037302885487251900834464581454650557929601414833921615734588139257095379769119277800826957735674444123062018757836325502728323789270710373802866393031428133241401624195671690574061419654342324638801248856147305207431992259611796250130992860241708340807605932320161268492288496255841312844061536738951487114256315111089745514203313820202931640957596464756010405845841566072044962867016515061920631004186422275908670900574606417856951911456055068251250406007519842261898059237118054444788072906395242548339221982707404473162376760846613033778706039803413197133493654622700563169937455508241780972810983291314403571877524768509857276937926433221599399876886660808368837838027643282775172273657572744784112294389733810861607423253291974813120197604178281965697475898164531258434135959862784130128185406283476649088690521047580882615823961985770122407044330583075869039319604603404973156583208672105913300903752823415539745394397715257455290510212310947321610753474825740775273986348298498340756937955646638621874569499279016572103701364433135817214311791398222983845847334440270964182851005072927748364550578634501100852987812389473928699540834346158807043959118985815145779177143619698728131459483783202081474982171858011389071228250905826817436220577475921417653715687725614904582904992461028630081535583308130101987675856234343538955409175623400844887526162643568648833519463720377293240094456246923254350400678027273837755376406726898636241037491410966718557050759098100246789880178271925953381282421954028302759408448955014676668389697996886241636313376393903373455801407636741877711055384225739499110186468219696581651485130494222369947714763069155468217682876200362777257723781365331611196811280792669481887201298643660768551639860534602297871557517947385246369446923087894265948217008051120322365496288169035739121368338393591756418733850510970271613915439590991598154654417336311656936031122249937969999226781732358023111862644575299135758175008199839236284615249881088960232244362173771618086357015468484058622329792853875623486556440536962622018963571028812361567512543338303270029097668650568557157505516727518899194129711337690149916181315171544007728650573189557450920330185304847113818315407324053319038462084036421763703911550639789000742853672196280903477974533320468368795868580237952218629120080742819551317948157624448298518461509704888027274721574688131594750409732115080498190455803416826949787141316063210686391511681774304792596709376" ] }, "execution_count": 122, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2**10000" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "Size of floats is limited:\n", "
" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "ename": "OverflowError", "evalue": "(34, 'Result too large')", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mOverflowError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;36m2.0\u001b[0m\u001b[0;34m**\u001b[0m\u001b[0;36m10000\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mOverflowError\u001b[0m: (34, 'Result too large')" ] } ], "source": [ "2.0**10000" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "Modulus:\n", "
" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "19%5" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "20%5" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "go top\n", "\n", "

\n", "3.2 Variable assignments\n", "

\n", "\n", "
" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "a = 3\n", "b = 4" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "7" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a+b" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "a,b,c = 4, 5, 6 " ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(4, 5)" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a, b" ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [], "source": [ "a, b = b, a" ] }, { "cell_type": "code", "execution_count": 64, "metadata": { "scrolled": false }, "outputs": [ { "data": { "text/plain": [ "(5, 4)" ] }, "execution_count": 64, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a, b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "go top\n", "\n", "

\n", "3.3 Importing Modules\n", "

\n", "\n", "
\n", "\n", "\n", "

\n", "Built-in functionality of Python is limited. E.g. no trigonometric functions:\n", "

\n", "\n", "\n", "
" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "scrolled": true }, "outputs": [ { "ename": "NameError", "evalue": "name 'sin' is not defined", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\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": [ "
\n", "

\n", " Part 2: Web Scrapping  \n", " \n", " \n", "

\n", "
" ] } ], "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 }