Table of Contents

Introduction to Python

Python

Python Uses

Python Online Resources

Python Execution (Conventional)

Python Execution (Jupyter Notebook)

Python Intro

What Python Looks Like

>>> import calendar
>>> cal = calendar
>>> cal.prmonth(2017,2)
  
   February 2017
Mo Tu We Th Fr Sa Su
       1  2  3  4  5
 6  7  8  9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28
  
>>> cal.weekday(2017,2,17)
4
  
>>> cal.weekday(1973,11,14)
2
    
>>> cal.prmonth(1973,11)
   November 1973
Mo Tu We Th Fr Sa Su
          1  2  3  4
 5  6  7  8  9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30

>>> # Some object introspection
>>> # Type cal. and hit tab a couple of times
>>> cal.
>>> # Also, try
>>> dir(cal)

Python: Everything is an Object

>>> x = 'hello, world'
>>> y = x.upper()
>>> y
'HELLO, WORLD!'
  
>>> def swapper(mystr):
.  .  .      return mystr.swapcase() # indent mandatory
.  .  .
>>> swapper(x)
'HELLO, WORLD!'
  
>>> x
'hello, world!'
  
>>> def parts(mystr,sep=','):
.  .  .     return mystr.partition(sep)
.  .  .

>>> parts(x)
('hello', ',', ' world!')

Python: Everything is an Object (even functions)

>>> def personalize(greeting, name='Joni'):
.  .  .     # Replaces 'world' with a given name
.  .  .     return greeting.replace('world', name)
.  .  .

>>> x
'hello, world!'
  
>>> personalize(x, 'Joanne')
'hello, Joanne!'

>>> personalize(x) # Use the default name='Joni' parameter
'hello, Joni!'


>>> # Python functions are "first class" (http://tiny.cc/ggh4vz)
  
>>> funclist = [swapper, personalize, parts]
>>> for func in funclist:
.  .  .     func(x)
.  .  .
'HELLO, WORLD!'
'hello, Joni!'
('hello', ',', ' world!')

Python Syntax Highlights - blocks & indentation

void foo(int x) {
        if (x == 0) {
		bar();
		baz();
	} else {
		quo(x);
		foo(x - 1);
	}
}
def foo(x):
	if x == 0:
	        bar()
		baz()
	else:
		quo(x)
		foo(x - 1)
x = 1                       # block 0
if x == 1:                  # header line:
    y = 2                   # block 1
    if y == 2:              # header line:
	print('in block2')  # block 2
    print('in block1')      # block 1
print('in block0')          # block 0
# open list bracket [] pairs may span lines
L = ["Good",
     "Bad",
     "Ugly"]

# Backslashes allow line continuation
if a == b and c == d and \
   d == e and f == g:
   print('old')

# Parentheses allow line continuation, usually
if (a == b and c == d and
    d == e and e == f):
    print('new')

Python Syntax Highlights - standard input/output

while ($myline = <STDIN>) {
	print $myline;
}
import sys
for line in sys.stdin:
	sys.stdout.write( line )

Python Syntax - flow control

# Assume these assignments:
x = 10
y = 10
b = 1


# if then else
if (b == 1):
   y = 1
elif (x == 10):
   y = 2
else:
   y = 3


# while (else) loop
while (x != 0):
   x = x - 1
   if (b == 1): continue # continue with next loop repetition
   break                 # break out of loop; skip else:
else:                    # run if we didn't exit loop with break
   x = x + 1


# for (else) loop
for x in range(4):      # repeats 4 times x=0..3
   y = y + 1
   if (b == 1): continue
   break                # break out of loop; skip else:
else:                   # run if we didn't exit loop with break
   y = y + 1

Python Syntax - built-in objects

Python Syntax - strings

print("one is " + str(1))

Python Syntax - string formatting

'formatting codes corresponding to list of objects, and other characters' % ( comma-separated list of objects )
print("Number of %i character words: %i" % ( x, char_count[ x ] ))      # %i denotes integer

Python Syntax - lists

for x in [1, 2, 3]:   # list [1, 2, 3] is anonymous
   print(x)
