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-04
William Stein
Lectures 4: Basic Python (part 1/3)
Organization:
hmwk from last week graded and returned
hmwk for this week available now
office hours ta discussion
I will leave immediately after class to do a basic demo of SMC for another class today
stickers
remind me to turn on the screencast, since I definitely forgot!
Key things to learn this week:
Basics: functions, whitespace is significant,
if
,while
,for
, list comprehensionsData structures: lists, tuples, dicts, sets
(maybe) Classes:
class
, methods, inheritence
Functions
Exercise now: Write a function avg that takes four arguments (which you may assume are numbers) and returns their average.
If/else Statements
The condition can use ==, !=, and, or, not, and so on.
(Note: Python has no switch statement.)
Exercise now: Write a function that takes a number as input and prints "negative" if it is less than 0, "positive" if it is bigger than 0, and "zero" if it is equal to 0.
For loops
Exercise now: Write a function sum_pow_up_to(n, k)
that returns the sum . You may assume that and are positive integers.
While loops
(You can put break
in the code to immediately exit the while loop.)
Exercise now:
Use a while loop to write a function largest_power_of_2
that takes an input a positive integer and returns the largest power of that is less than . Your loop should start with pow=1 and while pow*2
is less than replaces pow by pow*2
. When the condition fails, just return pow
.