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 480: Open Source Mathematical Software
2016-04-06
William Stein
Lectures 5: Basic Python Data Structures: lists, tuples, dicts
Misc:
The TA has the flu, so his office hours tomorrow probably canceled (or unsafe).
Homework due Friday at 6pm. I've almost finished implementing peer grading 😃
Start the screencast!!!!
List Comprehensions
A clean way to make a list of things. You might then iterative over this list, or do something else with it. Very handy in mathematics.
etc.
You can typically rewrite a list comprehension as a for loop (or nested for loops).
KEY INSIGHT: The order of the for loops and the if are exactly the same as in the list comprehension.
Exercise now:
Construct the following list using a list comprehension and the if/else exercise function my_sign
you wrote above:
If you have time, do the same but using a for loop.
Python Lists
WARNING: Python lists are SUBTLE due to references.
A Python list is a an ordered sequence of references to Python objects. And assignment in Python is copy by reference.
This is totally different than how things work with, e.g., MATLAB.
Consider first this:
And here is what happens in MATLAB, actuallyOctave is a free clone of matlab (1-based and copy by value):
And R (1-based and copy by value):
Here's what happens in Javascript (0-based and copy by reference):
And in Julia (1-based and copy by reference!):
a =[5,2,3]
b =[5,2,3]
The point is that different programming languages make very subtle and different design choices. And that's all they are -- choices. One choices really is better for some purposes and worse for other purposes.
The copy by reference choice of Python makes lists potentially subtle. For example:
Now what the heck is w equal to? (Wait until lots of people in class think this through.)
Moral: Just because something prints out at [[1, 2, 3], [1, 2, 3], [1, 2, 3]]
doesn't mean you know what that something is! In Python, things can be very subtle.
Don't judge an object by its cover (how it prints).
If you don't get this point, you might hate Python. If you do, you might start to see huge power and flexibility in what Python offers... and understand why Python might be the most popular programming language in data science, which is itself a very popular emerging field.
Shallow and deep copies.
Let's try the copy module from Python's standard library: https://docs.python.org/2/library/
MORAL: Python is a real programming language designed by software engineers. It's not a special-purpose math-only language, and it respects deeper, and more powerful, subtle (and possibly confusing) ideas from programming.
Some more about data structures: tuples, dicts, sets
tuples
Like Python lists, except you use parens instead of square brackets
You can't change the number of entries or what they reference. However, you might be able to change the referenced thing itself.
Exercise right now: Write a function that takes as input a tuple and returns the sorted version of the tuple.