{ "cells": [ { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello, world!\n" ] } ], "source": [ "print(\"Hello, world!\") # shift+enter" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "x = 10 # enter = new line\n", "y = 20\n", "z = 30" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10\n" ] } ], "source": [ "print(x)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x # if the final line of a cell is an expression... that expression is returned" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1+1\n", "2+2\n", "3+3" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "x = 100\n", "y = 'abcd'" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "100" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'abcd'" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(x)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(y)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "x = 'wxyz'\n", "y = 12345" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(x)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(y)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "x = 1\n", "y = 2" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x + y" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "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\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0my\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'2'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0mx\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0my\u001b[0m\u001b[0;34m\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": [ "x = 1\n", "y = '2'\n", "\n", "x + y" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'ab'" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'a' + 'b'" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 + 1" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [], "source": [ "# dynamically typed\n", "# strongly typed\n", "\n", "x = 10\n", "x = 20\n", "x = 30" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "30" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hello, Reuven'" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "name = 'Reuven'\n", "\"Hello, \" + name" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Enter your name: Reuven\n" ] } ], "source": [ "name = input(\"Enter your name: \")" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Reuven'" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "name" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hello, Reuven'" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"Hello, \" + name" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [], "source": [ "# in Python 2, use raw_input ... don't use input\n" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello, Reuven\n" ] } ], "source": [ "print(\"Hello, \" + name)" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Enter your age: 48\n" ] } ], "source": [ "age = input(\"Enter your age: \")" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Wow, you are only 48?\n" ] } ], "source": [ "print(\"Wow, you are only \" + age + \"?\")" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "can only concatenate str (not \"int\") to 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[0mage\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: can only concatenate str (not \"int\") to str" ] } ], "source": [ "age + 1" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [], "source": [ "# this is a comment\n", "\n", "x = 100 # this is also a comment" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# we are a site for babies, and thus want their ages in months, not years\n", "age = input(\"Enter your age in months: \")" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Enter your name: something else\n", "Hello, something else\n" ] } ], "source": [ "name = input(\"Enter your name: \")\n", "if name == 'Reuven':\n", " print(\"Hello, master!\")\n", " print(\"So good to see you again!\") \n", "else:\n", " print(\"Hello, \" + name)\n" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Enter a letter: c\n", "I don't know that letter.\n" ] } ], "source": [ "letter = input(\"Enter a letter: \")\n", "if letter == 'a':\n", " print(\"That's the first letter!\")\n", "elif letter == 'b':\n", " print(\"That's the second letter!\")\n", "else:\n", " print(\"I don't know that letter.\")" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Less than 10!\n" ] } ], "source": [ "x = 5\n", "if x < 10:\n", " print(\"Less than 10!\")\n", "elif x < 8:\n", " print(\"Less than 8!\")\n", "else:\n", " print(\"I don't know\")" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Enter a letter: b\n", "That's one of the first three letters!\n" ] } ], "source": [ "letter = input(\"Enter a letter: \")\n", "if (letter == 'a' or \n", " letter == 'b' or \n", " letter == 'c'):\n", " print(\"That's one of the first three letters!\")\n", "elif letter == 'd':\n", " print(\"That's the fourth letter!\")\n", "else:\n", " print(\"I don't know that letter.\")" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "It's between 5 and 20\n" ] } ], "source": [ "x = 10\n", "if (x > 5 and\n", " x < 20):\n", " print(\"It's between 5 and 20\")" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "It is not 20!\n" ] } ], "source": [ "x = 10\n", "if x != 20:\n", " print(\"It is not 20!\")" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "It is not 20!\n" ] } ], "source": [ "if not x == 20:\n", " print(\"It is not 20!\")" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Enter today's weather: oobleck\n", "I don't know about oobleck\n" ] } ], "source": [ "# Ask the user what today's weather is\n", "# And then:\n", "# (1) If it's rain or snow -- print something negative\n", "# (2) If it's sun -- print something positive\n", "# (3) If it's neither -- print that you don't know what it is\n", "\n", "weather = input(\"Enter today's weather: \")\n", "if (weather == 'rain' or \n", " weather == 'snow'):\n", " print(\"Oh, it must be winter!\")\n", "elif weather == 'sunny':\n", " print(\"Oh, what a nice day!\")\n", "else:\n", " print(\"I don't know about \" + weather)" ] }, { "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.7.1" } }, "nbformat": 4, "nbformat_minor": 2 }