{ "cells": [ { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5.0 6\n" ] } ], "source": [ "# Numbers in Python\n", "\n", "x = 5 # integer (discrete)\n", "y = 5.813 # floating point number / real number (continuous-ish)\n", "\n", "x = float(x)\n", "y = round(y) # round to nearest integer\n", "\n", "print(x, y)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Other discrete variables\n", "\n", "x = 'string'\n", "x = True" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 3, 5, 7, 7, 7, 9] 7\n", "{9, 3, 5, 1, 7} 5\n" ] } ], "source": [ "# Collections (Lists and sets)\n", "\n", "odds1 = [1, 3, 5, 7, 7, 7, 9] # list/array/vector\n", "odds2 = {1, 3, 5, 7, 7, 7, 9} # set\n", "\n", "#odds1 = set(odds1)\n", "#odds2 = list(odds2)\n", "\n", "print(odds1, len(odds1))\n", "print(odds2, len(odds2))" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 3\n", "9\n", "[1, 3, 5]\n" ] } ], "source": [ "# Accessing elements of lists\n", "\n", "odds = [1, 3, 5, 7, 9]\n", "\n", "first = odds[0] # first element starts at 0\n", "second = odds[1] # first element starts at 1\n", "print(first, second)\n", "\n", "last = odds[-1]\n", "print(last)\n", "\n", "# Slicing\n", "\n", "section = odds[0:3]\n", "print(section)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "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.1" } }, "nbformat": 4, "nbformat_minor": 2 }