{ "metadata": { "name": "", "signature": "sha256:984000ccb2968891de679cd17582d740c27d40afa65f34e374e8360adb7a2d07" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Built-In Object Types\n", "\n", "- **Author:** [Chris Albon](http://www.chrisalbon.com/), [@ChrisAlbon](https://twitter.com/chrisalbon)\n", "- **Date:** -\n", "- **Repo:** [Python 3 code snippets for data science](https://github.com/chrisalbon/code_py)\n", "- **Note:**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Numbers" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create a variable" ] }, { "cell_type": "code", "collapsed": false, "input": [ "x = 493" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 44 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### View the variable x" ] }, { "cell_type": "code", "collapsed": false, "input": [ "x" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 45, "text": [ "493" ] } ], "prompt_number": 45 }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Strings" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create a single" ] }, { "cell_type": "code", "collapsed": false, "input": [ "warning = \"There is an earthquake in the Pacific.\"" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 46 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### View the object \"warning" ] }, { "cell_type": "code", "collapsed": false, "input": [ "warning" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 47, "text": [ "'There is an earthquake in the Pacific.'" ] } ], "prompt_number": 47 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### View the first letter of the \"warning\" string variable." ] }, { "cell_type": "code", "collapsed": false, "input": [ "warning[0]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 48, "text": [ "'T'" ] } ], "prompt_number": 48 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### View the last letter of the \"warning\" string variable" ] }, { "cell_type": "code", "collapsed": false, "input": [ "warning[-1]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 49, "text": [ "'.'" ] } ], "prompt_number": 49 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### View the number of characters in the \"warning\" string variable" ] }, { "cell_type": "code", "collapsed": false, "input": [ "len(warning)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 50, "text": [ "38" ] } ], "prompt_number": 50 }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Lists" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create a list of strings" ] }, { "cell_type": "code", "collapsed": false, "input": [ "employees = [\"Jake\", \"Steve\", \"Sarah\"]" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 51 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### View the first element the list" ] }, { "cell_type": "code", "collapsed": false, "input": [ "employees[0]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 52, "text": [ "'Jake'" ] } ], "prompt_number": 52 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Add two new employees to the list." ] }, { "cell_type": "code", "collapsed": false, "input": [ "employees = employees + [\"Bill\", \"Stan\"]" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 53 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### View the entire new list of employees." ] }, { "cell_type": "code", "collapsed": false, "input": [ "employees" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 54, "text": [ "['Jake', 'Steve', 'Sarah', 'Bill', 'Stan']" ] } ], "prompt_number": 54 }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Dictionaries" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create a dictionary of greetings" ] }, { "cell_type": "code", "collapsed": false, "input": [ "greetings = {\n", "\t'English': 'Hello',\n", "\t'Spanish': 'Hola'\n", "}" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 55 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### View the greetings dictionary" ] }, { "cell_type": "code", "collapsed": false, "input": [ "greetings" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 56, "text": [ "{'English': 'Hello', 'Spanish': 'Hola'}" ] } ], "prompt_number": 56 }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Tuples" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create a toople" ] }, { "cell_type": "code", "collapsed": false, "input": [ "dates = (12, 15, 92)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 57 }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Files" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Open a new file in output mode." ] }, { "cell_type": "code", "collapsed": false, "input": [ "f = open('data.txt')" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "FileNotFoundError", "evalue": "[Errno 2] No such file or directory: 'data.txt'", "output_type": "pyerr", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mFileNotFoundError\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[0mf\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mopen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'data.txt'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'data.txt'" ] } ], "prompt_number": 58 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Close out the file connection." ] }, { "cell_type": "code", "collapsed": false, "input": [ "f.close()" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "NameError", "evalue": "name 'f' is not defined", "output_type": "pyerr", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mNameError\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[0mf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mclose\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mNameError\u001b[0m: name 'f' is not defined" ] } ], "prompt_number": 59 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Find Out An Object's Type" ] }, { "cell_type": "code", "collapsed": false, "input": [ "type(dates)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 43, "text": [ "tuple" ] } ], "prompt_number": 43 } ], "metadata": {} } ] }