Date Tags Python

I am still working on my Python reference but updates are going to be temporarily less frequent because classes for my Computer Science degree at Oregon State have started back up!

This reference is really just for myself as a way to index and document everything I have learned and am learning about Python so I can have it all in one easy to acccess place.

Eventually, I may want to consider throwing this all into a wiki or maybe even writing my own simple wiki software.

Contents

Learning Python

Best Books on Python

Basics

Strings

Functions

OOP in Python

Working with the file system

Other

Learning Python

books

Best Books on Python

Coming soon...

Basics

Coming soon...

Basics

Everything is an object

In Python everything is an object which means you can get info on its type, methods, etc.

The Python dynamic typing model

In languages like C and C++ variable types are idenitifed by the programmer when he/she declares a variable to be an int, char, float, etc.

In Python variables are also called names. Variables in Python never have any type information or other constraints associated with them. The concept of types is only associated with objects. Variables (names) are generic and only refer to a particular object at a particular time.

When a variable (name) appears in an expression it is immeadiately replaced with the object it is referencing.

names and objects

Getting the type of an object

myVar = 100
print type(myVar)
# output: <type 'int'>

Getting the attributes and methods of an object

Python objects often have attributes that are other objects stored inside them. Python objects also have methods which are functions associated with the object that have access to the object's internal data.

To list these objects and methods you can use the dir() function.

x = "Hello World"
print dir(x)
# output:
# ['__abs__', '__add__', '__and__', '__class__', '__cmp__', '__coerce__', '__delattr__', '__div__', '__divmod__', '__doc__', '__float__', '__floordiv__', '__format__', '__getattribute__', '__getnewargs__', '__hash__', '__hex__', '__index__', '__init__', '__int__', '__invert__', '__long__', '__lshift__', '__mod__', '__mul__', '__neg__', '__new__', '__nonzero__', '__oct__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'bit_length', 'conjugate', 'denominator', 'imag', 'numerator', 'real']

Comments

Its important to know how to comment in any lanuage:

# Single line comment

"""
multi
line
comment
"""

Printing

# Under Python 2 you may still see
print "Hello World!"

# Under Python 3
print("Hello World!")

# Why the difference?  Print is a function in Python 3, and not a statement like it is in Python 2.

Basic arithmetic operations in Python

Notice under Python 2 the division operator performs integer division by default. While in Python 3 the division operator performs floating point division by default.

print(2 + 2)
# output: 4

print(2 - 2)
# output: 0

print(2 * 2)
# output: 4

print(5 / 2)
# output under Python 2: 2
# output under Python 3: 2.5

