{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# CS1001.py, Tel-Aviv University, Spring 2019" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Recitation 1: Python basics \n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We discussed 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. \n", "We also solved an excercise of counting the number of zeros in a number that is given as an input by the user.\n", "\n", "Takeaways:\n", "1. Try to \"play\" with types, operators, structures, and built-in functions in order to understand the basics of Python.\n", "2. To tackle programming exercises, try to first imagine what you want the solution to do intuitively and then find ways to implement it.\n", "3. Read the instructions for the submission of home assignments and submit your code as a .py file.\n", "4. Test your code before you submit." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## IDLE\n", "We saw a demonstration on how to use IDLE (needed for homework coding part).\n", "Don't forget to run the test() function that is already given in the skeleton file.\n", "\n", "In order to open a .py file in Script mode, rightclick the file and select \"Edit with IDLE\". \n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Working with notebooks\n", "Read the following instructions if you are interested in working with Jupyter notebooks." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Code for printing several outputs in one cell (not part of the recitation):" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "from IPython.core.interactiveshell import InteractiveShell\n", "InteractiveShell.ast_node_interactivity = \"all\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Types: int, float, string, bool" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Types and Variables" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "\n", "\n" ] }, { "data": { "text/plain": [ "int" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = 3\n", "y = 3.14\n", "name = \"michal\"\n", "b = True\n", "print(type(x))\n", "print(type(y))\n", "print(type(name))\n", "print(type(b))\n", "type(x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Operators" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(\"4\")" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3.3333333333333335" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "10 / 3\n" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "10 // 3" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5.0" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "10 // 2.0" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9.0" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "4 + 5.0" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "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": {}, "outputs": [ { "data": { "text/plain": [ "3.0" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "9 ** 0.5" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'45'" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"4\" + \"5\" " ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'444'" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"4\" * 3" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "can't multiply sequence by non-int of type 'str'", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;34m\"4\"\u001b[0m \u001b[1;33m*\u001b[0m \u001b[1;34m\"5\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mTypeError\u001b[0m: can't multiply sequence by non-int of type 'str'" ] } ], "source": [ "\"4\" * \"5\"" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "10 % 2" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 % 10" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "8" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(-2) % 10" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3 < 4\n" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3 < 4 and 3 > 5\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Task: find the rightmost digit in a given number, using the operators mentioned above:" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" }, { "data": { "text/plain": [ "4" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = 345\n", "# rightmost digit\n", "x % 10\n", "# second rightmost digit\n", "(x // 10) % 10" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Operator precedence:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Conversions" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int(3.14)" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int(\"3\")" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3.0" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "float(\"3\")" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3.14" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "float(\"3.14\")" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'3'" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "str(3)" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'3.14'" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "str(3.14)" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "## If-else structure" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Better find something to eat...\n", "Cook something\n" ] } ], "source": [ "hungry = True\n", "today = \"Thursday\"\n", "tv_night = \"Thursday\" #\"Sunday\"\n", "last_episode_seen = 5\n", "\n", "if hungry:\n", " print(\"Better find something to eat...\")\n", " if today == \"Friday\":\n", " print(\"Go to a restaurant\")\n", " else:\n", " print(\"Cook something\")\n", "elif today == tv_night:\n", " print(\"Watch episode number\", last_episode_seen + 1, \"of your favorite show\")\n", "elif today == \"Thursday\":\n", " print(\"Go out for drinks!\")\n", "elif today == \"Monday\" or today == \"Wednesday\":\n", " print(\"Workout\")\n", "elif today == \"Friday\" or today == \"Tuesday\" or today == \"Saturday\":\n", " print(\"Go to sleep early :(\")\n", "else:\n", " print(\"That's not a real day\")\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Another If-elif-else example" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hi\n", "bye\n", "end of if structure\n" ] } ], "source": [ "n = 100\n", "if n % 2 == 0:\n", " print(\"hi\")\n", "if n > 2:\n", " print(\"bye\")\n", "else:\n", " print(\"something\")\n", "print(\"end of if structure\")" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "bye\n", "end of if structure\n" ] } ], "source": [ "n = 101\n", "if n % 2 == 0:\n", " print(\"hi\")\n", "if n > 2:\n", " print(\"bye\")\n", "else:\n", " print(\"something\")\n", "print(\"end of if structure\")" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hi\n", "end of if structure\n" ] } ], "source": [ "n = 100\n", "if n % 2 == 0:\n", " print(\"hi\")\n", "elif n > 2:\n", " print(\"bye\")\n", "else:\n", " print(\"something\")\n", "print(\"end of if structure\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## The function input()" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Please enter an integer: 3000\n", "3000 \n" ] } ], "source": [ "s = input(\"Please enter an integer: \")\n", "print(s, type(s))" ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Please enter an integer: 3000\n", "3000 \n" ] } ], "source": [ "num = int(input(\"Please enter an integer: \"))\n", "print(num, type(num))" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Please enter an integer: michal\n" ] }, { "ename": "ValueError", "evalue": "invalid literal for int() with base 10: 'michal'", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mnum\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0minput\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Please enter an integer: \"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 2\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnum\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtype\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnum\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;31mValueError\u001b[0m: invalid literal for int() with base 10: 'michal'" ] } ], "source": [ "num = int(input(\"Please enter an integer: \"))\n", "print(num, type(num))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "num = int(input(\"Please enter an integer: \"))\n", "print(num, type(num))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Introducing loops!\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### While loop example: print all integers between 0 to 9" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "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", "out of loop, value of i is 10\n" ] } ], "source": [ "i = 0\n", "while i < 10:\n", " print(i)\n", " i = i + 1\n", "print(\"out of loop, value of i is\", i)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Counting the number of zeros in a given integer (solution 1)" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "This program will tell you how many 0's are in an integer\n", "Please enter an integer: 0\n", "0 has 0 0-s\n" ] } ], "source": [ "print(\"This program will tell you how many 0's are in an integer\")\n", "orig_num = int(input(\"Please enter an integer: \"))\n", "\n", "num = orig_num\n", "cnt = 0\n", "\n", "while num > 0:\n", " if num % 10 == 0:\n", " cnt = cnt + 1 #cnt += 1\n", " num = num // 10 #num //= 10\n", "\n", "print(orig_num, \"has\", cnt, \"0-s\")\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(\"This program will tell you how many 0's are in an integer\")\n", "orig_num = int(input(\"Please enter an integer: \"))\n", "\n", "num = orig_num\n", "cnt = 0\n", "\n", "if num == 0:\n", " cnt = 1\n", "\n", "while num > 0:\n", " if num%10 == 0:\n", " cnt = cnt + 1 # cnt += 1\n", " num = num // 10 #num //= 10\n", "\n", "print (orig_num, \"has\", cnt , \"0-s\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### For loop example:" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "m\n", "\n", "mmmm\n", "i\n", "\n", "iiii\n", "c\n", "\n", "cccc\n", "h\n", "\n", "hhhh\n", "a\n", "\n", "aaaa\n", "l\n", "\n", "llll\n" ] } ], "source": [ "for x in \"michal\":\n", " print(x)\n", " print(type(x))\n", " print(x * 4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Counting the number of zeros in a given integer (solution 2)" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "This program will tell you how many 0's are in an integer\n", "Please enter an integer: 1025\n", "1025 has 1 0-s\n" ] } ], "source": [ "print(\"This program will tell you how many 0's are in an integer\")\n", "num_str = input(\"Please enter an integer: \")\n", "\n", "cnt = 0\n", "\n", "for digit in num_str:\n", " if digit == \"0\":\n", " cnt += 1\n", "\n", "print (num_str, \"has\", cnt, \"0-s\")\n", "\n", "\n" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "This program will tell you how many 0's are in an integer\n", "Please enter an integer: -10\n", "-10 has 1 0-s\n" ] } ], "source": [ "print(\"This program will tell you how many 0's are in an integer\")\n", "num_str = input(\"Please enter an integer: \")\n", "cnt = 0\n", "\n", "for digit in num_str:\n", " if digit == \"0\":\n", " cnt = cnt + 1\n", "\n", "print(num_str, \"has\", cnt, \"0-s\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Counting the number of zeros in a given integer (solution 3)" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Please enter an integer: 10034\n", "10034 has 2 0's\n" ] } ], "source": [ "num_str = input(\"Please enter an integer: \")\n", "cnt = str.count(num_str, \"0\") \n", "#cnt = num_str.count(\"0\")\n", "print (num_str, \"has\", cnt, \"0's\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "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.6.2" } }, "nbformat": 4, "nbformat_minor": 1 }