{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Author: [James Bourbeau](http://www.jamesbourbeau.com)" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2016-12-29 \n", "\n", "CPython 3.5.2\n", "IPython 5.0.0\n" ] } ], "source": [ "%load_ext watermark\n", "%watermark -d -v" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "# Survey of some built-in types in Python\n", "\n", "Let's get a feel for some of the built-in types in python. Specifically, we'll discuss some of the most commonly used types, how to change the type of an object, and how some numeric operations behave between different types. If you would like to know the type of something, you can always use python's built-in `type()` function.\n", "\n", "1. [Built-in types](#Built-in-types)\n", " 1. [Numeric types](#Numeric-types)\n", " 2. [Boolean type](#Boolean-type)\n", " 3. [Sequence types](#Sequence-types)\n", " 4. [Mapping types](#Mapping-types)\n", "2. [Type casting](#Type-casting)\n", "3. [Numeric operations](#Numeric-operations)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Built-in types\n", "[[back to top](#top)]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Numeric types\n", "\n", "In python there are three basic numerical types: integers, floating-point, and complex numbers. These types are denoted by `int`, `float`, and `complex`, repsectively. Generally speaking, integers represent whole numbers ( ...,-2, -1, 0, 1, 2,...), floats represent decimal numbers, and complex are, well, complex numbers (where the real and imaginary parts are both floats). Information regarding the precision of floating-point can be found via" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "sys.float_info(max=1.7976931348623157e+308, max_exp=1024, max_10_exp=308, min=2.2250738585072014e-308, min_exp=-1021, min_10_exp=-307, dig=15, mant_dig=53, epsilon=2.220446049250313e-16, radix=2, rounds=1)" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import sys\n", "sys.float_info" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Numbers without a decimal point produce integers, while number that include a decimal (or an exponent sign) produce floats." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = 2\n", "type(a)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "float" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b = 6.47\n", "type(b)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "float" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c = 9e8\n", "type(c)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A complex number can be created by including a 'j' or 'J' in a number. The corresponding real and imaginary parts of the complex number can be accessed using `real` and `imag` attributes." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "complex" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "z = 7 + 4.3j\n", "type(z)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "7.0" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "z.real" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "4.3" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "z.imag" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that the real and imaginary parts for the `complex` type are floating-point numbers--regardless of whether or not there is a decimal point. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---\n", "There are other built-in numeric types such as `fractions` and `decimal`, but I find `int`, `float`, and `complex` to be the most commonly used." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Boolean type\n", "\n", "Booleans are actually a subtype of integers with values represent one of two constant values: `True` or `False`. More will be discussed about booleans later on when we discuss type casting." ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "bool" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "n = True\n", "type(n)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "bool" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p = False\n", "type(p)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Sequence types\n", "\n", "Some commonly used sequence types are the list, tuple, and string. Denoted by `list`, `tuple`, and `str`, respectively. These can be thought of as an ordered containers that can store several different items. It is important to note that the items that are stored in a list or tuple need not be of the same type." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Lists are can be constructed in several ways. However, using square brackets with list items seperated by commas is very common. " ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "list" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d = [1, 2.3, 5, 74.7]\n", "type(d)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Tuples can be construced similarly, but with parenthesis instead of square brackets. " ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "tuple" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f = (83.2, -4 ,5e7)\n", "type(f)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "One weird quirk is that a tuple with a single item needs to have a trailing comma, e.g." ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(83.2,)\n" ] }, { "data": { "text/plain": [ "tuple" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f = (83.2,)\n", "print(f)\n", "type(f)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If this trailing comma is left out, then python will assume you don't actually want a tuple and will assign whatever the single item is in parenthesis to your variable. For example, " ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "83.2\n" ] }, { "data": { "text/plain": [ "float" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f = (83.2)\n", "print(f)\n", "type(f)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Each item in a sequence has a number to represent its location in the sequence. This number, called the index, starts at 0 for the first item, 1 for the second item, and so on. One can access an individual item in a sequence by using square brackets and the item's index. For example, the first item in the `d` list is" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d[0]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The second item in the `d` list is" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "2.3" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d[1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The third item in the `d` list is" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d[2]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "and so on..." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The length of a sequence, the number of items is contains, can be found with the built-in `len()` function. " ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2.3, 5, 74.7]\n" ] }, { "data": { "text/plain": [ "4" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "print(d)\n", "len(d)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---\n", "Up to this point, it may seem like lists and tuples aren't any different. They are both containers that can hold items, you can access the items with an index, etc. How are these things different? The difference between the `list` type and the `tuple` type is that **lists are mutable, while tuples are immutable**. Mutable objects are objects that can be changed in place, while immutable objects cannot be changed in place. Let's look at an example. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's create a list" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 3, 4]\n" ] } ], "source": [ "g = [1, 2, 3, 4]\n", "print(g)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now let's modify the list in place. That is, let's try to change the items in the list without creating a whole new list. " ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[99, 2, 3, 4]\n" ] } ], "source": [ "g[0] = 99\n", "print(g)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As you can see, there wasn't a problem here. We assigned to the variable `g` the list `[1, 2, 3, 4]`, then modified the first item in the `g` list to be the number `99`. This is an example of a mutable object—it can be changed. Let's try the same thing with a `tuple` now." ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(1, 2, 3, 4)\n" ] } ], "source": [ "g = (1, 2, 3, 4)\n", "print(g)" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": false }, "outputs": [ { "ename": "TypeError", "evalue": "'tuple' object does not support item assignment", "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[0mg\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m99\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" ] } ], "source": [ "g[0] = 99\n", "print(g)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We got this error because tuples are immutable—they can't be modified once they're created." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### String type\n", "\n", "Strings are another sequence type and are used to represent a sequence of characters. Strings can be created by enclosing characters in either single or double quotes. " ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "g = 'pizza'\n", "type(g)" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "h = \"jamesbond007\"\n", "type(h)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Because a string is a sequence, it can also be index just like lists and tuples. " ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'j'" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "h[0]" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'a'" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "h[1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can also find out how many characters are in a string using the `len()` function" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "12" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(h)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Mapping types" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In python, there is only one mapping type: the dictionary. It is denoted by `dict`. Dictionaries are unordered containers for key-value pairs. That is, for each key in a dictionary there is an associated value. Dictionaries are created by placing comma-separated key-value pairs inside curly brackets `{}`. For a key-value pair, the key and corresponding value are seperated by a colon, `:`.\n", "\n", "An example might help..." ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "dict" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "k = {'key1': 23, 'key2': -53.2, 'key3': 'Tokyo'}\n", "type(k)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here, the dictionary keys are 'key1', 'key2', and 'key3', with corresponding values of 23, -53.2, and 'Tokyo'. In a similar way to sequences, you can access the values in a dicionary by giving the corresponding key in square brackets. " ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "23" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "k['key1']" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "-53.2" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "k['key2']" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'Tokyo'" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "k['key3']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The size of a dictionary, the number of key-value pairs is contains, can be found with the built-in `len()` function. " ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(k)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It is important to note that in the previous example all the keys were strings, but this doesn't have to be the case. The only restriction on keys is that they be an *immutable type*. For example, the following is also an acceptable dictionary. " ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "collapsed": true }, "outputs": [], "source": [ "m = {-23: [1, 2, 3, 4], 'desk': 3.2, 7.12: (-3, 'bird')}" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4]" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m[-23]" ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "3.2" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m['desk']" ] }, { "cell_type": "code", "execution_count": 36, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(-3, 'bird')" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m[7.12]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let see what happens if I try to contruct a dictionary with a list key" ] }, { "cell_type": "code", "execution_count": 37, "metadata": { "collapsed": false }, "outputs": [ { "ename": "TypeError", "evalue": "unhashable type: 'list'", "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[0mn\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m{\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m3\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;34m'WOO'\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: unhashable type: 'list'" ] } ], "source": [ "n = {[1, 2, 3]: 'WOO'}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Whoops lists are mutable! So remember to always use immutable objects for dictionary keys!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Type casting\n", "[[back to top](#top)]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Sometimes it can be useful to manually change the type of an object. In python, this so-called 'type-casting' can be accomplished using several built-in functions:\n", "\n", "* `int()`—casts to integer\n", "* `float()`—casts to float\n", "* `str()`—casts to string\n", "* `bool()`—casts to boolean\n", "\n", "Let's see it in action" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Casting integers to floats is fairly straight-forward" ] }, { "cell_type": "code", "execution_count": 38, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2.0\n" ] }, { "data": { "text/plain": [ "float" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = float(2)\n", "print(a)\n", "type(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When casting a float to an integer, python will round down to the nearest integer " ] }, { "cell_type": "code", "execution_count": 39, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "78\n" ] }, { "data": { "text/plain": [ "int" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b = int(78.81)\n", "print(b)\n", "type(b)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can even cast a number to a string! (This effectively just returns the number in quotes)" ] }, { "cell_type": "code", "execution_count": 40, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "-1324.1\n" ] }, { "data": { "text/plain": [ "str" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c = str(-1324.1)\n", "print(c)\n", "type(c)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Things get a little less straight-forward when casting to `bool`. Below are the `bool` casting rules:\n", "\n", "* Numbers: Zero of any numeric type, for example, `0`, `0.0`, `0j`, cast to `False`. Everything else casts to `True`.\n", "* Sequences: Any empty sequence, for example, `()`, `[]`, `''`, cast to `False`. Everything else casts to `True`.\n", "* Mapping: The empty dictionary, `{}`, casts to `False`. Everything else casts to `True`.\n", "\n", "Here are some examples:" ] }, { "cell_type": "code", "execution_count": 41, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool(0)" ] }, { "cell_type": "code", "execution_count": 42, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool(178.3)" ] }, { "cell_type": "code", "execution_count": 43, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool([])" ] }, { "cell_type": "code", "execution_count": 44, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool([1, 2, 3, 4, 5])" ] }, { "cell_type": "code", "execution_count": 45, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool({})" ] }, { "cell_type": "code", "execution_count": 46, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool({'key': 'value'})" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Numeric operations\n", "[[back to top](#top)]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The following numerical operations are supported for numeric types:\n", "\n", "* Addition: `+`\n", "* Subtraction: `-`\n", "* Multiplication: `*`\n", "* Division: `/`\n", "* Floored division: `//`\n", "* Exponentiation: `**`\n", "\n", "Some examples..." ] }, { "cell_type": "code", "execution_count": 47, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1+1" ] }, { "cell_type": "code", "execution_count": 48, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "7" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "10-3" ] }, { "cell_type": "code", "execution_count": 49, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "15.0" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3*5.0" ] }, { "cell_type": "code", "execution_count": 50, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "2.5" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "5/2" ] }, { "cell_type": "code", "execution_count": 51, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "5//3" ] }, { "cell_type": "code", "execution_count": 52, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "81.0" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "9.0**2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When performing numeric operations, the type of numbers *does* matter. According to the [Python Software Foundation](https://docs.python.org/3/library/stdtypes.html#numeric-types-int-float-complex): \n", "\n", "> Python fully supports mixed arithmetic: when a binary arithmetic operator has operands of different numeric types, the operand with the 'narrower' type is widened to that of the other, where integer is narrower than floating point, which is narrower than complex. \n", "\n", "So, when you have a numeric operation with mixed numeric types, say adding an `int` and a `float`, the result will have the 'widest' type of the two numbers, in this case `float`. The convention is `int` is the most narrow type, `float` is wider than `int`, and `complex` is wider than `float`. \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.5.2" } }, "nbformat": 4, "nbformat_minor": 0 }