# explicit floor division
print(5 // 2)
# output: 2

print(4 ** 2)
# output: 16

# Modulo (the remainder of division)
print(10 % 3)
# output: 1

string formatting

There are two ways to format strings in Python. An old C like formatting expression using % and a new prferred, formatting method using .format().

numOfDogs = 2
dogStatus = "sleeping"

print("I have %d dogs. They are currently %s." % (numOfDogs, dogStatus) )
# output: I have 2 dogs. They are currently sleeping.

print("I have {} dogs. They are currently {}.".format( numOfDogs, dogStatus ))
# output: I have 2 dogs. They are currently sleeping.

The Python docuumentation on string formatting is actually not that great so here is a site that provides a lot more info: https://pyformat.info/

Working with dates

Print todays date:

import datetime
print datetime.date.today()

Print current date and time:

import datetime
print datetime.datetime.now()

Reimporting a module in the interactive interpreter

import importlib
importlib.reload(myModule)

from: http://stackoverflow.com/questions/684171/how-to-re-import-an-updated-package-while-in-python-interpreter

Note that using reload may cause unexpected namespace issues and the cleanest option is always to simply close and restart the interpreter whenever you make changes in a module.

Tab Completion In Windows for the Python Interpreter

The python interpreter doesn't come with tab completion out of the box, on Windows. To get it, you will need to install pyreadline https://pypi.python.org/pypi/pyreadline

strings

Strings

What is a string?

A string is simply an array of characters. It blows the mind of some people, but simpler languages like C don't have built in datatypes for strings. Instead you have to represent strings with an array of characters. This complicates things a bit in C because you have to keep in mind how complex a creature a string can be when doing things that seem simple, like copying a string, checking if two strings are equal, getting the length of a string, etc.

To work with strings it might be helpful to also understand that under the hood, a string (array of characters) is actually an array of integers. What I mean by this is that computers do not store the actual character for a string but instead store an integer value that humans have defined to represent a certain character in an alphabet. Those integer values are later decoded by the computer and replaced with the appropriate character. The mappings for integer to chracter is based upon the encoding scheme used.

If you want more info, check out this Wikipedia article: https://en.wikipedia.org/wiki/Character_encoding

Luckily for us, Python has a built in string datatype and so Python is able to manage a lot of this complexity for us in dealing with strings.

Creating and storing a string in a variable

myString = "This string is enclosed in double quotes."

myString2 = "This string is enclosed in single quotes, which may feel a bit unatural for C/C++ programmers! :)"

myMultiLineStr = """This string spans
multiple lines. And that's ok."""

myOtherMultiLineStr = '''This string also spans multiple lines but notice it is enclosed
in three single quotes instead of three double quotes.'''

myMultiLineStr3 = """Notice that you can also preserve white space...


...with multiline strings."""

Getting the length of a string

myString = "Hello Python World!"
print( len(myString) )

Concatenating strings

string1 = "Hello " 
string2 = "World!"

>>> print( string1 + string2 )
Hello World!

Using commas to concatenate strings

>>> print("I have a", "dog named", "charlie.")
I have a dog named charlie.

Accessing individual characters in a string

>>> myString = "Hello World!"
>>> myString[4]
'o'

Slicing strings

>>> best_scifi_show = "Battlestar Galactica"
>>> best_scifi_show[0:10]
'Battlestar'

Python strings are immutable

>>> best_scifi_show[0] = "a"
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment

How then do I modify a single character in a string in Python?

http://stackoverflow.com/questions/1228299/change-one-character-in-a-string-in-python

Functions

Functions

basic function with positional arguments

def add(x, y):
    return x + y

>>> add(10, 20)
30

returning multiple values from a function

def add2Return3(addendX, addendY):
    aSum = addendX + addendY
    return aSum, addendX, addendY

>>> aSum, addendX, addendY = add2Return3(10, 20)
>>> print(aSum, addendX, addendY)
30 10 20

basic function with positional arguments

def add(x, y):
    return x + y

>>> add(10, 20)
30

function with optional default arguments

def greet(greeting='Hello World!'):
    return greeting
>>> func1.greet()
'Hello World!'
>>> func1.greet("Hello, Dave")
'Hello, Dave'

Positional arguments vs key word arguments

You can pass arguments via position or keyword in Python

Good Stack Overflow question about keyword vs positional arguments in Python http://stackoverflow.com/questions/1419046/python-normal-arguments-vs-keyword-arguments

function with a variable number of parameters

The arguments you pass into the function are input into a tuple (immutable list essentially) named args.

def dynAdd(*args):
    sum = 0
    for addend in args:
        sum += addend
    return sum

>>> print(dynAdd(100, 200, 300, 400, 500))
1500

functions with a variable number of keyword arguments

python stores all of the keyword arguments into a dictionary

def get_dog_info(**kwargs):
    dogColor = kwargs.pop("dogColor")
    print(dogColor)

>>>get_dog_info(dogColor='brown')
"brown"

Object Oriented Programming in Python

Object Oriented Programming in Python

Creating a simple class

To illustrate classes in Python let's start with a simple example of a Box class, adapted from a C++ assignment.

Here is the code:

class Box(object):

    # This is the contructor of the class
    # This code is run every time an instance of the class is created
    def __init__(self, height, width, length):
        self.height = height
        self.width = width
        self.length = length

    def setHeight(self, height):
        self.height = height

    def setWidth(self, width):
        self.width = width

    def setLength(self, length):
        self.length = length

    # returns the volume of the box
    def getVolume(self):
        return self.height * self.length * self.width

    # returns the surface area of the box
    def getSurfaceArea(self):
        return (2 * self.length * self.width) + (2 * self.length * self.height) + (2 * self.width * self.height)


def main():

    newBox = Box(10.0, 10.0, 5.0)
    print(newBox.getVolume())
    print(newBox.getSurfaceArea())

# will only run the program if it is executed directly
if __name__ == '__main__':
    main()

Private and Public Instance Variables?

As a note for those coming from C++ and other similar languages, Python does not have Private instance variables. Instead, it is the responibility of the programmer to make sure they don't do something irresponsible.

More info: http://stackoverflow.com/questions/1641219/does-python-have-private-variables-in-classes

Self

Coming soon...

Working with the filesystem

Working with the filesystem

Extracting extension from filename in Python

Listing all filenames (and folders) in a folder

filenames = os.listdir(dir_path)

Other

Other

Preparing for Python 3 code while using Python 2

from __future__ import print_function

explanation coming soon...

Docstrings

Python and the Python Commununity are very big on documentation and so built into the concept of Python modules is something called a docstring that allows you to provide some basic documentation about your module when you insert a string literal into the first line of a module, class, or function.

That is probably a bit confusing so let's take a look at a quick example in the interactive interpreter.

>>> import csv
>>> print(csv.__doc__)
CSV parsing and writing.

This module provides classes that assist in the reading and writing
of Comma Separated Value (CSV) files, and implements the interface
described by PEP 305.  Although many CSV files are simple to parse,
the format is not formally defined by a stable specification and
is subtle enough that parsing lines of a CSV file with something
like line.split(",") is bound to fail.  The module supports three
basic APIs: reading, writing, and registration of dialects.


DIALECT REGISTRATION:

Readers and writers support a dialect argument, which is a convenient
handle on a group of settings.  When the dialect argument is a string,
it identifies one of the dialects previously registered with the module.
If it is a class or instance, the attributes of the argument are used as
the settings for the reader or writer:

    class excel:
        delimiter = ','
        quotechar = '"'
        escapechar = None
        doublequote = True
        skipinitialspace = False
        lineterminator = '\r\n'
        quoting = QUOTE_MINIMAL

SETTINGS:

    * quotechar - specifies a one-character string to use as the
        quoting character.  It defaults to '"'.
    * delimiter - specifies a one-character string to use as the
        field separator.  It defaults to ','.
    * skipinitialspace - specifies how to interpret whitespace which
        immediately follows a delimiter.  It defaults to False, which
        means that whitespace immediately following a delimiter is part
        of the following field.
    * lineterminator -  specifies the character sequence which should
        terminate rows.
    * quoting - controls when quotes should be generated by the writer.
        It can take on any of the following module constants:

        csv.QUOTE_MINIMAL means only when required, for example, when a
            field contains either the quotechar or the delimiter
        csv.QUOTE_ALL means that quotes are always placed around fields.
        csv.QUOTE_NONNUMERIC means that quotes are always placed around
            fields which do not parse as integers or floating point
            numbers.
        csv.QUOTE_NONE means that quotes are never placed around fields.
    * escapechar - specifies a one-character string used to escape
        the delimiter when quoting is set to QUOTE_NONE.
    * doublequote - controls the handling of quotes inside fields.  When
        True, two consecutive quotes are interpreted as one during read,
        and when writing, each quote character embedded in the data is
        written as two quotes

As you can see, stored in the modules __doc__ variable is a string literal that provides information about that module.

Note that this is also the same mechanism that the interactive interpreter's help function uses. So, we could get the same string literal (docstring) by using the following:

>>> import csv
>>> help(csv)
Adding your own Docstrings

To illustrate how docstrings are created, I am going to take the Box class from earlier and will add docstrings to its module, class definition, and each of its class methods.

# Box.py

"""Box.py
Provides a simple class for modeling a box and 
calculating the box's volume and surface area.
"""

class Box(object):

    """Models a box and provides its volume and surface area."""

    def __init__(self, height, width, length):
        """Create a box with the given height, width, and length."""
        self.height = height
        self.width = width
        self.length = length

    def setHeight(self, height):
        """Sets the height of the box."""
        self.height = height

    def setWidth(self, width):
        """Sets the width of the box."""
        self.width = width

    def setLength(self, length):
        """Sets the length of the box."""
        self.length = length

    def getVolume(self):
        """"Returns the voume of the box."""
        return self.height * self.length * self.width

    def getSurfaceArea(self):
        """"Returns the surface area of the box."""
        return (2 * self.length * self.width) + 
        (2 * self.length * self.height) + 
        (2 * self.width * self.height)


def main():

    newBox = Box(10.0, 10.0, 5.0)
    print(newBox.getVolume())
    print(newBox.getSurfaceArea())

# will only run the program if it is executed directly
if __name__ == '__main__':
    main()

That is probably a bit of documentation overkill, but you get the idea. To add docstrings, to a module, class, or method simply add a triple quoted string literal as the first line after its definition.

We can view these docstrings interactively by importing the module into the interactive interpreter.

>>> import box
>>> print(box.__doc__)
Box.py
Provides a simple class for modeling a box and
calculating the box's volume and surface area.

>>> print(box.Box.__doc__)
Models a box and provides its volume and surface area.

>>> print(box.Box.getVolume.__doc__)
"Returns the voume of the box.

For more info on Docstrings: https://www.python.org/dev/peps/pep-0257/

Conditional Program Execution

Some Python modules are designed to be only imported, like libraries and some are designed simply to be executed like your normal scripts and simple programs.

There is a way though to make a type of hybrid file that can be both only imported but then also executed directly under certain conditions.

The key to doing this is understanding that when Python imports a module its sets a special variable called __name__ to be the name of the imported module.

Lets view this with the interactive interpreter:

Start the interpreter and import the csv module. Then view the value of the __name__ variable for the csv module.

>python
Python 3.5.1rc1 (v3.5.1rc1:948ef16a6951, Nov 22 2015, 23:41:41) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import csv
>>> csv.__name__
'csv'
>>>

As you can see python sets the csv.__name__ variable to 'csv'.

Let's see what the __name__ variable itself is in the interpreter:

>>> __name__
'__main__'

So, when you start the Python interpreter directly or when you execute a script directly by typing something like python myScriptsName.py, Python sets the __name__ variable to be __main__.

Knowing this condition exists allows us to use the following conditional statement at the bottom of our code. This will cause our main() function to be executed when it is run directly but to NOT be executed when it is imported.

if __name__ == '__main__':
    main()

Under extreme construction

(so extreme, I had to break out the 90s gifs)

Under construction gif

More coming soon!!!



For loops

for x in range(100):
    print x

Comments

comments powered by Disqus