{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "#GLACINDIA Workshop\n", "##Part 5: Python Basics" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Nikolay Koldunov\n", "\n", "\n", "koldunovn@gmail.com" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Variables" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python uses [duck typing](http://en.wikipedia.org/wiki/Duck_typing)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Variable - is a facility for storing data. The current value of the variable is the data actially stored in the variable." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Int" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "a = 10" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "a" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "type(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Float" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "z = 10.\n", "z" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "aa = 3" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "bb = 2." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "aa/bb" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "type(z)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "calculate $a+b$, where $a=2$, $b = 3$" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Legal names for variables" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Legal names consist of any combination of letters and digits, starting with a letter. \n", "\n", "These are allowable:\n", "\n", " LatLon, Lat2Lon, l5, L2, z25c5\n", "\n", "These are not allowable:\n", "\n", " Lat-Lon, 2Lat, %x, @sign\n", "\n", "Use names that reflect the values they represent." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### String" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "b = '2'\n", "b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Some operations are not allowed on different types:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "a+b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "But some of them are allowed:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "b*a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Might be a source of confusion :)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "String variables can be combined:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "c = ' guys walk into a bar'\n", "c" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "d = \"Two\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "d+c" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In order to include variable of another type in to string you have to convert it:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "str(a)+c" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Everything is an object" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In IPython you can get the list of object's methods and attributes by typing dot and pressing TAB:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "c." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Methods are basically default functions that can be applied to our variable:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "c.upper()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "c.title()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "c.count('a')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "c" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "c.find('into')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you need help on method in IPython type something like:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "c.find?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Or open bracket and press SHIFT + TAB:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "c.find(" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* Create variables with your name and word Hi, and print out \"Hi, Name\"\n", "* Make your name apear in all capital letters\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "c.rsplit()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Modules" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Pure python does not do much. To do some specific tasks you need to import modules. Here I am going to demonstrate several ways to do so." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The most common one is to import complete library. In this example we import *math* - a library that contain mathematical functions:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "pi" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import math" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here we get value of pi. Note how function *pi* is called. We have to use name of the library, then dot, then name of the function from the library:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "math.pi" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Another option is to import it like this:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from math import *" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this case all functions will be imported in to the name-space and you can use *pi* directly, without typing the name of the library first:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "pi" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "But generally it's a bad idea, because your name-space is populated by things that you don't really need and it's hard to tell where the function comes from." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "whos" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can import only function that you need:" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from math import pi" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "3.141592653589793" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pi" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Or import library as alias in order to avoid extensive typing:" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import math as mt" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "3.141592653589793" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mt.pi" ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "float" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(pi)" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Links:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[Dive Into Python](http://www.diveintopython.net/index.html)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.9" } }, "nbformat": 4, "nbformat_minor": 0 }