MAT 370: Python Foundations

Dr. Gilbert

January 7, 2026

Purpose

Because we’ll be using Python heavily in our course, having a foundation in python and programming paradigms will be useful.

In our Day 1 discussion, we got exposure to using python for

  • scalar arithmetic
  • storing values into named variables and operating with those variables
  • initializing and operating with lists
  • testing logical expressions

We also saw that there exist some python “quirks” that we should be aware of and that we should check the contents of our objects to ensure they contain what we expect/want/assume/intend.

This discussion will address the following items.

  • Extending Python functionality with modules.
  • The {numpy} module and creating/operating with vectors, matrices, and other arrays.
  • Repeating sets of instructions via loops (for and while)
  • Defining reusable functions

Arrays and {numpy}

We won’t actually use lists very often in our course

We’re much more interested in special structures, like arrays (think: vectors or matrices)

We can import the {numpy} module and use it to create these special structures

import numpy as np

Note. We used the alias np so that we can type np.function_name() instead of numpy.function_name() when we want to use functionality from this module. Doing this is common in Python.

Now we can use np.array() with lists to define our structures.

myVector = np.array([1, 2, 3])
myMatrix = np.array([
  [4, 5], #row 1
  [6, 7], #row 2
  [8, 9]  #row 3
])

print("My vector is: ", myVector)
print("My matrix is: ", myMatrix)
My vector is:  [1 2 3]
My matrix is:  [[4 5]
 [6 7]
 [8 9]]

Arithmetic with {numpy} Arrays

Arithmetic on arrays works the way you would want it to work, mathematically.

myVec_v1 = np.array([1, 2, 3])
myVec_v2 = np.array([4, 5, 6])

print(myVec_v1 + myVec_v2)
[5 7 9]
myMatrix_A = np.array([
  [4, 5],
  [6, 7],
  [8, 9]
  ])

print(5*myMatrix_A)
[[20 25]
 [30 35]
 [40 45]]

Developers also build in some conveniences which aren’t mathematically meaningful, but are colloquially understandable and useful!

myVec_v3 = myVec_v1 + 7

print(myVec_v3)
[ 8  9 10]

Programming Paradigm: Loops

Computers are excellent at following instructions and performing tedious tasks.

It is often the case where we’ll want to perform the same set of instructions over and over again.

If we find ourselves in this situation, loops will be helpful.

We’ll encounter two types of loop in our course.

  • A for loop is useful when we know ahead of time how many iterations our instructions must be run for.

  • A while loop can be used when we would like to run a set of instructions over and over again until a condition is no longer satisfied.

    • Be careful with these while loops though – using the wrong stopping condition can lead to infinite loops or unnecessarily lengthy procedures.

Programming Paradigm: Loops

  • A for loop is useful when we know ahead of time how many iterations our instructions must be run for.

  • A while loop can be used when we would like to run a set of instructions over and over again until a condition is no longer satisfied.

    • Be careful with these while loops though – using the wrong stopping condition can lead to infinite loops or unnecessarily lengthy procedures.
#A for loop
my_sum = 0
for i in range(6):
  my_sum = my_sum + i
  print("i is ", i, " and my_sum is ", my_sum)


print("The total sum is: ", my_sum)
i is  0  and my_sum is  0
i is  1  and my_sum is  1
i is  2  and my_sum is  3
i is  3  and my_sum is  6
i is  4  and my_sum is  10
i is  5  and my_sum is  15
The total sum is:  15

Programming Paradigm: Loops

  • A for loop is useful when we know ahead of time how many iterations our instructions must be run for.

  • A while loop can be used when we would like to run a set of instructions over and over again until a condition is no longer satisfied.

    • Be careful with these while loops though – using the wrong stopping condition can lead to infinite loops or unnecessarily lengthy procedures.
#A while loop
x = 1
while x <= 100:
  x = 2*x
  print(x)

print("The final value of x is: ", x)
2
4
8
16
32
64
128
The final value of x is:  128

Programming Paradigm: Conditional Statements

Another useful way to control your code is with the use of conditional statements.

This allows for code to be executed if a condition is true, and perhaps other (or no) code to be executed if it is false.

We use if, elif, and else statements for this.

For example, look at the following for loop which will print out values of x between \(0\) and \(100\) which are divisible by \(13\).

x = 0

for x in range(101):
  if x % 13 == 0:
    print(x)
0
13
26
39
52
65
78
91

Programming Paradigm: Functions

It will often be the case that we’d like to write functions in our course.

Functions begin with the def keyword and end with a return statement.

Consider the following silly function which multiplies two numbers together.

def product(a, b):
  #Function instructions
  myProduct = a*b
  
  #What the function results in
  return myProduct

Now that the product() function has been defined, we can use it!

product(3, 6)
18

Below is a perhaps more useful application.

def f(x):
  #my_output = -3*(x**2) -5*x + 8
  term1 = -3*x**2
  term2 = -5*x
  my_output = term1 + term2 + 8
  return my_output
f(4)
-60

Practice with Python Basics

See how many of the following tasks you can complete. Talk with one another and work together.

  1. Write a Python function mood() that takes a single parameter (temp) and returns
  • "miserable" if temp < 0
  • "unhappy" if temp is at least 0 but less than 25
  • "fine" if temp is between 25 and 80
  • "it's too hot" if temp is above 80
  1. Write a loop that counts down from 10 to 0 and prints "Liftoff!" at the end. Repackage your loop as a function that will take the start of the countdown (countdown_time) as an input parameter, counts down from countdown_time to 0, and then prints "Liftoff!".

  2. Create a function that, given an input array of integers, will print out whether each entry contains an "even" or "odd" number.

  3. Create a function that prints out the first n Fibonacci Numbers. Update your function so that it stores the numbers in a numpy array and returns the array.

Summary

  • In this notebook, you learned some basics about Python and the Jupyter Notebook environment.

  • You were exposed to some of the foundational Python that we’ll need throughout the semester – in particular,

    • importing modules,
    • working with {numpy},
    • using for and while loops to execute the same instructions over and over again,
    • and defining functions to make your code reusable.
  • I expect (and you should, too) that you’ll come back here often over the next couple of weeks as you continue to gain familiarity with Python.

  • In particular, you should feel free to copy/paste/edit from this notebook, especially when you are constructing functions and/or loops.