MATH 150 Python Lab Notes

Basic Arithmetic

Here is how you tell the computer to do some of the basic arithmetic operations in python.

Data Types

We often use computers in the following way:

  1. Give the computer some data, such as numbers, words, letters, etc.
  2. Tell the computer to do stuff with the data.
  3. Get the results from the computer so that we can use them in some meaningful way.

Computers can do all kinds of things with all kinds of data. What a computer can and cannot do with a piece of data largely depends on what "type" of data it is. Here are some of the common data types that we will encounter:

  1. Number, such as 7, 11, 3.14159, 7 + 6i, etc.
  2. Text (also known as strings), such as "hello", "a", "How are you?", etc.
  3. Boolean (also known as bools), such as True and False.

These don't cover all possible data types, but this is a good starting point for us. Depending on what type of data you give the computer, the computer knows how to do certain things with the data. We don't really need to get into data types too heavily at the moment. We just need to know that they are an important concept in computer programming and that computers can do different things with data depending on what type of data it is.

Using Functions / Operators

Referring to the discussion above, we give a computer some data, and we tell the computer what we want to do with the data. One of the ways that we can get the computer to do stuff with our data is by telling the computer to apply a function or an operator to the data. Here, we use the words function and operator synonymously for the sake of simplicity. We will also only talk about how to use functions that have been previously defined for us. It is possible to create our own functions, but we will save that discussion for a later day.

Now, let us see what a function is and figure out how to use it. Essentially, a function is something that takes in some input, does something, and returns some output. Here is a diagram: $$\text{input} \rightarrow \text{[function]} \rightarrow \text{output}$$ Let's look at a bunch of different functions and see how they work. Remember, we are using the words function and operator synonymously.

The Print Function

The print function tells the computer to display stuff on the screen. It's input is some text, and what it does to the input is that it displays the inputted text on the screen. Here is an example showing how to use it.

Example

The Assignment Operator = versus the Comparison Operator ==

In regular mathematics, we can see statements such as $x = 7$, which can mean either that (1) we are assigning the variable $x$ to have the value $7$, or it can mean that (2) we are comparing the variable $x$ and the number $7$ and saying that $x$ is equal to (as in, the same number as) $7$. Humans have no problem using the same symbol $=$ to have these two separate meanings, but computers aren't capable of making the distinction unless we tell them which definition of $=$ we are using.

Hence, we have to use two different symbols to let the computer differentiate between the two different meanings. In computer code, when we see something such as x = 7, we mean that we are assigning the variable $x$ to have the value $7$. The single = in computer programming is called the assignment operator for this reason.

In contrast, when we see something like x == 7 in computer code, we mean that we are comparing a previously defined variable $x$ and the number $7$ and saying that they have the same value. This == is called the comparison operator. Let's look at an example to make this distinction more clear.

Example with =

Example with ==

Why Data Types are Important

More on Boolean Values and Logical Operators

There are only two Boolean values. They are either True or False. Computers use these values to help make logical decisions. There are certain operators that take some input and return either True or False as its output. Such an operator is called a logical operator. The comparison operator == is an example of a logical operator. Here are some other basic logical operators:

Less than < Operator

Given two numbers x and y, x < y gives True if x is smalled than y, and gives False otherwise.

Greater than >, Less than or Equal to <=, and Greater than or Equal to >= Operators

These three behave similarly to the < operator. We leave as an exercise to the reader to determine when these three logical operators return True and when they return False.

Logical and and Logical or Operators

These operators take in two Boolean values and return either True or False. Here is some code displaying how they behave:

These operators are best for combining multiple other Logical operators together. Here is an example:

Logical not Operator

The not operator takes in one Boolean value and returns either True or False. Here's how it works:

This operator is useful for when you are interested in when some condition does NOT happen. We will see this be used later.

The input Function and the type Function

Sometimes we want the computer to ask someone else to give it data. We do this by telling the computer to use the input function. Here's how it works.

The eval Function to Change Text to Numbers

The eval function is a very powerful (and somewhat dangerous if used incorrectly) function. We won't dive into the details discussing what all eval actually does or what all it can be used for, but rather we will only talk about how we can use eval to change a piece of text data such as '7' into the number 7. One can easily look up the eval function online and learn more about it there.

Here's how we'll be using the eval function:

Function Composition

Suppose that I have two functions $f$ and $g$, and let $x$ be a possible input to the function $g$. Let $g(x)$ be the output of $g$ when it receives $x$ as an input. In a diagram: $$x \rightarrow_g g(x)$$ which is meant to be read as "the input $x$ is sent by the function $g$ to the output $g(x)$". Now suppose that $g(x)$ is a possible input for the function $f$, and let $f(g(x))$ be the output of the function $f$ when it receives $g(x)$ as an input. Once again, in a diagram: $$x \rightarrow_g g(x) \rightarrow_f f(g(x))$$ which is meant to be read as "the input $x$ is sent by the function $g$ to the output $g(x)$, and then $g(x)$ is sent by the function $f$ to the output $f(g(x))$". This process of chaining together the functions $f$ and $g$ by sending $x$ to $f(g(x))$ is called composing $f$ and $g$. Let's look at an example to see how we can use this concept to write better code.

