Skip to content

Instantly share code, notes, and snippets.

@polynomialherder
Last active June 28, 2022 03:40
Show Gist options
  • Save polynomialherder/f66b83ffd2b4c3881a0a0744dcea9d2d to your computer and use it in GitHub Desktop.
Save polynomialherder/f66b83ffd2b4c3881a0a0744dcea9d2d to your computer and use it in GitHub Desktop.

Computer programs consist of instructions that either

  1. Compute a value
  2. Perform some action

Instructions that compute values are called expressions. Expressions always return a value. For example, 2 + 3, add(50, 100), "Nestor " + "is a cat", 1, "hello" and 3.141592653589793 are all expressions (why?).

Instructions that perform actions are called statements. The actions they perform are called "side effects". This nearly always implies some kind interaction with the world outside the program: printing stuff to a terminal screen, fetching data over the internet, or creating and deleting files. Often, but not always, statements return None (different from the string "None"). Paradoxically, None is a value that signifies the quality of not having a value!

# assignment statements associate values with names (perform an action)
x = 1

# print statements communicate messages from your program to a terminal window
print("hello there :) i am a python program starting up!")

print("maybe I will create a directory!")
import os

folder_name = "my_cool_thoughts"
os.mkdir(folder_name)

print("no thoughts today though")
os.rmdir(folder_name)

Functions

A function is an abbreviation for a computation or sequence of actions (or both).

Python consists of many functions: print, add, max, and urlfetch are all examples of function.

When we use a function, we say that we are calling it (and refer to that usage as "a function call"). The inputs to functions are called arguments or parameters.

To call a function, we write the name of the function, an open parenthesis, any arguments separated by commas, and a closed parenthesis. Some functions take no arguments at all. When a function call results in a value, we say that a function "returns" a value.

For instance, calling add with the arguments 1 and 2 returns 3:

from operator import add

x = add(2, 1)
print(x)

We use functions to avoid repetition, hide complexity, and to make code more readable and shareable.

More examples

What do you expect this program to do?

from operator import add

x = add(3, 4)
x

Now replace the last line with a print statement

What do we expect it to do this time?

from operator import add

x = add(3, 4)
print(x)

Let's do something a little wacky.

What will this program do?

from operator import add

x = add(3, 4)
y = print(x)
print(y)

Nested expressions

Because expressions always return values, they can be nested, with arbitrarily many sub-expressions:

x = max(add(3, 4), 10)
print(x)

The Python interpreter evaluates the expression max(add(3, 4), 10), starting with the innermost call of add(3, 4). The evaluation of that line goes something like this max(add(3, 4), 10) -> max(7, 10) -> 10.

Of course, nesting takes a huge toll on readability. Remember that code is written primarily for humans to read, and only incidentally for computers to execute.

Try writing out how the Python interpreter would evaluate the following statement:

from operator import sub

max(min(2, 3), max(4, min(2, sub(6, 2)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment