#!/usr/bin/env python # coding: utf-8 # #GLACINDIA Workshop # ##Part 5: Python Basics # Nikolay Koldunov # # # koldunovn@gmail.com # ## Variables # Python uses [duck typing](http://en.wikipedia.org/wiki/Duck_typing) # Variable - is a facility for storing data. The current value of the variable is the data actially stored in the variable. # ### Int # In[ ]: a = 10 # In[ ]: a # In[ ]: type(a) # ### Float # In[ ]: z = 10. z # In[ ]: aa = 3 # In[ ]: bb = 2. # In[ ]: aa/bb # In[ ]: # In[ ]: type(z) # ### Exercise # calculate $a+b$, where $a=2$, $b = 3$ # In[ ]: # ### Legal names for variables # Legal names consist of any combination of letters and digits, starting with a letter. # # These are allowable: # # LatLon, Lat2Lon, l5, L2, z25c5 # # These are not allowable: # # Lat-Lon, 2Lat, %x, @sign # # Use names that reflect the values they represent. # ### String # In[ ]: b = '2' b # Some operations are not allowed on different types: # In[ ]: a+b # But some of them are allowed: # In[ ]: b*a # Might be a source of confusion :) # String variables can be combined: # In[ ]: c = ' guys walk into a bar' c # In[ ]: d = "Two" # In[ ]: d+c # In order to include variable of another type in to string you have to convert it: # In[ ]: str(a)+c # ## Everything is an object # In IPython you can get the list of object's methods and attributes by typing dot and pressing TAB: # In[ ]: c. # Methods are basically default functions that can be applied to our variable: # In[ ]: c.upper() # In[ ]: c.title() # In[ ]: c.count('a') # In[ ]: c # In[ ]: c.find('into') # If you need help on method in IPython type something like: # In[ ]: get_ipython().run_line_magic('pinfo', 'c.find') # Or open bracket and press SHIFT + TAB: # In[ ]: c.find( # ### Exercise # * Create variables with your name and word Hi, and print out "Hi, Name" # * Make your name apear in all capital letters # # In[ ]: c.rsplit() # ## Modules # 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. # The most common one is to import complete library. In this example we import *math* - a library that contain mathematical functions: # In[ ]: pi # In[ ]: import math # 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: # In[ ]: math.pi # Another option is to import it like this: # In[ ]: from math import * # 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: # In[ ]: pi # 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. # In[ ]: whos # You can import only function that you need: # In[29]: from math import pi # In[30]: pi # Or import library as alias in order to avoid extensive typing: # In[31]: import math as mt # In[32]: mt.pi # In[33]: type(pi) # In[34]: import numpy as np # ## Links: # [Dive Into Python](http://www.diveintopython.net/index.html)