Python - Functions - Azure Jupyter Notebooks
Python - Functions - Azure Jupyter Notebooks
FUNCTIONS
ADRI JOVIN J J
ASSISTANT PROFESSOR (SR. GR.)
DEPARTMENT OF INFORMATION TECHNOLOGY
SRI RAMAKRISHNA INSTITUTE OF TECHNOLOGY
WHO’S A GREAT TEACHER?
31/05/2018 FUNCTIONS-PYTHON | 3
EXPECTED LEARNING OUTCOMES
31/05/2018 FUNCTIONS-PYTHON | 4
RECOMMENDED TEACHING-AID
31/05/2018 FUNCTIONS-PYTHON | 5
LEARNING RESOURCES
http://notebooks.azure.com
24x7 availability
Create Libraries
Clone Libraries
31/05/2018 FUNCTIONS-PYTHON | 8
JUPYTER NOTEBOOK
Create Libraries
Clone Libraries
31/05/2018 FUNCTIONS-PYTHON | 9
JUPYTER NOTEBOOK
Create Libraries
Clone Libraries
31/05/2018 FUNCTIONS-PYTHON | 10
JUPYTER NOTEBOOK
Click “New”
Note: Item Type must be
selected or the file will be
blank
31/05/2018 FUNCTIONS-PYTHON | 11
JUPYTER NOTEBOOK
Code Cell
Markdown Cell
Raw NBConvert Cell
Header Cell
31/05/2018 FUNCTIONS-PYTHON | 12
RUNNING A CELL
31/05/2018 FUNCTIONS-PYTHON | 13
WORKING IN NOTEBOOK
EDIT MODE
text cells in editing mode show markdown code
Markdown cells keep editing mode appearance until the cell is run
code (python 3) cells in editing look the same after editing, but may show different run output
clicking another cell moves the green highlight that indicates which cell has active editing focus
ADD A CELL
Highlight any cell and then... add a new cell using Menu: Insert > Insert Cell Below or Insert Cell Above
Add with Keyboard Shortcut: "ESC + A" to insert above or "ESC + B" to insert below
CHOOSE CELL TYPE
Format cells as Markdown or Code via the toolbar dropdown or Menu: Cell > Cell Type > Code or Markdown
Cells default to Code when created but can be reformatted from code to Markdown and vice versa
CHANGE NOTEBOOK PAGE LANGUAGE
The course uses Python 3 but Jupyter Notebooks can be in Python 2 or 3 (and a language called R)
To change a notebook to Python 3 go to "Menu: Kernel > Change Kernel> Python 3"
31/05/2018 FUNCTIONS-PYTHON | 15
FUNCTIONS WITH ARGUMENTS
Functions are used for code tasks that are intended to be reused
Make code easier to develop and maintain
Python allows
−User Defined Functions
−Built-in Functions (e.g.: print())
31/05/2018 FUNCTIONS-PYTHON | 16
FUNCTIONS WITH ARGUMENTS
31/05/2018 FUNCTIONS-PYTHON | 17
BASICS OF A USER DEFINED FUNCTION
31/05/2018 FUNCTIONS-PYTHON | 18
CERTAIN RULES
31/05/2018 FUNCTIONS-PYTHON | 19
SYNTAX
def some_function():
#code the function tasks indented here
31/05/2018 FUNCTIONS-PYTHON | 20
EXAMPLE
def say_hi():
print("Hello World!")
print("say hi!")
say_hi()
Output:
Hello World!
say hi!
31/05/2018 FUNCTIONS-PYTHON | 21
CALLING FUNCTIONS
31/05/2018 FUNCTIONS-PYTHON | 22
TEST THIS…
Test:
def say_hi():
print("Hello World!")
print("say hi!")
def three_three():
print(33)
# calling the functions
say_hi()
print()
three_three()
31/05/2018 FUNCTIONS-PYTHON | 23
TEST RESULT…
Output:
Hello World!
say hi!
33
31/05/2018 FUNCTIONS-PYTHON | 24
TASK
31/05/2018 FUNCTIONS-PYTHON | 25
FUNCTION WITH PARAMETERS
type() has a parameter for a Python Object and sends back the
type of the object
31/05/2018 FUNCTIONS-PYTHON | 26
ARGUMENT VS PARAMETER
DEFAULT ARGUMENT
31/05/2018 FUNCTIONS-PYTHON | 28
Hi Hello
TASK
31/05/2018 FUNCTIONS-PYTHON | 29
Hi Hello
31/05/2018 FUNCTIONS-PYTHON | 30
Hi Hello
def msg_double(phrase):
double = phrase + " " + phrase
return double
31/05/2018 FUNCTIONS-PYTHON | 31
Hi Hello
TASK
31/05/2018 FUNCTIONS-PYTHON | 32
Hi Hello
31/05/2018 FUNCTIONS-PYTHON | 33
Hi Hello
TASK
31/05/2018 FUNCTIONS-PYTHON | 34
Hi Hello
SEQUENCE/FLOW OF EXECUTION
31/05/2018 FUNCTIONS-PYTHON | 35
In the statement have_hat = hat_available('green') the function hat_available() needs to be called after the function has been defined
Hi Hello
SEQUENCE/FLOW OF EXECUTION
print('hat available is', have_hat) NameError Traceback (most recent call last)
31/05/2018 FUNCTIONS-PYTHON | 36
Hi Hello
TASK
def hat_available(color):
hat_colors = 'black, red, blue, green, white, grey, brown, pink'
return(color.lower() in hat_colors)
31/05/2018 FUNCTIONS-PYTHON | 37
Hi Hello
TASK
31/05/2018 FUNCTIONS-PYTHON | 38
Hi Hello
TRAINING WORKBOOK
https://notebooks.azure.com/adrijovin/libraries/functions-
ge8151
Disclaimer:
All products and company names are trademarks™ or registered® trademarks of their respective holders. Use of them does not imply any affiliation with or endorsement by them.
Jupyter is a registered U.S. Patent & Trademark Office of Project Jupyter, USA
31/05/2018 FUNCTIONS-PYTHON | 39