{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "### Types and `type`\n", "\n", "Values in Python have different *types*. You can find the type of a value using `type`" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(42)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In Python, the integer type is called `int`" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "float" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(3.14)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(\"Hello\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In Python, the string type is called `str`" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that an expression like `40 + 2` is evaluated to a value with a type" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(40 + 2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Something like this would also work:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "n = 5\n", "type(n + 2)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(n)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Floats" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If we perform division, we will get a type that's possibly different from the type of the values in the expression:" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "float" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(5/2)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "float" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(4/2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Why is `type(4/2)` still `float`, even though 4 is divisible by 2? For consistency. Division always produces a `float`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Multiplying a string by an integer produces a string (remember that `\"ha\" * 2` is `\"haha\"`. Multiplying an integer by a `float` produces a `float` (think about this like this: if we multiply a fraction by an integer, we can't be sure we'll get an integer. For consistency, we'll *always* produce a `float`)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "float" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(2 * 3.14)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can go a little crazy:" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "type" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(type(42))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Functions are also objects and have a type" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "builtin_function_or_method" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(print)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here are the types of values that we've seen so far:\n", "Integer (`int`): a whole number (positive or negative). In Python 3 (which we are using), `int`s can be as large as you like, as long as there is enough space in memory to store them." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Floating-point number (`float`): a fractional number (like 3.14, 5.0, 4.0, or 10000.5). In the computer, floats are stored in scientific notation, with up to about 16 (decimal) digits in the mantissa and about 3 digits in the exponent (the exponent can go from about -300 to 300). \n", "\n", "(Reminder about scientific notation: in scientific notation, numbers are stored in the format $m\\times b^n$. For example, Avogadro's number is $6.02\\times 10^23 mol^{-1}$. $m$ is called the *mantissa*, and $n$ is called the exponent, and $b$ is called the base.)\n", "\n", "This means that we can't have numbers that are too large, or too close to 0, and can't store number with more than 16 digits of precision." ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "1e+300" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1.0 * 10**300" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false }, "outputs": [ { "ename": "OverflowError", "evalue": "int too large to convert to float", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mOverflowError\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[0;36m1.0\u001b[0m \u001b[0;34m*\u001b[0m \u001b[0;36m10\u001b[0m\u001b[0;34m**\u001b[0m\u001b[0;36m350\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mOverflowError\u001b[0m: int too large to convert to float" ] } ], "source": [ "1.0 * 10**350" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Strings" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Strings are another type that we saw. Expressions like `\"Hi\"`, `\"H2O\"`, and `'Hi' + \"there\"` are all of type `str`. You can enclose text in single or double quotes, as you as you are consistent. The string must be written on a single line:" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'abc'" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'abc'" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'abc'" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"abc\"" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false }, "outputs": [ { "ename": "SyntaxError", "evalue": "EOL while scanning string literal (, line 1)", "output_type": "error", "traceback": [ "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m \"ab\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m EOL while scanning string literal\n" ] } ], "source": [ "\"ab\n", "c\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You *can* store strings that contain multiple lines:" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'a\\nbcd\\nef'" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'''a\n", "bcd\n", "ef'''" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(Recall that we can have values written in the text of Python programs that Python will simply ignore. We sometimes have multi-line strings in programs that basically act as comments. You saw this with docstrings, although docstrings are treated in a special way in Python)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`\\n` is a special character. It means \"switch to a new line.\"" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ab\n", "cd\n" ] } ], "source": [ "print(\"ab\\ncd\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Suppose we want quotes inside quotes:" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": false }, "outputs": [ { "ename": "SyntaxError", "evalue": "invalid syntax (, line 1)", "output_type": "error", "traceback": [ "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m \"Getting into medical school is \"easy\"\"\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" ] } ], "source": [ "\"Getting into medical school is \"easy\"\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "That didn't work! The reason is that Python reads from left to right. `\"Getting into medical school is \"` makes sense, but the `easy` that follows makes no sense to Python." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There are two solutions: one is to mix single quotes and double quotes:" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'Getting into medical school is \"easy\"'" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'Getting into medical school is \"easy\"'" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "\"Getting into medical school is `easy'\"" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"Getting into medical school is `easy'\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Another is to use `\\\"` inside double quotes to let Python know that you don't mean to terminate the string" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'Getting into medical school is \"easy\"'" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"Getting into medical school is \\\"easy\\\"\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(Note that Python uses the mixed quotes notation)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(But how do you put forward slashes in quotes? Use `\"\\\\\"` for a single forward slash.)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Converting values to different types\n", "\n", "We already saw an example of converting from one type to another when we used `n = float(\"Give me a number\")`. You can convert a string to a float:" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "3.14" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "float(\"3.14\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(You can tell the value is a float because of the lack of quotes when the value is displayed in the shell. But note that if we use `print()`, both string and floats are printed without quotes)" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3.14\n", "3.14\n" ] } ], "source": [ "print(float(\"3.14\"))\n", "print(\"3.14\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Converting from a string to a `float` is useful if we want to perform arithmetic operations on a value that is stored as text (for example, because we got the value as input from a user)" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4242\n", "84.0\n" ] } ], "source": [ "print(\"42\" * 2)\n", "print(float(\"42\") * 2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If we try to convert something that's not a number to a float, we'll get an error" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "collapsed": false }, "outputs": [ { "ename": "ValueError", "evalue": "could not convert string to float: 'forty-two'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mValueError\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[0mfloat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"forty-two\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mValueError\u001b[0m: could not convert string to float: 'forty-two'" ] } ], "source": [ "float(\"forty-two\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Converting floats to string makes sense if, for example, we want to use \"string addition\":" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "collapsed": false }, "outputs": [ { "ename": "TypeError", "evalue": "Can't convert 'int' object to str implicitly", "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[0;34m\"abc\"\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;36m123\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: Can't convert 'int' object to str implicitly" ] } ], "source": [ "\"abc\" + 123" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "But:" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'abc123'" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"abc\" + str(123)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Floats and strings can be converted to integers:" ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int(3.14)" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int(3.75)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(Integers are *truncated* rather than rounded)" ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "42" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int(\"42\")" ] }, { "cell_type": "code", "execution_count": 36, "metadata": { "collapsed": false }, "outputs": [ { "ename": "ValueError", "evalue": "invalid literal for int() with base 10: '42.3'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mValueError\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[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"42.3\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mValueError\u001b[0m: invalid literal for int() with base 10: '42.3'" ] } ], "source": [ "int(\"42.3\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Doesn't work: the string must contain an integer to be converted. But we have the tools to do this:" ] }, { "cell_type": "code", "execution_count": 37, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "42" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int(float(\"42.3\"))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Converting strings to lists and using strings as iterables\n", "\n", "You can convert strings to lists:" ] }, { "cell_type": "code", "execution_count": 38, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "['a', 'b', 'c']" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(\"abc\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We got a list that consists of the individual characters of the string. This suggests a way to find out the length of the string, and get the individual character of a string (by going `list(\"abc\")[1]`). But in fact, there is an easier way: you can just treat the string the way we treated lists:" ] }, { "cell_type": "code", "execution_count": 39, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'b'" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = \"abc\"\n", "s[1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In fact, you can directly go" ] }, { "cell_type": "code", "execution_count": 40, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'c'" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"abc\"[2]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can also find the length of a string (i.e., the number of characters the string has) like so:" ] }, { "cell_type": "code", "execution_count": 41, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(\"hello\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Booleans\n", "\n", "Another type that we already so was Booleans (`bool` type in Python). Those values can be either `True` or `False`. We already saw this kind of thing:" ] }, { "cell_type": "code", "execution_count": 42, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 == 3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In fact, `2 == 3` is an expression in the same way that `2 + 3` is an expression, and both of those are evaluated to a value. In the same way that we can say `sum23 = 2 + 3` we can go" ] }, { "cell_type": "code", "execution_count": 43, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n" ] } ], "source": [ "are_equal_2_3 = (2 == 3)\n", "print(are_equal_2_3)" ] }, { "cell_type": "code", "execution_count": 44, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "x = are_equal_2_3\n", "print(x == are_equal_2_3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Generally, we would see Boolean values used in conditionals. A Boolean value is what goes after the if." ] }, { "cell_type": "code", "execution_count": 45, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The world makes sense\n" ] } ], "source": [ "if x == are_equal_2_3:\n", " print(\"The world makes sense\")\n", "else:\n", " print(\"The world doesn't make sense\")" ] }, { "cell_type": "code", "execution_count": 46, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The world makes sense\n" ] } ], "source": [ "if are_equal_2_3:\n", " print(\"The world doesn't make sense\")\n", "else:\n", " print(\"The world makes sense\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(We can check that the values are indeed of type `bool`:" ] }, { "cell_type": "code", "execution_count": 47, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "bool" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(2 != 3)" ] }, { "cell_type": "code", "execution_count": 48, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "bool" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(False)" ] } ], "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.5.2" } }, "nbformat": 4, "nbformat_minor": 0 }