Python Review

Introduction

There will be a mix of programming in this class. A lot of it will be in the form of building queries that databases operate on and that will be a new skill for most of you. But other portions of the class will require writing programs to do some more complicated tasks or calculations. That is where Python will come in. To that end you want to be sure you are ready to go so that you don’t have to refresh your Python skills at the same time that you are trying to learn a complicated new visualization tools.

This section is not meant to teach you any of the concepts mentioned, but rather just remind you of their general use and purpose. If they seem confusing or don’t quite work the way you remember it is a good indicator it is a topic that probably needs review.

The Very Basics

Variables

some_number = 42
print(some_number)
# Prints 10
some_number = "Hello World"
print(some_number)
# Prints Hello World

Variables hold values, we use them all over the place, they can hold different kinds of values at different times. But just because they can, does not mean they should.

Expressions

print(42/7)

This wil print 7. Expressions are things which are evaluated down to a single value. They can result in things like numbers, strings or boolean values. Knowing the order of operations for expressions is important as you can introduce some interesting bugs if you get it wrong.

Types

Among others there are int, float, str, list and boolean. Various types work with various functions and operators but not others. Sometimes functions or operators can convert types. For example you can print(42) which will convert 42 to a string for printing. But you cannot 42 + 'Hello World' because you can not add a number to a string. You can however '42 ' + 'Hello World' concatenate two strings to make a longer string. Knowing when type conversion will happen and when it wont is important.

Logic

42 < 100
# Evaluates to True

42 < 100 and 3 == 4
# Evaluates to False

42 < 100 or 3 == 4
# Evaluates to True

Logical expressions could be just grouped under expressions but they are extra important. They are what we use to make decisions in a program when we may or may not want to execute some particular set of code. We have a range of mathematical and logical operators to make logical expressions both simple and complex. Know the difference between = and ==.

Control Flow

These are tools that let us conditionally execute a portion of code or to execute it multiple times. They are a core concept in programming.

if

if condition:
  # Code to execute if true
else: # optional
  # Code to execute if false
# Normally indented code that will always be executed

This is the basic if conditional. If the condition is true it executes the following code. You can optionally have an else condition that is executed if the condition is false. Like all control flow statements indentation determines what is in the controlled block.

for in

for container in collection:
  # Loop through the collection

This is the basic for loop. It will execute the following code block once for each item int he collection. On each iteration container variable will hold an item in the collection. It is common to use the range function to make a collection of numbers up to a certain value in order to loop a specified number of times.

while

while condition:
  # Code to execute while the condition is true

This is a little like a for loop in that it will execute the same code over and over. But it will continue to execute until the condition becomes false. Common use cases are to execute code until a list becomes empty or until the end of a file gets reached.

Functions

Sometimes we want to encapsulate and reuse code, functions let us do that.

Calling

my_var = my_function(some_argument, some_other_argument, specific_argument=5)

Here we are calling my_function and passing it some arguments. It presumably will return a value which we then store in my_var. In some situations the arguments we pass in might be modified. In other cases they are not. Sometimes there are keyword arguments which we explicitly call out and set equal to a value in the function call, in this case specific_argument is one such argument. Sometimes these keyword arguments are optional or have default values if we don’t specify one. They always come last in the list of arguments

Defining

def my_function(arg1, arg2, specific_argument=0):
    for i in range(specific_argument):
        print(str(arg1) + str(arg2))
    return str(arg1) + str(arg2)

Here we defined a function called my_function which we can then call in our code. We specify the names of arguments so that we can reference them in the function and we return a value at the end. There is quite a bit more that can be done when defining functions but this is a basic example that will be similar to the sorts of things you need to do in this class.

Classes

Classes are used when there is a specific sort of thing that we want to have many instances of. We will define a class that sets up the methods and properties that are available to it and then we will create instances of that class in our program.

Defining a class

class Dog:
    def __init__(self, name):
        self.name = name
        self.tricks = []    # creates a new empty list for each dog
    def add_trick(self, trick):
        self.tricks.append(trick)

Here we make a Dog class. It has an instance variable named tricks that will be a list of tricks each specific dog knows. There is also a function called add_trick which will add a new trick to the list of tricks the dog knows.

__init__ it the function that is automatically called when an instance is first named. It should be, in this case, passed a name which will be assigned to the dog’s name property. self is a variable which represents a specific instance of a class. By convention it is always named self and it must appear as the first argument in the list. We can then use self within the method to access that particular instance of the class.

Instantiating a class

charlies_dog = Dog('Snoopy')
charlies_dog.add_trick('Fight Red Baron')
print(charlies_dog.tricks)

We make an instance of a class by calling the class name. So in this case we call Dog and pass it a name. That returns a new instance of the class. We can then call the methods of that class and to access the properties of that class.

Importing stuff

We will be relying heavily on 3rd party libraries to do work in this class so being comfortable with importing things is important.

from mapreduce import mapreduce_pipeline

mapreduce_pipeline.MapreducePipeline.do_stuff()

The syntax from package import submodule is that preferred syntax for importing stuff. Usually documentation will tell you you need something from a specific submodule of a package. So you import that submodule from the package. You then need to access its contents using the . notation in the form of submodule.the_thing_you_need.

If you just import the package directly you will then need to prefix it with the package name: mapreduce.mapreduce_pipeline.MapreducePipeline.do_stuff() is how we would access the MapreducePipeline class and call its do_stuff class method.