for x in range( 0, 10 ): # range( 0, 10 ) or just range( 10 ) yields the range 0..9
   print(x)
List1 = [0, 1, 2, 3]
List2 = range( 1, 5 )     # Not a list, but a range object
List2 = list(range(1, 5)) # Convert range to list containing [1, 2, 3, 4]
List3 = []   # an empty list

Python Syntax - list functions

len( List3 )
list1 = list(range( 1, 5 ))
list2 = list(range( 6, 10 ))
list3 = list1 + list2
print(list3)   # output: [1, 2, 3, 4, 6, 7, 8, 9]
list1.append( 4 )
list1.extend( [5, 6, 7] )
list1.reverse()
print(list1)   # output: [4, 3, 2, 1]
list1.sort()
print(list1)   # output: [1, 2, 3, 4]
del list3[ len(list3) - 1 ]   # delete last element at last index of list3

x = list3.pop()   # delete last element of list3 and assign val to x

Python Syntax - list iteration

# Method 1:
for x in list3:
   print(x)   # print values in list3, one per line

# Method 2:
for x in range( len( list3 ) ):
   print(list[ x ])   # print index-accessed values in list3, one per line

Python Syntax - dictionaries

D2 = { 'spam': 2, 'eggs': 3 }   # 2 string keys and 2 int values
D3 = { 1: 10, 2: 14 }           # 2 int keys and 2 int values
D2[ 'eggs' ] += 1   # increment number of eggs by 1
print(D2['eggs'])

D3[ 1 ] += 1        # increment value at key=1 by 1
print(D3[ 1 ])

D3[ 5 ] = 4         # new key:value pair added to D3

Python Syntax - dictionary functions

print(D2.keys())    # outputs an keys object containing the list: ['eggs', 'spam']
print(D2.values())  # outputs a values object containing a list: [2, 4]
print(D2.items())
print(len(D2))
print(D2.get('bacon'))       # outputs 'None' since nonexistent key
print(D2.get('bacon', -1))   # outputs -1 since nonexistent key
D3.has_key( 3 )   # (Python 2 only) returns False since the key 3 does not exist in D3
3 in D3           # Check if key 3 is in dictionary D3
D3 = { 'toast':4, 'muffin':5, 'spam':1000 }
D2.update( D3 )   # 'spam' value from D3 overwrites 'spam' value in D2

Python Syntax - dictionary iteration

table = {'Python': 'Guido van Rossum',
	'Perl':   'Larry Wall',
	'Tcl':    'John Ousterhout' }

for lang in table.keys():
   print (lang, '\t', table[ lang ])

Python Syntax - files (not stdin)

x = open("input.txt", "r")             # open file for input and assign to object x

while 1:
   y = x.readline()                    # read next line of x
   if (not y):
	  break

for eachline in x.readlines():         # x.readlines() creates [list] of lines in x
   y = eachline

y = x.read()                           # read entire file into a string
y = x.readline()                       # read next line
y = x.readlines()                      # read file into list of strings

x.close()
file = open("output.txt", "w") 

file.write("Hello World") 
file.write("This is our new text file.")

file.close() 

Python Syntax - command line arguments

import sys

x = sys.argv   # List of command line arguments
print(x)

print(x[ 1 ])


output:
$ python cmdargs.py 1 2 3
['cmdargs.py', '1', '2', '3']
1

Python Syntax - functions

def factorial(n):                      # define a function
   if (n == 1):
	  return (1)
   else:
	  return (n * factorial(n-1))      # recursion

x = factorial(5)                       # call a function
def accessglobal():
   global glib                         # access a global scope var
   glib = 100

glib = 0
accessglobal()
print("glib is %i after call to accessglobal()" % glib)


Python & regular expressions

# python_re.py

import re

str = 'I am a string'

# store RE in a RE pattern object:
regex = re.compile(r'string$')

#
# matching:
#
# finds first instance of regex using module's (re) search function:
if re.search(r'string$', str):
	print("str ends with 'string'")
	
# finds first instance of regex using RE pattern object:
if regex.search(str):
	print("str ends with 'string'")

# finds all instances of regex (returns list of matching substrings)
if regex.findall(str): # findall being used as an if condition
	print("str ends with 'string'")

