{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# CS1001.py, Tel-Aviv University, Fall 2017-2018" ] }, { "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 (available here) 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": [ "## Types and Variables" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "\n", "\n" ] } ], "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" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Operators" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(\"4\")\n" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3.3333333333333335" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "10/3\n" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "10//3" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9.0" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "4+5.0" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.4142135623730951" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2**0.5 " ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3.0" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "9**0.5" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'45'" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"4\"+\"5\" " ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'444'" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"4\"*3" ] }, { "cell_type": "code", "execution_count": 4, "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": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "10%2" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2%10" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "8" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(-2)%10" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3 < 4\n" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3 < 4 and 3 >5\n" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5\n", "34\n", "45\n", "4\n" ] } ], "source": [ "x = 345\n", "print (x%10)\n", "print (x//10)\n", "print (x%100)\n", "print ((x//10)%10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Conversions" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int(3.14)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int(\"3\")" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3.0" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "float(\"3\")" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3.14" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "float(\"3.14\")" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'3'" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "str(3)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'3.14'" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "str(3.14)" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "## If-else structure" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "no intro to CS\n" ] } ], "source": [ "today = \"Tuesday\"\n", "strike = \"No\"\n", "my_recitation = \"Tuesday\"\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": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hi\n", "out of if statement\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(\"out of if statement\")" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hi\n", "bye\n", "out of if statement\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(\"out of if statement\")" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "bye\n", "out of if statement\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(\"out of if statement\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## The function input()" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Please enter an integer: 300\n", "300 \n" ] } ], "source": [ "s = input(\"Please enter an integer: \")\n", "print(s, type(s))" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Please enter an integer: 300\n", "300 \n" ] } ], "source": [ "num = int(input(\"Please enter an integer: \"))\n", "print(num, type(num))" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Please enter an integer: 300txt\n" ] }, { "ename": "ValueError", "evalue": "invalid literal for int() with base 10: '300txt'", "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: '300txt'" ] } ], "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" ] }, { "cell_type": "code", "execution_count": 21, "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", "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": 20, "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 1 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", "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\")\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### For loop example:" ] }, { "cell_type": "code", "execution_count": 21, "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": 17, "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: 1001\n", "1001 has 2 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", "for digit in num_str:\n", " if digit == \"0\":\n", " cnt += 1\n", " \n", "print (num_str, \"has\", cnt, \"0's\")\n" ] }, { "cell_type": "code", "execution_count": 18, "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", "for digit in num_str:\n", " if digit == \"0\":\n", " cnt += 1\n", " \n", "print (num_str, \"has\", cnt, \"0's\")\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Counting the number of zeros in a given integer (solution 3)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Please enter an integer: 1001\n", "1001 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\")\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.6.2" } }, "nbformat": 4, "nbformat_minor": 1 }