{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "

cs1001.py , Tel Aviv University, Fall 2017-2018

\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Recitation 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We have seen some basic types and operations, conversion functions, the function input and several structures that Python has to offer: if-else, while loops, and for loops. We had also solved an excercise of counting the number of zeros in a number inputted by the user.\n", "\n", "#### Takeaways:\n", "\n", "
    \n", "
  1. Try to \"play\" with types, operators, structures, and built-in functions in order to understand the basics of Python.
  2. \n", "
  3. To tackle programming exercises, try to first imagine what you want the solution to do intuitively and then find ways to implement it.
  4. \n", "
  5. Read the instructions for the submission of home assignments and submit your code as a .py file.
  6. \n", "
  7. Test your code before you submit.
  8. \n", "
\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Types and Variables" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(int, float, str, bool)" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = 3\n", "y = 3.14\n", "c = \"intro2CS\"\n", "b = True\n", "type(x), type(y), type(c), type(b)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Operators" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "6.140000000000001" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x + y" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'intro2CSamir'" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c + \"amir\"" ] }, { "cell_type": "code", "execution_count": 63, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'45'" ] }, "execution_count": 63, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"4\" + \"5\"3 < 4 and 3==4" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "ename": "TypeError", "evalue": "can't multiply sequence by non-int of type 'str'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\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[0mc\u001b[0m \u001b[1;33m*\u001b[0m \u001b[1;34m\"amir\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: can't multiply sequence by non-int of type 'str'" ] } ], "source": [ "c * \"amir\"" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'intro2CSintro2CS'" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c * 2" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "ename": "TypeError", "evalue": "can't multiply sequence by non-int of type 'float'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\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[0mc\u001b[0m \u001b[1;33m*\u001b[0m \u001b[1;36m2.5\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: can't multiply sequence by non-int of type 'float'" ] } ], "source": [ "c * 2.5" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "ename": "TypeError", "evalue": "can't multiply sequence by non-int of type 'float'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\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[0mc\u001b[0m \u001b[1;33m*\u001b[0m \u001b[1;36m2.0\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: can't multiply sequence by non-int of type 'float'" ] } ], "source": [ "c * 2.0" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "4.0" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 + 2.0" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "1.4142135623730951" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2**0.5" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "4.0" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2**2.0 " ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "4.0" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2.0**2" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "10%2" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "9" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "109%10" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "9//2 # rounding down" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3 < 4" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3 == 4" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3 != 4" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "True and False" ] }, { "cell_type": "code", "execution_count": 64, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 64, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3 < 4 and 3==4" ] }, { "cell_type": "code", "execution_count": 59, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "8" ] }, "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ "-2%10" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "bool" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(3 < 4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Conversion Functions" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int(\"4\")" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int(4.0)" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "collapsed": false }, "outputs": [ { "ename": "ValueError", "evalue": "invalid literal for int() with base 10: '4.0'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mValueError\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[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"4.0\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mValueError\u001b[0m: invalid literal for int() with base 10: '4.0'" ] } ], "source": [ "int(\"4.0\")" ] }, { "cell_type": "code", "execution_count": 65, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 65, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int(4.99) # int() always rounds down" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "4.0" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "float(4)" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "3.14" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "float(\"3.14\")" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "3.0" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "float(\"3\")" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'43'" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "str(43)" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'True'" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "str(True)" ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'intro2cs'" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "course = \"intro\" + str(2) + \"cs\"\n", "course" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "## If-else structure" ] }, { "cell_type": "code", "execution_count": 36, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Go to recitation!\n" ] } ], "source": [ "today = \"Monday\"\n", "strike = \"No\"\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", "elif today == \"Wednesday\":\n", " print(\"Another lecture in intro to CS!\")\n", "elif today==my_recitation:\n", " print(\"Go to recitation!\")\n", "elif today==\"Monday\" or today==\"Tuesday\" or today==\"Friday\" or today==\"Saturday\":\n", " print(\"no intro to CS\")\n", "else:\n", " print(\"Not a day\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Another If-elif-else example" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Two conditional structures:" ] }, { "cell_type": "code", "execution_count": 61, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hi\n", "bye\n" ] } ], "source": [ "n=100\n", "if n > 90 and n%2 == 0:\n", " print(\"hi\")\n", "if n > 99:\n", " print(\"bye\")\n", "else:\n", " print(\"something\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now there is only one conditional structure:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hi\n" ] } ], "source": [ "n=100\n", "if n > 90 and n%2 == 0:\n", " print(\"hi\")\n", "elif n > 99:\n", " print(\"bye\")\n", "else:\n", " print(\"something\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## the function input()" ] }, { "cell_type": "code", "execution_count": 66, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " please input an integer: 3445\n" ] }, { "data": { "text/plain": [ "'3445'" ] }, "execution_count": 66, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = input(\" please input an integer: \")\n", "x" ] }, { "cell_type": "code", "execution_count": 67, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " please input an integer: 3445\n" ] }, { "data": { "text/plain": [ "3445" ] }, "execution_count": 67, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = input(\" please input an integer: \")\n", "int(x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Introducing Loops!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Simple While Loop" ] }, { "cell_type": "code", "execution_count": 45, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n", "5\n", "6\n", "7\n", "8\n", "9\n", "value of a: 10\n" ] } ], "source": [ "a = 0\n", "while a < 10:\n", " print(a)\n", " a+=1\n", "print(\"value of a: \" + str(a))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Counting the number of zeros in a given integer (solution 1)" ] }, { "cell_type": "code", "execution_count": 68, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "please input an integer: 10003\n", "10003 3\n" ] } ], "source": [ "num = int(input(\"please input an integer: \"))\n", "m = num\n", "\n", "cnt = 0\n", "while num > 0: # if we change to >= we get an infinite loop\n", " if num % 10 == 0:\n", " cnt += 1\n", " num = num // 10\n", " \n", "print(m, cnt)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Simple For Loop" ] }, { "cell_type": "code", "execution_count": 50, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a\n", "m\n", "i\n", "r\n" ] } ], "source": [ "for c in \"amir\":\n", " print(c)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Counting the number of zeros in a given integer (solution 2)" ] }, { "cell_type": "code", "execution_count": 52, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "please input an integer: 10003\n", "10003 3\n" ] } ], "source": [ "num = input(\"please input an integer: \")\n", "\n", "cnt = 0\n", "for digit in num:\n", " if digit == \"0\":\n", " cnt += 1\n", "print(num, cnt)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Counting the number of zeros in a given integer (solution 3)" ] }, { "cell_type": "code", "execution_count": 54, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "please input an integer: 10003\n", "10003 3\n" ] } ], "source": [ "num = input(\"please input an integer: \")\n", "cnt = str.count(num, \"0\")\n", "print(num, cnt)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Simple Function Example" ] }, { "cell_type": "code", "execution_count": 58, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "22" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def amir(a):\n", " return a+1 # as opposed to print which will not return the value \n", "\n", "x = amir(10)\n", "x * 2" ] } ], "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.4.4" } }, "nbformat": 4, "nbformat_minor": 1 }