Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Math 152: Intro to Mathematical Software
2017-01-11
Kiran Kedlaya; University of California, San Diego
adapted from lectures by William Stein, University of Washington
Lecture 3: The Python Language (part 1)
Guest lecturer: Alyson Deines (Center for Communications Research)
Administrivia:
No class Monday, January 16 (university holiday: Martin Luther King, Jr. Day). Also no sections or office hours that day.
No instructor office hours on Tuesday, January 17.
Homework 1 due Tuesday, January 17 at 8pm. Remember, put all your work in the "assignments/2017-01-17" folder; it will be collected automatically after the deadline passes.
Guest lecturer Friday, January 20: William Stein, the creator of SageMath and SageMathCloud.
Python is a high-level programming language used throughout the computing world for a variety of tasks, including scientific computation. Its design includes various features meant to improve readability of code, making it ideal for newcomers to programming.
The home page of the Python project is http://www.python.org. From there, you can find a wealth of documentation about the language and its usage. Note that there are two major forms of the language in common circulation: "Python 2" and "Python 3". Since Sage is built on Python 2, we will focus on that; however, the distinctions are mostly subtle and we generally won't run into them in our examples.
In today's lecture, we will focus on:
Hello World (and beyond)
Flow control:
for
,if
,while
. Whitespace matters!Functions
As usual, please follow along and experiment in your copy of this file!
Hello World
Every time you evaluate a cell in SMC, you are running a Python program (unless the cell is marked as a different language, like md as these narrative cells.) In particular, the following is a complete Python program!
Type stuff
One can of course string together commands to make a more interesting program. Note that no punctuation is required at the end of a line.
Flow control
Most interesting programs do something more complicated than simply execute a list of instructions in order. The process by which a program skips or repeats code is called flow control.
Among programming languages, Python is unusual in that whitespace matters: it uses indentation levels to perform flow control. This design choice was made to enforce a degree of readability.
The previous example demonstrates the if
statement. (The else
part is optional.)
The true/false condition can be built out of such ingredients as:
comparison (<, >, ==; note that a single = is assignment, not comparison)
boolean operators (or, and, not)
parentheses (to control the order of operations).
Try some examples below!
The simplest way to repeat an operation is the for
statement, which involves setting one variable (the counter) to a succession of different values. There are several ways to specify the values, but more on that in a moment.
You can put any valid code inside a for
block, including additional flow control.
For more sophisticated iteration, use the while
statement. It keeps repeating the block as long as a true/false condition remains true. (Within the block, ` terminates the loop immediately.)
Functions
Functions are batches of code that can be called from within other code.
A function takes zero or more parameters, and produces zero or more return values. A function may also have side effects, although it cannot change the values of variables outside of the function (that is, variables inside a function are locally scoped).
Definitions of functions persist between cells.
Try this now: write a function that takes four parameters and returns their average.
Try this now: Write a function that takes a number and returns "positive" if the number is greater than 0, "negative" if the number is less than zero, or "zero" if the number is equal to zero. Hint: the statement elif
is equivalent to else if
.
Try this now: Use a while
loop to write a function largest_power_of_2
that takes as input a positive integer n
and returns the largest power of 2 that is less than n
. Your loop should start with pow=1
and while pow*2
is less than n
replaces pow
with pow*2
; once the loop ends, return pow
.
%hide Type stuff