Composing Programs

louist87@gmail.com

Sep. 2015

Review: Core Concepts

What problems do operating systems solve?

What is a system shell?

What is an interpreter?

What is the python shell command?

Review: Our Tools

1. What is atom ?

2. What is ipython and why should it be preferred over python?

Programming in Python

Programming languages are written for humans!

x86 assembly wants you to think in terms of CPU registers

section .data
str:     db 'Hello world!', 0Ah
str_len: equ $ - str

section .text
global _start

_start:
mov eax, 4
mov ebx, 1

mov ecx, str
mov edx, str_len
int 80h

mov eax, 1
mov ebx, 0
int 80h

Brainfuck wants you to think in terms of memory addresses and pointers:

++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.

Python wants you to think in terms of physical objects:

print "Hello world!"

Object-Oriented Programming

  1. OOP is a programming paradigm: a way to think about your problem and your code.
  2. OOP uses the metaphor of objects to describe elements of code and their behaviors.

In Python, everything is an object!

So, what is an object?

Two levels of analysis:

  1. Metaphorical level: what does the term object tell me about how I should think about my problem?
  2. Practical level: what can I do with this thing called object, and where can I get one?

Metaphorical level

All data are not equal. Python is a strongly typed language:

Adding integers:

1 + 1

Dividing floats:

38.7 / 3.2

Adding strings:

"Hello " + "world!"

Adding an integer to a string:

"I am " + 28 + " years old"

Conclusions:

  1. An object’s type tells you something about how you’re expected to use it.
  2. Know what you’re manipulating at all times!

Practical level

Common Object Types

Type Name Python object Use case Caveats
Boolean bool Representing truth
Integer int Representing whole numbers Beware of integer division! Try 10 / 2
Float float Representing decimal fractions Beware of floating-point errors! Try .1 * 3
String str Representing text Beware of accents and diacriticals!
List list Maintaining an ordered sequence of objects that may change. Useful for homogenous types
Tuple tuple Maintainging an immutable ordered sequence Useful for heterogeneous types.
Dictionary dict Associating one object (the key) to another (the value) Dicts are unordered
Set set Keeping track of values that were encountered. Testing for the presence of multiple values. Sets are unordered and contain at most one of each item.

Control Structures

Computers build upon automata by allowing for data-dependent computation. How do we change the behavior of a program based on the value of a variable?

x = 10
if x > 10:
    print("Greater than 10")
elif x:
    print("Less than 10, but nonzero")
else:
    print("exactly 0")

if and elif evaluate the expression that follows them. If the statement evaluates to True, the block of code is executed.

All objects have a Boolean truth value:

bool(0)
bool(1)
bool("")
bool("false")
bool([])
bool([1, 2, 3])

Zero and null values evaluate to False. Everything else evaluates to True.

Comparison operators:

Symbol Meaning Example
< Less than 1 < 4
> Greater than 2.3 > 0
== Equal to 1 == True
!= Not equal to True != False

WARNING:

This will throw an error:

x = "hello"
if x = "hello":
    print("success!")

Exercises

  1. Write a script to test if two strings are equal. If they are equal, print the string, otherwise print a message indicating that they are not equal. Bonus: edit your script to print the string in all capital letters. (Hint: look at the methods for str objects!)

  2. Write a script to test if a dictionary contains the key “x”. The expression "x" in d (where d is a dictionary) will evaluate to True if "x" is a key in the dictionary. If the key is present, print its associated value. If it is absent, do the same for the key "y". If niether key is present, print a message explaining the dictionary is empty.

Functions

Functions are the basic element of abstraction in most programming languages. They are used to:

Functions take in objects, and return objects. Using a function is referred to as calling. Function calls are denoted by parentheses, e.g.:

sum([1, 3, 6, 2])

Functions are defined via the def keyword:

def sum(terms):
    """Add all elements of a list together and return the result"""
    s = 0
    for i in terms:
        s += i

    return i

Exercises

  1. Create a function that takes three arguments a, b, and c that correspond to the three sides of a right triangle with c being the hypotenuse. The function should calculate the length of the side for which None was passed using the Pythagorean theorem. For example: calculate_missing_side(3, 8, None) should return 8.544...

  2. Create a function called map2 that takes a function as it’s first argument and a list of integers as it’s second argument. map2 should call the function passed into it once for each argument in the list and return a list containing all the results. Next, write a function called double that doubles a number. Call map2 as follows: map2(double, range(10)).