while Loops

A while loop is a block of code that the computer will repeatedly execute until some stopping condition is met. This is most useful when you want the computer to perform the same task over and over again until it produces some desired result.

Let's look at an example to see how to write a while loop and to better understand how it works.

Example

Given two real numbers $a$ and $b$ such that $1 < a < b$, what is the smallest positive integer $n$ such that $a^n > b?$ We can use while loops to answer this question experimentally. Let's look at the following code that solves our problem below:

In the code above, we used two different while loops. The general structure of the loop is of the form

while (some condition is true):
    do something

For instance, in Loop 2, the condition the computer checks is if $a^n \leq b$. In Loop 1, the three indented lines of code that follow the "while" statement is the "something" that the computer does.

In a while loop, the computer first checks if the condition is true, and if it is, then the computer does whatever you've told it to do. Then the computer checks if the condition is still true. If it still is true, then the computer does whatever you've told it to do a second time. It keeps repeating this process of checking and then doing something forever and ever until it checks the condition and finds out that it is no longer true. Once the computer sees that the condition is false, it stops the loop and moves on with the program.

In both loops, we make the computer change the values of some of the variables that are involved in the condition to be checked before it runs through the loop another time. For instance, in Loop 2, we increase the value of the variable $n$ before we make the computer check if $a^n \leq b$. This guarantees that the while loop will eventually stop, since we will eventually reach a large enough $n$ such that $a^n > b$.

It is possible to write a while loop that never stops. This is one of the dangers of while loops. If your computer gets caught in an infinite loop, then it will never proceed with the rest of the code and you will never get the code to do what you want it to do. Here is an example of an infinite loop:

Since $1$ is always equal to $1$, if you run the code above, it will keep printing out integers until you manually hit the stop button at the top of your jupyter notebook. The second print statement that is outside of the while loop will never be printed to the screen, since the computer will be stuck in the while loop forever.

The moral of this story is that you should always make sure the condition the computer checks in the while loop will eventually become false after running through the loop enough times.

SymPy Basics

SymPy is short for Symbolic Python, and is a package that allows us to make the computer work with symbolic expressions such as $ax^2 + bx + c = 0$ or $(a+bi)(c+di) = (ac - bd) + (ad + bc)i$. Let's learn some of the basic commands and see how it works.

Plotting in SymPy

The goal of this section is to learn some of the useful commands for plotting with the sympy package. As per usual, this is best learned through examples.

Okay, we have seen an example showing us how to use the plot function in Python, specifically with the SymPy package. Now let's explore what else we can do with the plot function.

Plotting Multiple Functions at the Same Time

Using the div function

Suppose we have two different polynomial expressions $p(x)$ and $q(x)$ in a single variable $x$. If we want to divide $p(x)$ by $q(x)$, we can use the div function. Let's see how this works:

Substituting a value into an expression

If we have some expression involving a symbol $x$, we can substitute another symbol or a specific value for $x$ using sympy. Here's how:

if statements

Sometimes we wish the computer to check some condition and make a decision based on what that condition says. The if statement is how we get the computer to do this. Here is an example.

This example captures what the if statement does. The computer checks if some condition is true, and if it is, it will do whatever you tell it to do. Otherwise, the computer will do nothing and skip to the next available line of code (or stop if there is no more code after the if statement).

Using else

Now, what if instead of telling the computer to do nothing if the condition it checks is false, what if we want the computer to do something else instead? This is where the else statement comes in. Let's look at an example.

As we can see from the example above, the else statement is written after an if statement for when you want the computer to do something else besides nothing if the condition being checked in the if statement turns out to be false.

Lists

We've learned about a few different data types: numbers, text, and Boolean. We now introduce another data type called a list. Before talking about what lists are in python, we should review what we know about lists in an intuitive sense.

We think of a list as an ordered collection of things. Here is an example of a list:

  1. 12
  2. Banana
  3. :)
  4. Howdy!

We see that the first item on the list is the number 12, the second item is the word Banana, the third is a smiley face :), and the last item is the phrase Howdy! Many different kinds of items can be put together on one list, and each item on the list has a clearly defined order in which it comes in.

We can re-write the list above in a more compact way: [12, Banana, :), Howdy]. The square brackets tell us that we have a list, and each item in the list is separated by a comma. Moreover, the order in which the items are written tell us which order they belong to in the list.

Now we are ready to talk about how lists are defined in Python. Let's look at an example:

There are many more things we can learn about lists, such as different ways to create them, how to combine lists, how to add or remove elements from a list, how to sort through a list, etc. However, we will learn about that later.

for Loops

Sometimes we wish to tell the computer to repeat a certain command for a certain amount of time. This is what for loops are used for. In contrast with while loops, we know exactly how many times we want the computer to do something. Let's look at an example.

As mentioned in the example, there are many other ways one can use a for loop, but this is good enough for now.