12.
010 Computational
Methods of Scientific
Programming 2021
Tom Herring, tah@[Link]
Chris Hill, cnh@[Link]
Lecture 4: Algorithms and common problems
Topics
• Numpy arrays
• Pandas Data frames: An advancement on simple arrays
• Functions
9/17/2024 12.010 Lec 04 2
Python Data types
• Numpy arrays are the most useful of the basic data types for collecting
things
• Try Lec04_numpy.ipynb
9/17/2024 12.010 Lec 04 3
Python Dataframes
• To do more beyond primitive types (float, array etc…), we need to introduce
more concepts (functions, classes, objects)
• First, look at Dataframes from Pandas () module.
• Pandas Dataframes use functions, classes, and objects to work and show some nice
things that can be encapsulated in a library this way.
• Pandas Data frames – ([Link] )
• Provides an intuitive table-like representation of data with column headings and rows
• Provides a Python-style interface to the table
9/17/2024 12.010 Lec 04 4
Python Dataframes try
• Lec04_DataFrames.ipynb
9/17/2024 12.010 Lec 04 5
Python Functions, Classes and Object
• With arrays and existing libraries like Dataframes (and also for example,
Scipy) can do a lot.
• Can do even more by learning about other core programming concepts
• Functions – a way to create generic “operations” that can be applied to different
instances of similar things.
• Classes – way to group variables with functions to create more advanced function
behaviors
• Objects – “instances” of classes that are variables with functions and internal
variables all grouped together. A Pandas dataframe is an example of an object
• Functions, classes, and objects, together with variables, data types, and
control structures (if, for etc…) are the key elements of programming in
Python.
9/17/2024 12.010 Lec 04 6
Python Functions*
• A basic function is a way to encapsulate some set of steps into a special
collection of code
• Like in a recipe – a separate step might be “make a cheese sauce,” which
might be the same for several recipes.
• Simplest form syntax
def printhi():
print(hi)
def - says this is a function
: - starts function body
everything indented is part of the function e.g.
9/17/2024 12.010 Lec 04 7
Python Functions
• Most things about functions are best tried with examples
• Some key aspects are
• Functions can take arguments, e.g.
• the last example will give an error because the argument does not have a
default setting.
9/17/2024 12.010 Lec 04 8
Python Functions
• Adding a default for an argument is easy!
9/17/2024 12.010 Lec 04 9
Python Functions
• Functions can return a result
9/17/2024 12.010 Lec 04 10
Python Functions
• Special syntax allows for
variable numbers of
arguments
9/17/2024 12.010 Lec 04 11
Python Functions
• Functions can “call”
themselves (recursion)
9/17/2024 12.010 Lec 04 12
Python Functions
• Lab exercises
Lec04_functions.ipynb
9/17/2024 12.010 Lec 04 13
Summary
• Input/Output: Simply first, more later
• Programming approach: Algorithm development
• Example of how to think about the process: Speed of coding versus speed of
execution
• Example with [Link]
• Verification of code. Expecting the unexpected.
• Common problems that can occur.
9/17/2024 12.010 Lec 04 14
MIT OpenCourseWare
[Link]
12.010 Computational Methods of Scientific Programming, Fall 2024
For more information about citing these materials or our Terms of Use,
visit [Link]
09/05/2024 12.010 Lec 01 15