print(re.findall(r'[AEIOUaeiou]', str)) # findall being used normally


#
# search/replace:
#
# replaces all instances of ' a ' with ' another ' in str;
# so the default is global search and replace:
str = re.sub(r' a ', ' another ', str)
print("str is now: " + str)

# Added "1" to replace only the first instance of ' a ' with ' another ' in str:
str = re.sub(r' a ', ' another ', str, 1)


#
# split:
#
pattern = re.compile(r'\W+')  # \W matches any non-alphanumeric character;

print(pattern.split('This is a test, short and sweet, of split().'))
# output: ['This', 'is', 'a', 'test', 'short', 'and', 'sweet', 'of', 'split', '']

print(pattern.split('This is a test, short and sweet, of split().', 3))
# At most 3 splits are performed, and the rest of the string is left unsplit.
# output: ['This', 'is', 'a', 'test, short and sweet, of split().']

# Can use re module split method without using a pattern object:
print(re.split(r'\W+', 'This is a test, short and sweet, of split().'))


#
# regexp groups (back references in Perl):
#
mystring = "abcdefg"
mygroups = re.search( '(a.)(c.)(e.)(g)', mystring )
# mygroups is a "match object" (See http://docs.python.org/library/re.html#match-objects)

print(mygroups.group( 0 ))   # group( 0 ) is the whole of mystring, i.e., abcdefg
print(mygroups.group( 1 ))   # group( 1 ) is ab
print(mygroups.group( 2 ))
print(mygroups.group( 3 ))
print(mygroups.group( 4 ))   # group( 4 ) is g

# The match object .group() method returns regular strings:
mynewstring = mygroups.group(4) + mygroups.group(3) + mygroups.group(2)

print(mynewstring)

Python modules and sys.path

import sys
print(sys.path)

sys.path and user-created modules

import sys

# Add my own module path to sys.path;
# possible because sys.path is a standard Python list
sys.path.append( '/export/home/hawkdom2/jchung/lib/python/test' )

print(sys.path)
# Import /export/home/hawkdom2/jchung/lib/python/test/mymod.py
# where the mymod.py contains a simple function def:
#
#    def greeting():
#       print "This is being printed in jchung's mymod module."
#
import mymod

# Calls the greeting() method in the imported mymod module
mymod.greeting()

Importing modules in various ways

import module1                   # Get module as a whole
module1.printer('Hello world!')  # Access module member names using module name.member_name
from module1 import printer      # Import member name from module1
printer('Hello world!')          # Access module member names directly.
from module1 import *          # Import 'printer()' and any other names from module1
printer('Hello world!')

Python packages


Python List Comprehensions

S = [2*x for x in range(101) if x**2 > 3]
print S

Output: [4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50,
		 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100,
		 102, 104, 106, 108, 110, 112, 114, 116, 118, 120, 122, 124, 126, 128, 130, 132, 134, 136, 138, 140,
		 142, 144, 146, 148, 150, 152, 154, 156, 158, 160, 162, 164, 166, 168, 170, 172, 174, 176, 178, 180,
		 182, 184, 186, 188, 190, 192, 194, 196, 198, 200]




freshfruit = ['  banana', '  loganberry ', 'passion fruit  ']
print [weapon.strip() for weapon in freshfruit]

Output: ['banana', 'loganberry', 'passion fruit']




# multiple lists in a list comprehension:
vec1 = [2, 4, 6]
vec2 = [4, 3, -9]
print [x*y for x in vec1 for y in vec2]

Output: [8, 6, -18, 16, 12, -36, 24, 18, -54]




# If the expression would evaluate to a tuple, it must be parenthesized:
vec = [2, 4, 6]
print [(x, x**2) for x in vec]

Output: [(2, 4), (4, 16), (6, 36)]




# The dict() constructor builds dictionaries directly from lists of 
# key-value pairs stored as tuples.
# When the pairs form a pattern, list comprehensions can compactly
# specify the key-value list.
print dict([(x, x**2) for x in (2, 4, 6)]) # dict applied to a list comprehension

Output: {2: 4, 4: 16, 6: 36}