{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ecdb9492",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Types\n",
    "#### CS 65: Introduction to Computer Science I"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0383cdec",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Values we've seen so far\n",
    "\n",
    "So far, we've seen different kinds of data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a5b9d421",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello world!\n"
     ]
    }
   ],
   "source": [
    "num_students = 42 #whole numbers\n",
    "avg_gas_estimate = 2.70 #numbers with decimal\n",
    "print(\"Hello world!\") #text data"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dabbe6af",
   "metadata": {},
   "source": [
    "All data has a __type__ which defines what kind of thing it is and what you can do with it."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b6f64bfd",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Names of some types\n",
    "\n",
    "__integer:__ whole numbers\n",
    "\n",
    "__floating point:__ numbers with decimal points\n",
    "\n",
    "__string:__ text data\n",
    "\n",
    "The `type()` function can be used to tell you what kind of type something is. \n",
    "\n",
    "Both values and variables can have types."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "270933e6",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "float"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(2.70)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "b058d225",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "float"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(avg_gas_estimate)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "cbd45fb8",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "str"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(\"Hello world\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "21c386e8",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "int"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(num_students)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "863d6688",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Expressions on strings\n",
    "\n",
    "We've seen how arithmetic operators work on numerical types like ints and floats.\n",
    "\n",
    "Strings have their own operators too - and some look like arithmetic operators!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "c101ad84",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'HelloEric'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "greeting = \"Hello\"\n",
    "name = \"Eric\"\n",
    "greeting+name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "be2ab0bd",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "unsupported operand type(s) for -: 'str' and 'str'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-7-ea5c4b3c4eb7>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mgreeting\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0mname\u001b[0m \u001b[0;31m#not allowed\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 -: 'str' and 'str'"
     ]
    }
   ],
   "source": [
    "greeting-name #not allowed"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "92ccb283",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Can't apply all string operations to numbers\n",
    "\n",
    "Similarly, you can't do everything with numbers that you can with strings."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "14b045d6",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "71465b95",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "object of type 'int' has no len()",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-9-5c4b8b950dfb>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnum_students\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m: object of type 'int' has no len()"
     ]
    }
   ],
   "source": [
    "len(num_students)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "36c95a33",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Other types we'll see\n",
    "\n",
    "There are several other built-in types that we'll hear more about later like\n",
    "\n",
    "__Boolean:__ can have value `True` or `False`\n",
    "\n",
    "__List:__ for keeping track of a sequence of values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "43131248",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "bool"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "c8262932",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "list"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type([1,2,3])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c5c7bd08",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "You can even make your own types or use those that others have created."
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Slideshow",
  "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.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
