Skip to content

Instantly share code, notes, and snippets.

@joshainglis
Created May 6, 2014 05:58
Show Gist options
  • Select an option

  • Save joshainglis/3b7ae9c2925a8a90edf0 to your computer and use it in GitHub Desktop.

Select an option

Save joshainglis/3b7ae9c2925a8a90edf0 to your computer and use it in GitHub Desktop.
{
"metadata": {
"name": "",
"signature": "sha256:f96ce6e9c2cb192094b3c6aae954a354c6e4b05e44715652334735ce8e85fe01"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": [
"from __future__ import division\n",
"from pylab import *"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"NOTE: This information IS NOT REQUIRED for SCIE1000."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"I have just written this to help those that were running into issues with their programming assignments, as I noticed it was a common thread."
]
},
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"A brief introduction to Data Types"
]
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"Glossary"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* **Integer**: A whole number with no decimal places. Can be positive or negative.\n",
" * `0`, `1`, `-24`, `103940249`\n",
" * Python calls this an **`int`**\n",
"* **Float**: A floating point number. A number with decimal places. Can be positive or negative\n",
" * `0.0`, `0.5`, `-10.456`, `10039402.4028457298`\n",
" * Python calls this a **`float`**"
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"An introduction to `int` and `float`"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`int` and `float` are **data types**"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"type(1)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 2,
"text": [
"int"
]
}
],
"prompt_number": 2
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"type(1.0)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 3,
"text": [
"float"
]
}
],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"x = 1\n",
"type(x)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 4,
"text": [
"int"
]
}
],
"prompt_number": 4
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"x = 1.1\n",
"type(x)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 5,
"text": [
"float"
]
}
],
"prompt_number": 5
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"Converting between types"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Python allows you to convert between data types"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"int(2.7)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 6,
"text": [
"2"
]
}
],
"prompt_number": 6
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You'll notice that converting to `int` simply keeps the number before the decimal point. It does not round to the nearest integer."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"float(2)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 7,
"text": [
"2.0"
]
}
],
"prompt_number": 7
},
{
"cell_type": "code",
"collapsed": true,
"input": [
"x = 2.7\n",
"int(x)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 8,
"text": [
"2"
]
}
],
"prompt_number": 8
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"x = 2\n",
"float(x)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 9,
"text": [
"2.0"
]
}
],
"prompt_number": 9
},
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Arrays"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**An array cannot mix `float` and `int`, it must be all of one or the other!**\n",
"\n",
"This means that every element within an array is always the same data type as every other element in the same array.\n",
"\n",
"When you create an array, its **data type** is stored with it. \n",
"\n",
"So if you make an array of integers, the array's **data type** (called a `dtype` by arrays) will be `int`.\n",
"\n",
"But if you create an array of floats, the array's `dtype` will be `float`. \n",
"\n",
"Lets have a look at this in practice"
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Examples of creating arrays"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"So to begin with, lets make a simple integer array:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"array([1,2,3,4,5])"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 10,
"text": [
"array([1, 2, 3, 4, 5])"
]
}
],
"prompt_number": 10
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You'll notice in the output there are no decimal places.\n",
"\n",
"So what about a float array:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"array([1.0 ,2.0 ,3.0 ,4.0 ,5.0])"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 11,
"text": [
"array([ 1., 2., 3., 4., 5.])"
]
}
],
"prompt_number": 11
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Everything has a decimal point, showing you that this is a an array with `dtype=float`\n",
"\n",
"Lets see what happens when we mix the `int` and `float` when we make the array"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"array([1, 2, 3, 4.1, 5])"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 12,
"text": [
"array([ 1. , 2. , 3. , 4.1, 5. ])"
]
}
],
"prompt_number": 12
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"As you can see, everything was converted to `float` and the array has `dtype=float`"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's not forget about `arange` and `zeros`!"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"arange(5)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 13,
"text": [
"array([0, 1, 2, 3, 4])"
]
}
],
"prompt_number": 13
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"zeros(5)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 14,
"text": [
"array([ 0., 0., 0., 0., 0.])"
]
}
],
"prompt_number": 14
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You'll notice that the `arange()` function returns an array with `dtype=int`, and the `zeros()` function returns an array with `dtype=float`"
]
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"Mixing types in an array"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"What happens if we try to add an integer into float array?\n",
"\n",
"Lets start by creating a float array, and assigning it to a variable called `x`"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"x = array([1.0 ,2.0 ,3.0 ,4.0 ,5.0])"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 15
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's look at it, to make sure it's what we expect"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print x"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"[ 1. 2. 3. 4. 5.]\n"
]
}
],
"prompt_number": 16
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Good! Now, lets print out the second element of x (**remember that indexes start from 0**)"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print x[1]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"2.0\n"
]
}
],
"prompt_number": 17
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now lets put an integer into that spot and see what happens..."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"x[1] = 10"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 18
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print x"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"[ 1. 10. 3. 4. 5.]\n"
]
}
],
"prompt_number": 19
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print x[1]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"10.0\n"
]
}
],
"prompt_number": 20
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can see that our integer (`10`) was converted into a float (`10.0`)!\n",
"\n",
"So lets try it the other way arround. Lets make an integer array and put a float into it.\n",
"\n",
"I'll do this in one fell swoop with a small script"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Create an integer array\n",
"y = array([1, 2, 3, 4, 5])\n",
"\n",
"# Print out the array and its second element\n",
"print 'y =', y\n",
"print 'y[1] =', y[1]\n",
"\n",
"# Now we'll assign a float to the second element\n",
"y[1] = 9.7\n",
"print 'We have assigned 9.7 to y[1]'\n",
"\n",
"# Print out the array amd 2nd element after the change\n",
"print 'y =', y\n",
"print 'y[1] =', y[1]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"y = [1 2 3 4 5]\n",
"y[1] = 2\n",
"We have assigned 9.7 to y[1]\n",
"y = [1 9 3 4 5]\n",
"y[1] = 9\n"
]
}
],
"prompt_number": 21
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You'll notice that all of the decimal information is gone! 9.7 has just been converted to 9 (it does not round up!)"
]
},
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Taking control"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"So what do you do if you want to use `arange()` but want an array of floats? Or `zeros()` but with integers?\n",
"\n",
"**This is where `dtype` jumps to centre-stage!**"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"arange(5, dtype=float)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 22,
"text": [
"array([ 0., 1., 2., 3., 4.])"
]
}
],
"prompt_number": 22
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Boom! An array of floats!"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"zeros(5, dtype=int)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 23,
"text": [
"array([0, 0, 0, 0, 0])"
]
}
],
"prompt_number": 23
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"An array of integers, just like that!"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"array([1, 2, 3, 4, 5], dtype=float)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 24,
"text": [
"array([ 1., 2., 3., 4., 5.])"
]
}
],
"prompt_number": 24
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"See where I'm going with this? :)"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"array([1.1, 2.4, 3.9, 4.16, 5.25], dtype=int)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 25,
"text": [
"array([1, 2, 3, 4, 5])"
]
}
],
"prompt_number": 25
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Other things to note"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"What happens if you multiply or divide an arrays of different types?\n",
"\n",
"Easy to remember. Float always wins!"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# I'm going to add dtypes just for emphasis!\n",
"\n",
"# an integer array\n",
"x = array([1,2,3,4,5], dtype=int)\n",
"\n",
"# and a float array\n",
"y = array([1.1, 2.2, 3.3, 4.4, 5.5], dtype=float)\n",
"\n",
"print 'Our two arrays:'\n",
"print 'x =', x\n",
"print 'y =', y\n",
"print\n",
"\n",
"print 'Multiplying them together:'\n",
"print 'x*x =', x*x\n",
"print 'y*y =', y*y\n",
"print 'x*y =', x*y\n",
"print\n",
"\n",
"print 'Dividing them:'\n",
"print 'x/x =', x/x\n",
"print 'y/y =', y/y\n",
"print 'x/y =', x/y\n",
"print\n",
"\n",
"print 'Adding them together:'\n",
"print 'x+x =', x+x\n",
"print 'y+y =', y+y\n",
"print 'x+y =', x+y\n",
"print\n",
"\n",
"print 'Subtracting them:'\n",
"print 'x-x =', x-x\n",
"print 'y-y =', y-y\n",
"print 'x-y =', x-y"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Our two arrays:\n",
"x = [1 2 3 4 5]\n",
"y = [ 1.1 2.2 3.3 4.4 5.5]\n",
"\n",
"Multiplying them together:\n",
"x*x = [ 1 4 9 16 25]\n",
"y*y = [ 1.21 4.84 10.89 19.36 30.25]\n",
"x*y = [ 1.1 4.4 9.9 17.6 27.5]\n",
"\n",
"Dividing them:\n",
"x/x = [ 1. 1. 1. 1. 1.]\n",
"y/y = [ 1. 1. 1. 1. 1.]\n",
"x/y = [ 0.90909091 0.90909091 0.90909091 0.90909091 0.90909091]\n",
"\n",
"Adding them together:\n",
"x+x = [ 2 4 6 8 10]\n",
"y+y = [ 2.2 4.4 6.6 8.8 11. ]\n",
"x+y = [ 2.1 4.2 6.3 8.4 10.5]\n",
"\n",
"Subtracting them:\n",
"x-x = [0 0 0 0 0]\n",
"y-y = [ 0. 0. 0. 0. 0.]\n",
"x-y = [-0.1 -0.2 -0.3 -0.4 -0.5]\n"
]
}
],
"prompt_number": 26
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**You may notice a pattern and an anomaly here.**\n",
"\n",
"Pattern:\n",
"* int and int = int\n",
"* float and float = float\n",
"* float and int = float\n",
"\n",
"Anomaly!\n",
"* int / int = float\n",
" * Just think that $1/2=0.5$"
]
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment