{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Python Introduction: Types" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's evaluate some simple expressions." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3*2" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "11" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "5+3*2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can use `type()` to find the *type* of an expression." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(5+3*2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now add decimal points." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "12.0" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "5+3.5*2" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "float" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(5+3.0*2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Strings are written with single (``'``) or double quotes (`\"`)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'hello'" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"hello\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Multiplication and addition work on strings, too." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'hello hello hello sc15'" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3 * 'hello ' + \"sc15\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Lists are written in brackets (`[]`) with commas (`,`)." ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[5, 3, 7]" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[5, 3, 7]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "List entries don't have to have the same type." ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "['hi there', 15, [1, 2, 3]]" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[\"hi there\", 15, [1,2,3]]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\"Multiplication\" and \"addition\" work on lists, too." ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 5, 5, 5]" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[1,2,3] * 4 + [5, 5, 5]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Hmmmmmm. Was that what you expected?" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 9, 13, 17])" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import numpy as np\n", "\n", "np.array([1,2,3]) * 4 + np.array([5,5,5])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [] } ], "metadata": {}, "nbformat": 4, "nbformat_minor": 0 }