Python Tools for Management Research

0c: Python Basics II

Jason T. Kiley

Python II

Functions, methods, and control structures let us create a procedure and reuse it.

We’re moving a bit closer to actual programming: define a small procedure, make decisions, and repeat work. It’s sometimes instructive to contrast with physical terms: imagine a machine that could perfectly process an input, scale practically infinitely for relatively little cost, and have any part easily changed.

Overview

Functions

Input, procedure, return.

Methods

Functions attached to objects.

Control structures

If-then statements and loops.

Functions and methods

Much like math, functions are objects that take input and map that input to a particular output.

In programming, we tend to refer to input as arguments, and output as returns.

def f_to_c(temp_f):
    return (temp_f - 32) * 5 / 9

print(f_to_c(72))
22.22222222222222

There are many built in, and we can make our own.

Function parts

def f_to_c(temp_f):
    return (temp_f - 32) * 5 / 9

Definition

def creates the function and gives it a name.

Parameters

The parentheses define what input the function expects.

Body

The indented block is the procedure to execute.

Return

return sends a value back to where the function was called.

Methods

Remember that everything in Python is an object.

Objects have methods, which are functions attached to objects and defined with their type in mind.

c_string = "MY CAPS LOCK KEY IS BROKEN, APPARENTLY."

print(c_string.lower())
print(c_string.lower().replace("broken", "fixed"))
my caps lock key is broken, apparently.
my caps lock key is fixed, apparently.

Mutability

Mutability describes whether objects themselves can be changed.

This is different from changing the object that a name points to.

Basic types

Often immutable.

Example: text = text.lower()

Data structures

Often mutable.

Example: items.append("new")

Mutability is about objects

c_list = [1, 2, 3, 4]
d_list = c_list

c_list.append(5)

print(c_list)
print(d_list)
print(c_list is d_list)
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]
True

Both names point to the same object. If we need a separate list, we make a copy.

d_list = c_list.copy()
print(c_list is d_list)
False

Control structures

Control structures allow code to make decisions and repeat procedures.

a = 5

if a > 5:
    print("a is greater than five.")
elif a == 5:
    print("a is exactly five.")
else:
    print("a is something else.")
a is exactly five.

Indentation matters

In Python, spacing is part of the syntax.

if a > 4:
    print("This is inside the if statement.")
print("This prints either way.")
This is inside the if statement.
This prints either way.

In some other languages, code blocks use braces. Python uses indentation.

While loops

While some condition is true, do something.

The condition needs to somehow become false, or this will run forever.

b = 1
while b < 5:
    print(f"b is {b}")
    b = b + 1
b is 1
b is 2
b is 3
b is 4

For loops

For each item in some sequence, do something.

This has a natural end point, unlike the more general while loop.

for num in [1, 2, 3, 4, 5]:
    print(num + 1)
2
3
4
5
6

Convenience syntax

We often use the same control structures in the same ways.

Language designers sometimes give us syntax that makes common patterns easier.

e_list = ["THIS", "LIST", "HAS", "ITEMS", "IN", "CAPS"]

new_list = []
for item in e_list:
    new_list.append(item.lower())

print(new_list)
['this', 'list', 'has', 'items', 'in', 'caps']
[item.lower() for item in e_list]
['this', 'list', 'has', 'items', 'in', 'caps']

Hands-on

Open notebooks/0c_python2.ipynb.

Summary

  • Functions and methods let us take a procedure and give it a name.
  • Mutability is sometimes tricky, but basic types are generally immutable and data structures are generally mutable.
  • Control structures allow code to make decisions and repeat procedures.

End of the day

You now know all of the basics of programming. (Whoa!)

The core set of things that we need to do to write computations is surprisingly short. There’s a lot beyond this, but it’s about efficiency, productivity, convenience, and style.