0% found this document useful (0 votes)
14 views

Week1 Lecture2

Uploaded by

frankhao4
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Week1 Lecture2

Uploaded by

frankhao4
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

functions, input & output, importing modules.

Week 1 | Lecture 2 (1.2)


While waiting for class to start: Upcoming:

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

§ Use whitespace to separate variables and


operators
§ >>> canda=cat+panda
§ Be consistent with spacing, too much
whitespace can be bad
§ >>> canda = cat +panda
§ Pick variable names that are easy to read and
interpret
§ >>> canda = nom + nomnomnomnomnom
§ >>> ca = c + p
§ Be consistent with naming schemes
§ >>> Canda = CAT + _panda42
Comments
§ Comments are to help you, and anyone
else who is reading/using your code, to
remember or understand the purpose
of a given variable or function in a
program.
§ A comment begins with the number
sign (#) and goes until the end of the
line.
§ Python ignores any lines that start with
the (#) character
Warning! This is not Python! It is an example from one of my iOS apps I had to come
back to after a few years. Comments are (//) in Swift instead of (#) in Python
Testing!
§ The more lines of code you write, the more
likely it is that you will make a mistake and
the harder it will be to find the mistake
§ “like finding a needle in a haystack”
Golden Rule: Never spend
§ Test your code as you write it more than 15 minutes
§ Requires you understanding what specific programming without testing!
output an input will provide
§ ”Modular code”
§ Test in small chunks or “modules”
§ Put a test input into the beginning where you
know what the output is and see what you get!
Error Reduction vs Debugging
§ It is pretty much impossible to write
code without errors.
§ Error Reduction: techniques we can
use to reduce the number and
severity of errors.
§ Debugging: techniques for
identifying and correcting errors
§ How Does That Work? Video
Code Efficiency

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.

§ The general form of a function call:


function_name(arguments)
Call
function_name() Function Function
()
function_name Would not result in
a function call.
§ Terminology The stuff the
§ argument: a value given to a function. function returns to
us after we call it. Returns
§ 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 general form of a function call: Open your
notebook
function_name(arguments)
Click Link:
2. Function Call
§ Terminology
§ argument: a value given to a function.
§ 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.
Back to evaluation and expressions
§ Earlier we learned about the First, the function is called
assignment statement (=). while passing it an
argument.
§ Remember, the value of the
expression on the right-hand
side (RHS) of the = sign is
figured out first and then x = abs(-20)
assigned to the variable on
the left-hand side.
Then, what the
§ This also applies if the thing function returns is
on the RHS is a function! assigned to x.
Back to evaluation and expressions
x = abs(-20+5)

§ Earlier we learned about the The stuff we pass Arguments ?


to the function.
assignment statement (=).
§ Remember, the value of the
expression on the right-hand
side (RHS) of the = sign is
Function
figured out first and then ?

assigned to the variable on


the left-hand side. The stuff the
§ This also applies if the thing function returns to
us after we call it. Returns ?
on the RHS is a function!
Back to evaluation and expressions
x = abs(-20+5)

§ Earlier we learned about the The stuff we pass Arguments -15


to the function.
assignment statement (=).
§ Remember, the value of the
expression on the right-hand
side (RHS) of the = sign is
Function
figured out first and then abs

assigned to the variable on


the left-hand side. The stuff the
§ This also applies if the thing function returns to
us after we call it. Returns 15
on the RHS is a function!
Back to evaluation and expressions
§ Earlier we learned about the
assignment statement (=). Open your
§ Remember, the value of the
expression on the right-hand notebook
side (RHS) of the = sign is
figured out first and then Click Link:
assigned to the variable on 3. Back to
the left-hand side. Evaluation and
§ This also applies if the thing Expressions
on the RHS is a function!
Breakout Session 1
!#$ # !∗$
§𝑥 =
!!
§where, Open your
§𝑦 = −20 notebook
§𝑧 = −100
§𝛼 = 2 Click Link:
4. Breakout Session 1
§What is 𝑥 ?
Built-in Functions
§ The function_name is the
name of the function (like
sin or print).
§ Python has many built-in
functions. Learn more
about them here.
Built-in Functions
§ The function_name is the
name of the function (like
sin or print). Open your
§ Python has many built-in
functions. Learn more notebook
about them here.
Click Link:
5. Built-in Functions
Function Help
§ To get information about
a particular function, call
help and pass the Open your
function as the argument.
§ help is one of Python’s notebook
built-in functions.
Click Link:
6. Function Help
§help(abs)
Output
§Python has a built-in function
named print for displaying
messages to the user.
§The general form of a print
Open your
function call: notebook
print(arguments)
Click Link:
§The arguments can be of type 7. Output
int, float, strings and
others we will discuss next week.
Input
§ Python has a built-in function
named input for reading inputs
from the user.
§ The general form of an input Open your
function call:
notebook
input(argument)
§ The argument is the text you want Click Link:
displayed to the user.
§ “What is your name?” 8. Input
§ The value returned by the input
function is always a string.
Breakout Session 2
§Write code to print out the
following text:
"Hello, my name is {} and Open your
I'm hoping to get a grade of
{} in APS106 this term."
notebook
§Where you see curly brackets {} Click Link:
you need to use the input 9. Breakout Session 2
function to prompt the user to
enter that information.
Importing Functions and Modules

§Not all useful functions are built-in and they must be


imported.
§Groups of functions are stored in separate Python files,
which are called modules.
§Some modules come pre-installed with Python and
other need to be installed separately.
§ For example, there are a lot of machine learning methods
implemented in the scikit-learn modules.
§To get access to the functions in a module, you need
to import the module.
Importing Functions and Modules

§The general for of an import statement is:


§ import module_name
§To access a function within a module:
§ module_name.function_name
§The dot is an operator:
1. Look up the object that the
import math variable to the left of the dot
refers to.
math.sqrt(16) 2. In that object, find the name
that occurs to the right of the
dot.
Importing Functions and Modules

§ The general for of an import


statement is:
§ import module_name
§ To access a function within a
module: Open your
§ module_name.function_name
notebook
Click Link:
10. Importing
Function and Modules
Defining Your Own Functions

§ The real power of functions is in


defining your own.
§ Good programs typically consist of
many small functions that call each
other.
§ If you have a function that does
only one thing (like calculate
the sine of an angle), it is likely not
too large.
§ If its not too large, it will be easy to
test and maintain.
Defining Your Own Functions

§ As a general rule, you should not


write functions more than a 30 or
40 lines.
§ Smaller is better: 10 or less is
good.
§ If you need something bigger,
break it up into multiple
functions.
§ #cleancode
Function Definitions
§ The general form of a function definition is:
def function_body(parameters): Colon
Indent body
(parameter1, parameter2, …)
§ def is a keyword, standing for definition. All function definitions must begin
with def. The def statement must end with a colon.
§ function_name is the name you will use to call the function (like sin, abs but
you need to create your own name).
§ parameters are the variables that get values when you call the function. You
can have 0 or more parameters, separated by commas. Must be in parenthesis.
§ body is a sequence of commands like we've already seen (assignment,
multiplication, function calls).
§ Important: all the lines of body must be indented. That is how Python knows
that they are part of the function.
Function Definitions
§ The general form of a function
definition is:
Open your
Definition
notebook
Function Name
Colon

def function_body(parameters): Click Link:


Indent body 11. Defining Your
Own Functions
(parameter1, parameter2, …)
Lecture Recap
§ Functions.
§ Calling functions.
§ Importing Modules.
§ Writing your own functions.

§ More on functions next lecture!


functions, input & output, importing modules.
Week 1 | Lecture 2 (1.2)

You might also like