{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Types & Programs\n", "\n", "Two somewhat unrelated topics, but important nonetheless are _Types_ and _Programs_." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Types\n", "\n", "We'll learn a lot of types throughout this quarter, but at this point there are four that we know about:\n", "\n", "* Integer (whole numbers): int\n", "* Floating point (approximate): float\n", "* Boolean (yes/no): bool\n", "* String (arbitrary text): str" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can inspect the type of a given value using the `type` function:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "float" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(4.0)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(\"Hello\")" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(42)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "bool" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(3 < 5)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = 3\n", "type(x)\n", "type(3)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "float" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = 4.0\n", "type(x)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = \"Hello\"\n", "type(x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Using different types in an expression _usually_ doesn't work. But some data types \"kind of look the same from a distance.\" For example, floats and ints are both **numeric**, and so can be used together. However, change happens!" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "7.0" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3 + 4.0" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3 < 4.0" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.75" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3 / 4.0" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.30000000000000004" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "0.1 + 0.1 + 0.1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Strings are especially hard to combine with other data types as they're quite different. (Consider: what would it mean to do `\"Hello\" - 3`?)." ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "unsupported operand type(s) for -: 'str' and 'int'", "output_type": "error", "traceback": [ "\u001b[31m---------------------------------------------------------------------------\u001b[39m", "\u001b[31mTypeError\u001b[39m Traceback (most recent call last)", "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[20]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m \u001b[33;43m\"\u001b[39;49m\u001b[33;43mHello\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m \u001b[49m\u001b[43m-\u001b[49m\u001b[43m \u001b[49m\u001b[32;43m3\u001b[39;49m\n", "\u001b[31mTypeError\u001b[39m: unsupported operand type(s) for -: 'str' and 'int'" ] } ], "source": [ "\"Hello\" - 3" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "unsupported operand type(s) for +: 'int' and 'str'", "output_type": "error", "traceback": [ "\u001b[31m---------------------------------------------------------------------------\u001b[39m", "\u001b[31mTypeError\u001b[39m Traceback (most recent call last)", "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[21]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m \u001b[32;43m3\u001b[39;49m\u001b[43m \u001b[49m\u001b[43m+\u001b[49m\u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43ma\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\n", "\u001b[31mTypeError\u001b[39m: unsupported operand type(s) for +: 'int' and 'str'" ] } ], "source": [ "3 + \"a\"" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "unsupported operand type(s) for -: 'str' and 'int'", "output_type": "error", "traceback": [ "\u001b[31m---------------------------------------------------------------------------\u001b[39m", "\u001b[31mTypeError\u001b[39m Traceback (most recent call last)", "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[22]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m \u001b[33;43m\"\u001b[39;49m\u001b[33;43maaa\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m \u001b[49m\u001b[43m-\u001b[49m\u001b[43m \u001b[49m\u001b[32;43m3\u001b[39;49m\n", "\u001b[31mTypeError\u001b[39m: unsupported operand type(s) for -: 'str' and 'int'" ] } ], "source": [ "\"aaa\" - 3" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "unsupported operand type(s) for /: 'str' and 'int'", "output_type": "error", "traceback": [ "\u001b[31m---------------------------------------------------------------------------\u001b[39m", "\u001b[31mTypeError\u001b[39m Traceback (most recent call last)", "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[23]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m \u001b[33;43m\"\u001b[39;49m\u001b[33;43maaa\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m \u001b[49m\u001b[43m/\u001b[49m\u001b[43m \u001b[49m\u001b[32;43m3\u001b[39;49m\n", "\u001b[31mTypeError\u001b[39m: unsupported operand type(s) for /: 'str' and 'int'" ] } ], "source": [ "\"aaa\" / 3" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'aaa'" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"a\" * 3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Some combinations and operations give potentially unexpected results:" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.75" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3 / 4" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3 // 4" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3 + True" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3 + False" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "False + True" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "ename": "ZeroDivisionError", "evalue": "division by zero", "output_type": "error", "traceback": [ "\u001b[31m---------------------------------------------------------------------------\u001b[39m", "\u001b[31mZeroDivisionError\u001b[39m Traceback (most recent call last)", "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[31]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m \u001b[32;43m3\u001b[39;49m\u001b[43m \u001b[49m\u001b[43m/\u001b[49m\u001b[43m \u001b[49m\u001b[32;43m0\u001b[39;49m\n", "\u001b[31mZeroDivisionError\u001b[39m: division by zero" ] } ], "source": [ "3 / 0" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3.0" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3 / (2 < 5)" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3 // 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Programs\n", "\n", "All of the homework assignments in CSE 160 deal with \"programs.\" A program is like a recipe, allowing you to save the instructions and when you want to use the program, runs the instructions from top to bottom.\n", "\n", "(NB: Notebooks deal with \"cells\" which exist somewhere between single lines of code and programs. Refer to this lecture's recording for discussion.)\n", "\n", "Programs do not automatically print anything out, and so we must make use of the `print` function anywhere we need to see the results." ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [], "source": [ "x = 2 * 3\n", "y = x / 5\n", "# nothing printed out" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "6 1.2\n" ] } ], "source": [ "x = 2 * 3\n", "y = x / 5\n", "print(x, y)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Print\n", "\n", "The `print` function comes in a few forms, allowing for combining expressions, strings, and variables in different ways for output. `print`'s arguments will always be some kind of expression -- something evaluating to a value." ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello World\n" ] } ], "source": [ "print(\"Hello World\")" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "42\n" ] } ], "source": [ "print(\"42\")" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "9\n" ] } ], "source": [ "print(3 + 6)" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Value of x is 3 and the value of y is 5\n" ] } ], "source": [ "x = 3\n", "y = 5\n", "print(\"Value of x is\", x, \"and the value of y is\", y)" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "unsupported operand type(s) for +: 'int' and 'str'", "output_type": "error", "traceback": [ "\u001b[31m---------------------------------------------------------------------------\u001b[39m", "\u001b[31mTypeError\u001b[39m Traceback (most recent call last)", "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[42]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m \u001b[43mx\u001b[49m\u001b[43m \u001b[49m\u001b[43m+\u001b[49m\u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43ma\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\n", "\u001b[31mTypeError\u001b[39m: unsupported operand type(s) for +: 'int' and 'str'" ] } ], "source": [ "x + \"a\"" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'3'" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "str(x)" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'3a'" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "str(x) + \"a\"" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Value of x is 3 and the value of y is 5\n" ] } ], "source": [ "x = 3\n", "y = 5\n", "print(\"Value of x is \" + str(x) + \" and the value of y is \" + str(y))" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Value of x is 3 and the value of y is 5\n" ] } ], "source": [ "x = 3\n", "y = 5\n", "print(f\"Value of x is {x} and the value of y is {y}\")" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "What if I wrote a haiku about a print function.\n" ] } ], "source": [ "print(\"What if I wrote a haiku about a print function.\")" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "What if I\n", "wrote a haiku about a\n", "print function.\n" ] } ], "source": [ "print(\"What if I\\nwrote a haiku about a\\nprint function.\")" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\\n\n" ] } ], "source": [ "print(\"\\\\n\")" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "HelloWorld\n" ] } ], "source": [ "print(\"Hello\", end=\"\")\n", "print(\"World\")" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "print(\"\")" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello\n", "\n", "World\n" ] } ], "source": [ "print(\"Hello\")\n", "print()\n", "print(\"World\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": ".venv", "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.13.2" } }, "nbformat": 4, "nbformat_minor": 2 }