Skip to content

Instantly share code, notes, and snippets.

@paulochf
Last active August 26, 2016 13:22
Show Gist options
  • Save paulochf/49c56a2f8fd7896163f1745ae96b6069 to your computer and use it in GitHub Desktop.
Save paulochf/49c56a2f8fd7896163f1745ae96b6069 to your computer and use it in GitHub Desktop.
Python presentation
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# CS1001.py\n",
"## Extended Introduction to Computer Science with Python, Tel-Aviv University, Spring 2013\n",
"# Recitation 1 - 28.2-4.3.2013\n",
"## Last update: 28.2.2013"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Python general comments\n",
"\n",
"1. Course site at <http://tau-cs1001-py.wikidot.com>\n",
"1. Programming language -> Interpreter -> Machine language\n",
"1. IDLE (editor + interpreter), see site for installation instructions\n",
"1. Interactive mode vs. Script mode\n",
"1. Python version 3.2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* print function - prints a textual representation to the console"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello world!\n"
]
}
],
"source": [
"print(\"Hello world!\")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello world!\n"
]
}
],
"source": [
"print(\"Hello\", \"world!\")"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2\n"
]
}
],
"source": [
"print(2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Variables, types"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* int - integers: ..., -3, -2, -1, 0, 1, 2, 3, ..."
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5\n"
]
}
],
"source": [
"t = x\n",
"print(t)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5 <class 'int'> 5\n",
"-3 <class 'int'>\n"
]
}
],
"source": [
"x=5\n",
"y=-3\n",
"print(x, type(x), x)\n",
"print(y, type(y))"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'float'>\n"
]
}
],
"source": [
"x = 5.5\n",
"print(type(x))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* float - floating point numbers, decimal point fractions: -3.2, 1.5, 1e-8, 3.2e5"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5.0 <class 'float'>\n",
"2200000.0 <class 'float'>\n"
]
}
],
"source": [
"x=5.0\n",
"y=-3.2\n",
"z=2.2e6\n",
"print(x, type(x))\n",
"print(z, type(z))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* str - character strings, text: \"intro2CS\", 'python'"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CS1001.py <class 'str'>\n",
"I love python <class 'str'>\n"
]
}
],
"source": [
"x = \"CS1001.py\"\n",
"y = 'I love python'\n",
"print(x, type(x))\n",
"print(y, type(y))"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": false
},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'CS1001' 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<ipython-input-12-3e6c875da43f>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mx\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mCS1001\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mNameError\u001b[0m: name 'CS1001' is not defined"
]
}
],
"source": [
"x = CS1001"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'int'> <class 'float'> <class 'str'>\n"
]
}
],
"source": [
"print(type(4), type(4.0), type(\"4\"))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* bool - boolean values: True and False"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True <class 'bool'>\n",
"False <class 'bool'>\n"
]
}
],
"source": [
"i_love_python = True\n",
"python_loves_me = False\n",
"print(i_love_python, type(i_love_python))\n",
"print(python_loves_me, type(python_loves_me))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Operators"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Mathematical operators"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Addition:"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"9"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"4 + 5"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"9"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = 5\n",
"4 + x"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"9.0 <class 'float'>\n"
]
}
],
"source": [
"x = 4.0 + 5\n",
"print(x, type(x))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Subtraction:"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"6.0"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x - 3"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Multiplication:"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"27.0"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x * 3"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Division - float and integral with / and //:"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(3.3333333333333335, 3)"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"10 / 3, 10 // 3"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Power:"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(8, 8.0, 9)"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"2 ** 3, 2 ** 3.0, 3 ** 2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Modolu:"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"10 % 3"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### String operators"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"String concatenation using +:"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"'Hello World'"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\"Hello\" + \" World\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"String duplication using *:"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"'ByeBye'"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\"Bye\" * 2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Strings vs. numbers:"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"9"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"4 + 5"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"'45'"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\"4\" + \"5\""
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {
"collapsed": false
},
"outputs": [
{
"ename": "TypeError",
"evalue": "Can't convert 'int' object to str implicitly",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-29-f945f8c7e111>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;34m\"4\"\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;36m5\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m: Can't convert 'int' object to str implicitly"
]
}
],
"source": [
"\"4\" + 5"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {
"collapsed": false
},
"outputs": [
{
"ename": "TypeError",
"evalue": "unsupported operand type(s) for +: 'int' and 'str'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-28-871c0c3bbca2>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;36m4\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;34m\"5\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for +: 'int' and 'str'"
]
}
],
"source": [
"4 + \"5\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Comparisons"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"5 < 4"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"5 > 4"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"5 >= 4"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"4 >= 4"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"4 <= 3"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 39,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"5 == 4"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 41,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"5 == 5.1"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"5 == \"5\""
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 47,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"5 != 4"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 44,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"2 + 2 == 4"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {
"collapsed": false
},
"outputs": [
{
"ename": "SyntaxError",
"evalue": "invalid syntax (<ipython-input-45-76c8f045e4cf>, line 1)",
"output_type": "error",
"traceback": [
"\u001b[0;36m File \u001b[0;32m\"<ipython-input-45-76c8f045e4cf>\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m 2 => 3\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"
]
}
],
"source": [
"2 => 3"
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.3333333333333333\n"
]
}
],
"source": [
"x = 1 / 3 \n",
"print(x)"
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 53,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"1/3 == 0.3333333333333333"
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 55,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"0.3333333333333333 == 0.333333333333"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Logical operators"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* not:"
]
},
{
"cell_type": "code",
"execution_count": 56,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"False\n"
]
}
],
"source": [
"print(not True)"
]
},
{
"cell_type": "code",
"execution_count": 57,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 57,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = 2 == 5\n",
"a"
]
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n"
]
}
],
"source": [
"print(not a)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* and:"
]
},
{
"cell_type": "code",
"execution_count": 59,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 59,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"True and True"
]
},
{
"cell_type": "code",
"execution_count": 60,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 60,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"True and False"
]
},
{
"cell_type": "code",
"execution_count": 61,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 61,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"False and False"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* or:"
]
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 62,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"True or True"
]
},
{
"cell_type": "code",
"execution_count": 63,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 63,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"True or False"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Conversions"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Use the functions `int()`, `float()`, and `str()` to convert between types (we will talk about *functions* next time):"
]
},
{
"cell_type": "code",
"execution_count": 64,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"6 <class 'str'>\n"
]
}
],
"source": [
"x = \"6\"\n",
"print(x, type(x))"
]
},
{
"cell_type": "code",
"execution_count": 67,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"6 <class 'int'>\n"
]
}
],
"source": [
"x = int(\"6\")\n",
"print(x, type(x))"
]
},
{
"cell_type": "code",
"execution_count": 68,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"1.25"
]
},
"execution_count": 68,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"float(\"1.25\")"
]
},
{
"cell_type": "code",
"execution_count": 69,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"1.0"
]
},
"execution_count": 69,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"float(\"1\")"
]
},
{
"cell_type": "code",
"execution_count": 70,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"'4'"
]
},
"execution_count": 70,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"str(4)"
]
},
{
"cell_type": "code",
"execution_count": 71,
"metadata": {
"collapsed": false
},
"outputs": [
{
"ename": "ValueError",
"evalue": "invalid literal for int() with base 10: 'a'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-71-91097a4105a2>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"a\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mValueError\u001b[0m: invalid literal for int() with base 10: 'a'"
]
}
],
"source": [
"int(\"a\")"
]
},
{
"cell_type": "code",
"execution_count": 72,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"intro2cs\n"
]
}
],
"source": [
"course = \"intro\" + str(2) + \"cs\"\n",
"print(course)"
]
},
{
"cell_type": "code",
"execution_count": 73,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"intro2cs\n"
]
}
],
"source": [
"print(\"intro\", 2, \"cs\", sep='')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Flow control"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Conditional statements"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The `if` condition formula - replace conditions and statements with meaningful code:\n",
"\n",
" if *condition*:\n",
" *statement*\n",
" *statement*\n",
" ...\n",
" elif *condition*: # 0 or more elif clauses\n",
" *statement*\n",
" *statement*\n",
" ... \n",
" else: # optional\n",
" *statement*\n",
" *statement*\n",
"\n",
"Example:"
]
},
{
"cell_type": "code",
"execution_count": 76,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Not a day\n"
]
}
],
"source": [
"today = \"Bla\"\n",
"strike = \"N\"\n",
"my_recitation = \"Monday\"\n",
"\n",
"if today == \"Sunday\":\n",
" print(\"Shvizut Yom Alef\")\n",
" if strike == \"Y\":\n",
" print(\"Stay home\")\n",
" else:\n",
" print(\"Lecture in intro to CS!\")\n",
" \n",
"elif today == \"Wednesday\":\n",
" print(\"Another lecture in intro to CS!\")\n",
" \n",
"elif today == my_recitation:\n",
" print(\"Go to recitation!\")\n",
" \n",
"elif today == \"Monday\" or today == \"Tuesday\" or today == \"Thursday\" or \\\n",
" today == \"Friday\" or today == \"Saturday\":\n",
" print(\"no intro to CS\")\n",
" \n",
"else:\n",
" print(\"Not a day\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Loops"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* While:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" while *condition*:\n",
" *statement*\n",
" *statement*\n",
"\n",
"Example - count how many times 0 appears in an integer number:"
]
},
{
"cell_type": "code",
"execution_count": 82,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1267650600228229401496703205376\n"
]
}
],
"source": [
"num = 2**100\n",
"print(num)"
]
},
{
"cell_type": "code",
"execution_count": 83,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1267650600228229401496703205376\n",
"126765060022822940149670320537\n",
"12676506002282294014967032053\n",
"1267650600228229401496703205\n",
"126765060022822940149670320\n",
"12676506002282294014967032\n",
"1267650600228229401496703\n",
"126765060022822940149670\n",
"12676506002282294014967\n",
"1267650600228229401496\n",
"126765060022822940149\n",
"12676506002282294014\n",
"1267650600228229401\n",
"126765060022822940\n",
"12676506002282294\n",
"1267650600228229\n",
"126765060022822\n",
"12676506002282\n",
"1267650600228\n",
"126765060022\n",
"12676506002\n",
"1267650600\n",
"126765060\n",
"12676506\n",
"1267650\n",
"126765\n",
"12676\n",
"1267\n",
"126\n",
"12\n",
"1\n",
"6\n"
]
}
],
"source": [
"count = 0\n",
"\n",
"while num > 0: #what if we changed to >=0?\n",
" print(num)\n",
" if num % 10 == 0:\n",
" count = count + 1\n",
" num = num // 10\n",
"\n",
"print(count)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* For:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" for *variable* in *iterable*:\n",
" *statement*\n",
" *statement*\n",
"\n",
"Example - solve the same problem with a `str` type instead of `int`:"
]
},
{
"cell_type": "code",
"execution_count": 84,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"6\n"
]
}
],
"source": [
"num = 2**100\n",
"count = 0\n",
"for digit in str(num):\n",
" #print(digit, type(digit))\n",
" if digit == \"0\":\n",
" count = count + 1\n",
"\n",
"print(count)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Builtin solution:"
]
},
{
"cell_type": "code",
"execution_count": 85,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"6\n"
]
}
],
"source": [
"num = 2**100\n",
"count = str.count(str(num), \"0\")\n",
"\n",
"print(count)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Efficiency"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can measure which solution is faster:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%%timeit\n",
"num = 2**100\n",
"count = 0\n",
"while num>0: #what if we changed to >=0?\n",
" if num % 10 == 0:\n",
" count = count + 1\n",
" num = num // 10"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%%timeit \n",
"num = 2**100\n",
"count = 0\n",
"\n",
"for digit in str(num):\n",
" if digit == \"0\":\n",
" count = count + 1"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%%timeit \n",
"num = 2**100\n",
"count = str.count(str(num), \"0\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The builtin solution is 4 times faster than the `for` solution which is 3 times faster than the `while` solution."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Other notes"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* The `while` solution will not work for `num <= 0`\n",
"* The `while` solution will not work for non-numerals (e.g, `num = \"Cola 0 is awesome!\"`)\n",
"* The builtin solution is implemented with C and that is why it is faster"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Fin"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This notebook is part of the [Extended introduction to computer science](http://tau-cs1001-py.wikidot.com/) course at Tel-Aviv University.\n",
"\n",
"The notebook was written using Python 3.2 and IPython 0.13.1.\n",
"\n",
"The code is available at <https://raw.github.com/yoavram/CS1001.py/master/recitation1.ipynb>.\n",
"\n",
"The notebook can be viewed online at <http://nbviewer.ipython.org/urls/raw.github.com/yoavram/CS1001.py/master/recitation1.ipynb>.\n",
"\n",
"The notebooks is also available as a PDF at <https://github.com/yoavram/CS1001.py/blob/master/recitation1.pdf?raw=true>.\n",
"\n",
"This work is licensed under a [Creative Commons Attribution-ShareAlike 3.0 Unported License](http://creativecommons.org/licenses/by-sa/3.0/)."
]
}
],
"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.5.2"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment