Lecture 2:

  • This lecture will cover the basics of arithmetic and basic datatypes in python
    • Integers (int)
    • Real numbers (float)
    • Functions in python


  • The set of integers, denoted mathematically as $\mathbb{Z}$, is the collection of all positive and negative whole numbers.
  • In particular, $$\mathbb{Z} := \{...,-3,-2,-1,0,1,2,3,...\}$$
    • Note: this notation is imprecise, but the important thing is we have an intuitive understanding of what an integer is.
  • In python, integers are implemented in the integer class (int). Unlike the $\mathbb{Z}$ or $\mathbb{R}$ which has no maximum, computers have finite memory and as a result are bounded. Later in the course we will talk about precision, overflow and underflow.
  • As usual, we can add, subtract, multiply, divide and exponentiate integers as we would on paper
    • Example: We know that $2+2=4$ but we can also have python give us the answer by running $2+2$

Real Numbers

  • The set of real numbers, denoted mathematically as $\mathbb{R}$, is a bit tricky to define rigorously, but can be thought of as the collection of all decimal sequences. This includes the integers, the rationals, and irrational numbers.

Operators on $\mathbb{Z}$ and $\mathbb{R}$

  • Addition $+$
    • Example: 2+2 = 4
  • Subtraction $-$
    • Example: 3-1=2
  • Multiplication $*$
    • 3*3=9
  • Division $/$
    • Example: 7/2=3.5}
  • Integer Division $//$ (division but you drop the remainder)
    • Note that integer division always rounds to closest non-zero value -i.e. 1//-10 = -1
    • Example: 7//2=3
  • Exponentiation $**$
    • Example 1: 3**2 = 9 is computing $3^2 = 9$
    • Example 2: 4**.5 = 2 is computing $4^{\frac{1}{2}} = \sqrt{4} = 2$
  • ## Recall that you can double click on this cell to see how these expressions are created using MathJax


  • In Python, a function is a a block of code that takes an input (potentially empty) and returns an output (also potentially empty)
  • Function blocks begin with the keyword def followed by the function name and parentheses ( ): followed by a colon.
  • Any input parameters or arguments should be placed within these parentheses. You can also define parameters inside these parentheses.
  • The first statement of a function can be an optional statement - the documentation string of the function or docstring.
  • The code block within every function starts with a colon (:) and is indented.
  • The statement return [expression] exits a function, optionally passing back an expression to the caller. A return statement with no arguments is the same as return None.
  • Source: https://www.tutorialspoint.com/python/python_functions.htm
  • Notice that this is different from what a mathematical function is!
  • That said, we can use python functions to build mathematical functions!

Mathematical Functions

  • A function is a mathematical relation between a set of inputs and a set of outputs were each input is associated to exactly 1 output
  • Example: $$f(x)=x^2 +1$$
  • Non Example: $$g(x)= \pm \sqrt{x}$$
    • You might have heard the expression "vertical line test" in the past. We explore this graphically in the coming days
In [9]:
def f(x):
    Arguments: x -- an int or a float
    Computes $x^2 + 1$ 
    return x **2 + 1
In [10]:
print("f(3) = ", f(3))
print("f(4) = ", f(4))
print("f(5) = ", f(5))
f(3) =  10
f(4) =  17
f(5) =  26


  • Python uses the usual mathematical order of operations
  • Parentheses come before Exponentiation which comes before Multiplication and division which comes before addition and subtraction
In [11]:
# Example: run this cell to try your hand at a question!
# It will ask you for input. Type your answer and hit enter! 
x = 2 + 1 * 3
# input grabs an input from the user/standard input
# int() coerces an object into an integer. 
answer = int(input("What do you think x is equal to? "))
# int(input()) converts the input to an integer 
assert(answer == x)
What do you think x is equal to? 5

Python Functions are not Mathematical Functions

  • As said above, python functions are not necessarily mathematical functions.
  • We can build functions that have multiple outputs!
In [12]:
def g(x):
    """returns the plus/minus square root of a value"""
    return x**.5, -x**.5
In [14]:
print("The positive/negative square root of 4 is ", g(4))
The positive/negative square root of 4 is  (2.0, -2.0)
In [19]:
def print_g(x):
    """Prints g(x)"""
    print("The positive/negative square root of ", x, " is ",x**.5, -x**.5)
In [20]:
The positive/negative square root of  1  is  1.0 -1.0
The positive/negative square root of  2  is  1.4142135623730951 -1.4142135623730951
The positive/negative square root of  3  is  1.7320508075688772 -1.7320508075688772
The positive/negative square root of  4  is  2.0 -2.0
The positive/negative square root of  5  is  2.23606797749979 -2.23606797749979

Functions of functions!

  • You can pass anything you want into a function-- including another function!
  • For example, what if we wanted to build a general print function?
  • We could set up into multiple pieces: first we have a string that describes what that function does on an input x.
  • Next we have the function called function that actually computes a value on x
  • Finally we tie it all together below
In [24]:
def print_function(function_string, function, x):
    f_x = function(x)
    print(function_string, x, " is ",f_x)
In [25]:
f_string = "x^2 + 1 for the given input x ="
print_function(f_string, f, 3)
x^2 + 1 for the given input x = 3  is  10

What just happened?

  • First we called print_function with the inputs "x^2 + 1 for the given input x =", f, 3
  • the function then computes f(3) and finally prints out the result
  • See python tutor code execution


  • Overview of operations and numbers
  • Overview functions ## Next Time:
  • Lists
  • Plotting in python