Week1 Lecture2
Week1 Lecture2
Download and open the Jupyter Notebook (.ipynb) for • Weekly Exit Quiz is available when you are ready
Lecture 1.2 (take your time, you have unlimited tries)
• Reflection 1 released after Thursday’s Tutorial
You may also use this lecture’s JupyterHub link instead
– in Tutorial, ask a mentor to help with your Jupyter • All Tutorials running this week
installation J
Programming Guide 101
§ Readability
§ If nothing else, write #cleancode
§ Comments
§ Save yourself from yourself
§ Lots of testing!
§ Modular code (you will learn about functions next week)
§ Test often and with purpose
§ Understanding errors
§ Types of errors
§ Error codes
§ Always have a plan!
Readability Tips (#cleancode) >>> canda = cat + panda
Supercomputer in Quebec
What is a function?
§ A function is a piece of code that you
can “call” repeatedly to do one thing.
§ Think about the sin key on your
calculator. It takes in an angle, does
some calculations and returns the
sine of that angle.
§ Python has built-in functions
(today), but programmers can also
create their own user-defined
functions (next lecture).
Why do we write functions?
§ Let’s consider our sine function.
§ In Python, this could take 10 or more
line of code to compute. Open your
§ If you have to compute the sine of an
angle multiple times in your code, this
notebook
means you have to repeat the same 10
lines of code over and over and OVER Click Link:
again! 1. Why do we write
§ This is both inefficient and it creates functions?
more opportunities to bugs (mistakes)
to creep into your code.
Why do we write functions?
§ Reuse:
§ The practice of using the same piece of code in multiple
applications.
§ Abstraction:
§ A technique for managing the complexity of the code (how
much do we really need to know?).
§ model.fit(X, y) à This could train a deep neural network.
§ Collaboration:
§ Easy to read, Easy to modify, Easy to maintain.
§#cleancode
In Python names of
Calling Functions "snake case" or
"pothole case“? variables and
functions use low
§ The general form of a function call: case and
function_name(arguments) underscores.
function_name()
function_name Would not result in function_name
a function call.
§ Terminology Function_Name
§ argument: a value given to a function. FunctionName
§ pass: to provide an argument to a function.
§ call: ask Python to execute a function (by name).
§ return: give a value back to where the function was called from.
Calling Functions The stuff we pass Arguments
to the function.