FUNCTIONS
ADRI JOVIN J J
ASSISTANT PROFESSOR (SR. GR.)
DEPARTMENT OF INFORMATIONTECHNOLOGY
SRI RAMAKRISHNA INSTITUTE OFTECHNOLOGY
Seven Days Faculty Development and Training Programme on
GE8151 - Problem Solving and
Python Programming
WHO’S A GREATTEACHER?
The mediocre teacher tells.
The good teacher explains.
The superior teacher demonstrates.
The great teacher inspires.
―William ArthurWard
31/05/2018 FUNCTIONS-PYTHON | 2
OBJECTIVE
Creating a simple function with a parameter
Exploring functions with return values
Creating functions with multiple parameters
Control Flow/Sequence in Python
31/05/2018 FUNCTIONS-PYTHON | 3
EXPECTED LEARNING OUTCOMES
Create functions with a parameter
Create functions with a return value
Create functions with multiple parameters
Understand the control flow in Python
31/05/2018 FUNCTIONS-PYTHON | 4
Cognitive level expected: “apply”
RECOMMENDEDTEACHING-AID
Jupyter Notebook - Open Source (requires Anaconda
environment)
http://notebooks.azure.com - Free cloud platform (requires
Microsoft account, probably a hotmail.com/live.com
account)
Spyder – Open Source (requires Anaconda environment)
31/05/2018 FUNCTIONS-PYTHON | 5
LEARNING RESOURCES
 Official Python 3 Documentation - https://docs.python.org/3/library/index.html
 Dive Into Python - http://www.diveintopython3.net/
 Think Python - http://greenteapress.com/wp/think-python-2e/
 The Official PythonTutorial - https://docs.python.org/3/tutorial/
 Learn Python the HardWay - http://learnpythonthehardway.org/book/
 PEP 8 - https://www.python.org/dev/peps/pep-0008/
 PythonTutor - http://www.pythontutor.com/
 Reserved Keywords in Python -
https://docs.python.org/3.0/reference/lexical_analysis.html#id8 [Don’t use]
31/05/2018 FUNCTIONS-PYTHON | 6
JUPYTER NOTEBOOK
31/05/2018 FUNCTIONS-PYTHON | 7
http://notebooks.azure.com
Log-in with a Microsoft ID like
MSN/Hotmail/Live account
Easy to access from any where
24x7 availability
Easy transfer of notebooks
JUPYTER NOTEBOOK
31/05/2018 FUNCTIONS-PYTHON | 8
 Create Libraries
 Clone Libraries
 Libraries may contain
number of Notebooks
JUPYTER NOTEBOOK
31/05/2018 FUNCTIONS-PYTHON | 9
 Create Libraries
 Clone Libraries
 Libraries may contain
number of Notebooks
 Jupyter Notebook format
(.ipynb files)
JUPYTER NOTEBOOK
31/05/2018 FUNCTIONS-PYTHON | 10
 Create Libraries
 Clone Libraries
 Libraries may contain
number of Notebooks
 Jupyter Notebook format
(.ipynb files)
JUPYTER NOTEBOOK
31/05/2018 FUNCTIONS-PYTHON | 11
 Creating a new Notebook
 Click + symbol > Name the
Notebook
Here, it is
“MY_FIRST_NOTEBOOK”
 Click “New”
Note: ItemType must be
selected or the file will be
blank
JUPYTER NOTEBOOK
31/05/2018 FUNCTIONS-PYTHON | 12
 Code Cell
 Markdown Cell
 Raw NBConvert Cell
 Header Cell
RUNNING A CELL
31/05/2018 FUNCTIONS-PYTHON | 13
Methods for running the code in a cell
 Click in the cell below and press "Ctrl+Enter" to run the code
or
 Click in the cell below and press "Shift+Enter" to run the code and move to the next cell
 Menu: Cell...
 > Run Cells runs the highlighted cell(s)
 > Run All Above runs the highlighted cell and above
 > Run All Below runs the highlighted cell and below
WORKING IN NOTEBOOK
31/05/2018 FUNCTIONS-PYTHON | 14
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
CELLS NEEDTO BE SAVED
 the notebook will frequently auto save
 best practice is to manually save after editing a cell using "Ctrl + S" or alternatively, Menu: File > Save
and Checkpoint
ALTERING NOTEBOOK
31/05/2018 FUNCTIONS-PYTHON | 15
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 CELLTYPE
 Format cells as Markdown or Code via the toolbar dropdown or Menu: Cell > CellType > 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"
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
 print()can be called using arguments (or without) and sends text to
standard output, such as the console.
 print()uses parameters to define the variable arguments that can be
passed to the Function.
 print()defines multiple string/numbers parameters which means we
can send a long list of arguments to print(), separated by commas.
31/05/2018 FUNCTIONS-PYTHON | 17
BASICS OF A USER DEFINED FUNCTION
 define a function with def
 use indentation (4 spaces)
 define parameters
 optional parameters
 return values (or none)
 function scope (basics defaults)
31/05/2018 FUNCTIONS-PYTHON | 18
def some_function() :
CERTAIN RULES
 use a function name that starts with a letter or underscore (usually a
lower-case letter)
 function names can contain letters, numbers or underscores
 parenthesis () follow the function name
 a colon : follows the parenthesis
 the code for the function is indented under the function definition
(use 4 spaces for this course)
31/05/2018 FUNCTIONS-PYTHON | 19
SYNTAX
def some_function():
#code the function tasks indented here
31/05/2018 FUNCTIONS-PYTHON | 20
The end of the function is denoted by returning to no indentation
EXAMPLE
def say_hi():
print("Hello World!")
print("say hi!")
say_hi()
31/05/2018 FUNCTIONS-PYTHON | 21
Output:
Hello World!
say hi!
CALLING FUNCTIONS
 Simple function can be called using the function name followed by
parentheses
31/05/2018 FUNCTIONS-PYTHON | 22
print()
Example:
def say_hi():
print("Hello World!")
print("say hi!")
say_hi()
TESTTHIS…
31/05/2018 FUNCTIONS-PYTHON | 23
Test:
def say_hi():
print("Hello World!")
print("say hi!")
def three_three():
print(33)
# calling the functions
say_hi()
print()
three_three()
TEST RESULT…
31/05/2018 FUNCTIONS-PYTHON | 24
Output:
Hello World!
say hi!
33
TASK
31/05/2018 FUNCTIONS-PYTHON | 25
Define and call a simple function shout()
shout() prints the phrase with "!" concatenated to the end
 takes no arguments
 indented function code does the following
 define a variable for called phrase and initialize with a short phrase
 prints phrase as all upper-case letters followed by "!"
 call shout at the bottom of the cell after the function def
(Tip: no indentation should be used)
FUNCTION WITH PARAMETERS
31/05/2018 FUNCTIONS-PYTHON | 26
 print()and type()are examples of built-in functions that have
parameters defined
 type() has a parameter for a Python Object and sends back the
type of the object
ARGUMENTVS PARAMETER
31/05/2018 FUNCTIONS-PYTHON | 27
 an argument is a value given for a parameter when calling a function
 type is called providing an Argument - in this case the string "Hello"
 Parameters are defined inside of the parenthesis as part of a
function def statement
 Parameters are typically copies of objects that are available for use
in function code
type(“Hello”)
def say_this(phrase):
print(phrase)
DEFAULT ARGUMENT
31/05/2018 FUNCTIONS-PYTHON | 28
 Default Arguments are used if no argument is supplied
 Default arguments are assigned when creating the parameter list
def say_this(phrase = "Hi"):
print(phrase)
say_this()
say_this("Hello")
Hi
Hello
Hi Hello
TASK
31/05/2018 FUNCTIONS-PYTHON | 29
Define shout_this()and call with variable argument
 define variable words_to_shout as a string gathered from user
input()
 Call shout_this() with words_to_shout as argument
 get user input()for the string words_to_shout
Hi Hello
FUNCTION WITH RETURNVALUE
31/05/2018 FUNCTIONS-PYTHON | 30
type()returns an object type
type()can be called with a float the return value can be
stored in a variable
Hi Hello
object_type = type(2.33)
FUNCTION WITH RETURNVALUE
31/05/2018 FUNCTIONS-PYTHON | 31
return keyword in a function returns a value after exiting
the function
Hi Hello
def msg_double(phrase):
double = phrase + " " + phrase
return double
TASK
31/05/2018 FUNCTIONS-PYTHON | 32
Define function print_doctor() that takes a parameter
name
get user input for variable full_name
call the function using full_name as argument
print the return value
Hi Hello
FUNCTION WITH MULTIPLE PARAMETERS
31/05/2018 FUNCTIONS-PYTHON | 33
Functions can have multiple parameters separated by
commas
Hi Hello
def make_schedule(period1, period2):
schedule = ("[1st] " + period1.title() + ", [2nd] " + period2.title())
return schedule
student_schedule = make_schedule("mathematics", "history")
print("SCHEDULE:", student_schedule)
TASK
31/05/2018 FUNCTIONS-PYTHON | 34
Define make_schedule()adding a 3rd period to
 Start with the above example code
 add a parameter period_3
 update function code to add period_3 to the schedule
 call student_schedule()with an additional argument such as
'science'
 print the schedule
Hi Hello
SEQUENCE/FLOW OF EXECUTION
31/05/2018 FUNCTIONS-PYTHON | 35
In programming, sequence refers to the order that code is
processed
Objects in Python, such as variables and functions, are not
available until they have been processed
Processing sequence flows from the top of a page of code to
the bottom
This often means that function definitions are placed at the
beginning of a page of code
Hi Hello
SEQUENCE/FLOW OF EXECUTION
31/05/2018 FUNCTIONS-PYTHON | 36
Hi Hello
have_hat = hat_available('green')
print('hat available is', have_hat)
def hat_available(color):
hat_colors = 'black, red, blue,
green, white, grey, brown, pink'
return(color.lower() in
hat_colors)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-1-95ed6a786fca> in <module>()
----> 1 have_hat = hat_available('green')
2 print('hat available is', have_hat)
3 def hat_available(color):
NameError: name 'hat_available' is not defined
In the statement have_hat = hat_available('green')the function
hat_available()needs to be called after the function has been defined
In the statement have_hat = hat_available('green') the function hat_available() needs to be called after the function has been defined
TASK
31/05/2018 FUNCTIONS-PYTHON | 37
Change the Sequence to fix the NameError
Hi Hello
have_hat = hat_available('green')
print('hat available is', have_hat)
def hat_available(color):
hat_colors = 'black, red, blue, green, white, grey, brown, pink'
return(color.lower() in hat_colors)
TASK
31/05/2018 FUNCTIONS-PYTHON | 38
Create and test market()
 market()takes 2 string arguments: commodity & price
 market returns a string in sentence form
 gather input for commodity_entry and price_entry to use in
calling market()
 print the return value of market()
Example of output: CommodityType: Basket costs Rs. 100/-
Hi Hello
TRAINING WORKBOOK
31/05/2018 FUNCTIONS-PYTHON | 39
https://notebooks.azure.com/adrijovin/libraries/functions-
ge8151
Hi Hello
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
Python is a registered trademark of the Python Software Foundation, USA
Microsoft Azure is a registered trademark of Microsoft Corporation, USA

More Related Content

PPTX
Chapter 02 functions -class xii
PPTX
Advance python programming
PPTX
Python Session - 4
DOC
Datastructure notes
PPTX
C++ ppt
PDF
Python unit 2 as per Anna university syllabus
PPT
C++ for beginners
PPT
Ch3 repetition
Chapter 02 functions -class xii
Advance python programming
Python Session - 4
Datastructure notes
C++ ppt
Python unit 2 as per Anna university syllabus
C++ for beginners
Ch3 repetition

What's hot (20)

PDF
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
PPTX
Functions in c language
PPTX
Function in C program
PPT
Ch4 functions
PDF
Python functions
PDF
Python algorithm
PPT
Basics of c++
PPS
Learn C
PPT
Introduction to C++
PDF
Programming Fundamentals Functions in C and types
PDF
Python recursion
PPTX
Functions in C
PPT
Ch1 principles of software development
PPT
Prsentation on functions
PDF
Lecture20 user definedfunctions.ppt
PPTX
Functions in C
PPTX
Functions in Python
PPTX
PDF
Function in C
PPT
Ch2 introduction to c
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
Functions in c language
Function in C program
Ch4 functions
Python functions
Python algorithm
Basics of c++
Learn C
Introduction to C++
Programming Fundamentals Functions in C and types
Python recursion
Functions in C
Ch1 principles of software development
Prsentation on functions
Lecture20 user definedfunctions.ppt
Functions in C
Functions in Python
Function in C
Ch2 introduction to c
Ad

Similar to Python - Functions - Azure Jupyter Notebooks (20)

PPTX
ForLoopandUserDefinedFunctions.pptx
PDF
Python - Lecture 2
PDF
L14-L16 Functions.pdf
PDF
3-Python Functions.pdf in simple.........
PPT
Python Training v2
PPTX
2_3 Functions 5d.pptx2_3 Functions 5d.pptx
PPTX
powerpoint 2-7.pptx
PPTX
cbse class 12 Python Functions2 for class 12 .pptx
PPTX
Python Programming Basics for begginners
PDF
Functions2.pdf
PPTX
Python and You Series
PPTX
Python Details Functions Description.pptx
PPTX
Python_Functions_Modules_ User define Functions-
PDF
Chapter Functions for grade 12 computer Science
PPTX
Introduction to Python External Course !!!
PPTX
Functions_new.pptx
PDF
Introduction To Programming with Python
PPTX
Teach The Nation To Code.pptx
PDF
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
PDF
Python Course Functions and Modules Slides
ForLoopandUserDefinedFunctions.pptx
Python - Lecture 2
L14-L16 Functions.pdf
3-Python Functions.pdf in simple.........
Python Training v2
2_3 Functions 5d.pptx2_3 Functions 5d.pptx
powerpoint 2-7.pptx
cbse class 12 Python Functions2 for class 12 .pptx
Python Programming Basics for begginners
Functions2.pdf
Python and You Series
Python Details Functions Description.pptx
Python_Functions_Modules_ User define Functions-
Chapter Functions for grade 12 computer Science
Introduction to Python External Course !!!
Functions_new.pptx
Introduction To Programming with Python
Teach The Nation To Code.pptx
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Python Course Functions and Modules Slides
Ad

More from Adri Jovin (20)

PPTX
Heart Bleed Bug - A case study (Course: Cryptography and Network Security)
DOCX
Curriculum Vitae of Adri Jovin John Joseph
PPTX
Introduction to Relational Database Management Systems
PPTX
Introduction to ER Diagrams
PPTX
Introduction to Database Management Systems
PPTX
Neural Networks
PPTX
Introduction to Genetic Algorithm
PPTX
Introduction to Fuzzy logic
PPTX
Introduction to Artificial Neural Networks
PPTX
Introductory Session on Soft Computing
PPTX
Creative Commons
PPTX
Image based security
PPTX
Blockchain Technologies
PPTX
Introduction to Cybersecurity
PPTX
Advanced Encryption System & Block Cipher Modes of Operations
PPTX
Heartbleed Bug: A case study
PPTX
Zoom: Privacy and Security - A case study
PPTX
Elliptic Curve Cryptography
PPTX
El Gamal Cryptosystem
PPTX
Data Encryption Standard
Heart Bleed Bug - A case study (Course: Cryptography and Network Security)
Curriculum Vitae of Adri Jovin John Joseph
Introduction to Relational Database Management Systems
Introduction to ER Diagrams
Introduction to Database Management Systems
Neural Networks
Introduction to Genetic Algorithm
Introduction to Fuzzy logic
Introduction to Artificial Neural Networks
Introductory Session on Soft Computing
Creative Commons
Image based security
Blockchain Technologies
Introduction to Cybersecurity
Advanced Encryption System & Block Cipher Modes of Operations
Heartbleed Bug: A case study
Zoom: Privacy and Security - A case study
Elliptic Curve Cryptography
El Gamal Cryptosystem
Data Encryption Standard

Recently uploaded (20)

PDF
Designing Intelligence for the Shop Floor.pdf
PDF
The Dynamic Duo Transforming Financial Accounting Systems Through Modern Expe...
PDF
How to Make Money in the Metaverse_ Top Strategies for Beginners.pdf
PDF
How Tridens DevSecOps Ensures Compliance, Security, and Agility
PPTX
AMADEUS TRAVEL AGENT SOFTWARE | AMADEUS TICKETING SYSTEM
PDF
Visual explanation of Dijkstra's Algorithm using Python
PPTX
Patient Appointment Booking in Odoo with online payment
PDF
Salesforce Agentforce AI Implementation.pdf
PDF
Top 10 Software Development Trends to Watch in 2025 🚀.pdf
DOCX
How to Use SharePoint as an ISO-Compliant Document Management System
PPTX
Cybersecurity: Protecting the Digital World
PDF
Website Design Services for Small Businesses.pdf
PDF
Product Update: Alluxio AI 3.7 Now with Sub-Millisecond Latency
PPTX
Advanced SystemCare Ultimate Crack + Portable (2025)
PPTX
Computer Software - Technology and Livelihood Education
PDF
Cost to Outsource Software Development in 2025
PPTX
Tech Workshop Escape Room Tech Workshop
PDF
AI Guide for Business Growth - Arna Softech
DOCX
Modern SharePoint Intranet Templates That Boost Employee Engagement in 2025.docx
PPTX
Trending Python Topics for Data Visualization in 2025
Designing Intelligence for the Shop Floor.pdf
The Dynamic Duo Transforming Financial Accounting Systems Through Modern Expe...
How to Make Money in the Metaverse_ Top Strategies for Beginners.pdf
How Tridens DevSecOps Ensures Compliance, Security, and Agility
AMADEUS TRAVEL AGENT SOFTWARE | AMADEUS TICKETING SYSTEM
Visual explanation of Dijkstra's Algorithm using Python
Patient Appointment Booking in Odoo with online payment
Salesforce Agentforce AI Implementation.pdf
Top 10 Software Development Trends to Watch in 2025 🚀.pdf
How to Use SharePoint as an ISO-Compliant Document Management System
Cybersecurity: Protecting the Digital World
Website Design Services for Small Businesses.pdf
Product Update: Alluxio AI 3.7 Now with Sub-Millisecond Latency
Advanced SystemCare Ultimate Crack + Portable (2025)
Computer Software - Technology and Livelihood Education
Cost to Outsource Software Development in 2025
Tech Workshop Escape Room Tech Workshop
AI Guide for Business Growth - Arna Softech
Modern SharePoint Intranet Templates That Boost Employee Engagement in 2025.docx
Trending Python Topics for Data Visualization in 2025

Python - Functions - Azure Jupyter Notebooks

  • 1. FUNCTIONS ADRI JOVIN J J ASSISTANT PROFESSOR (SR. GR.) DEPARTMENT OF INFORMATIONTECHNOLOGY SRI RAMAKRISHNA INSTITUTE OFTECHNOLOGY Seven Days Faculty Development and Training Programme on GE8151 - Problem Solving and Python Programming
  • 2. WHO’S A GREATTEACHER? The mediocre teacher tells. The good teacher explains. The superior teacher demonstrates. The great teacher inspires. ―William ArthurWard 31/05/2018 FUNCTIONS-PYTHON | 2
  • 3. OBJECTIVE Creating a simple function with a parameter Exploring functions with return values Creating functions with multiple parameters Control Flow/Sequence in Python 31/05/2018 FUNCTIONS-PYTHON | 3
  • 4. EXPECTED LEARNING OUTCOMES Create functions with a parameter Create functions with a return value Create functions with multiple parameters Understand the control flow in Python 31/05/2018 FUNCTIONS-PYTHON | 4 Cognitive level expected: “apply”
  • 5. RECOMMENDEDTEACHING-AID Jupyter Notebook - Open Source (requires Anaconda environment) http://notebooks.azure.com - Free cloud platform (requires Microsoft account, probably a hotmail.com/live.com account) Spyder – Open Source (requires Anaconda environment) 31/05/2018 FUNCTIONS-PYTHON | 5
  • 6. LEARNING RESOURCES  Official Python 3 Documentation - https://docs.python.org/3/library/index.html  Dive Into Python - http://www.diveintopython3.net/  Think Python - http://greenteapress.com/wp/think-python-2e/  The Official PythonTutorial - https://docs.python.org/3/tutorial/  Learn Python the HardWay - http://learnpythonthehardway.org/book/  PEP 8 - https://www.python.org/dev/peps/pep-0008/  PythonTutor - http://www.pythontutor.com/  Reserved Keywords in Python - https://docs.python.org/3.0/reference/lexical_analysis.html#id8 [Don’t use] 31/05/2018 FUNCTIONS-PYTHON | 6
  • 7. JUPYTER NOTEBOOK 31/05/2018 FUNCTIONS-PYTHON | 7 http://notebooks.azure.com Log-in with a Microsoft ID like MSN/Hotmail/Live account Easy to access from any where 24x7 availability Easy transfer of notebooks
  • 8. JUPYTER NOTEBOOK 31/05/2018 FUNCTIONS-PYTHON | 8  Create Libraries  Clone Libraries  Libraries may contain number of Notebooks
  • 9. JUPYTER NOTEBOOK 31/05/2018 FUNCTIONS-PYTHON | 9  Create Libraries  Clone Libraries  Libraries may contain number of Notebooks  Jupyter Notebook format (.ipynb files)
  • 10. JUPYTER NOTEBOOK 31/05/2018 FUNCTIONS-PYTHON | 10  Create Libraries  Clone Libraries  Libraries may contain number of Notebooks  Jupyter Notebook format (.ipynb files)
  • 11. JUPYTER NOTEBOOK 31/05/2018 FUNCTIONS-PYTHON | 11  Creating a new Notebook  Click + symbol > Name the Notebook Here, it is “MY_FIRST_NOTEBOOK”  Click “New” Note: ItemType must be selected or the file will be blank
  • 12. JUPYTER NOTEBOOK 31/05/2018 FUNCTIONS-PYTHON | 12  Code Cell  Markdown Cell  Raw NBConvert Cell  Header Cell
  • 13. RUNNING A CELL 31/05/2018 FUNCTIONS-PYTHON | 13 Methods for running the code in a cell  Click in the cell below and press "Ctrl+Enter" to run the code or  Click in the cell below and press "Shift+Enter" to run the code and move to the next cell  Menu: Cell...  > Run Cells runs the highlighted cell(s)  > Run All Above runs the highlighted cell and above  > Run All Below runs the highlighted cell and below
  • 14. WORKING IN NOTEBOOK 31/05/2018 FUNCTIONS-PYTHON | 14 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 CELLS NEEDTO BE SAVED  the notebook will frequently auto save  best practice is to manually save after editing a cell using "Ctrl + S" or alternatively, Menu: File > Save and Checkpoint
  • 15. ALTERING NOTEBOOK 31/05/2018 FUNCTIONS-PYTHON | 15 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 CELLTYPE  Format cells as Markdown or Code via the toolbar dropdown or Menu: Cell > CellType > 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"
  • 16. 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
  • 17. FUNCTIONS WITH ARGUMENTS  print()can be called using arguments (or without) and sends text to standard output, such as the console.  print()uses parameters to define the variable arguments that can be passed to the Function.  print()defines multiple string/numbers parameters which means we can send a long list of arguments to print(), separated by commas. 31/05/2018 FUNCTIONS-PYTHON | 17
  • 18. BASICS OF A USER DEFINED FUNCTION  define a function with def  use indentation (4 spaces)  define parameters  optional parameters  return values (or none)  function scope (basics defaults) 31/05/2018 FUNCTIONS-PYTHON | 18 def some_function() :
  • 19. CERTAIN RULES  use a function name that starts with a letter or underscore (usually a lower-case letter)  function names can contain letters, numbers or underscores  parenthesis () follow the function name  a colon : follows the parenthesis  the code for the function is indented under the function definition (use 4 spaces for this course) 31/05/2018 FUNCTIONS-PYTHON | 19
  • 20. SYNTAX def some_function(): #code the function tasks indented here 31/05/2018 FUNCTIONS-PYTHON | 20 The end of the function is denoted by returning to no indentation
  • 21. EXAMPLE def say_hi(): print("Hello World!") print("say hi!") say_hi() 31/05/2018 FUNCTIONS-PYTHON | 21 Output: Hello World! say hi!
  • 22. CALLING FUNCTIONS  Simple function can be called using the function name followed by parentheses 31/05/2018 FUNCTIONS-PYTHON | 22 print() Example: def say_hi(): print("Hello World!") print("say hi!") say_hi()
  • 23. TESTTHIS… 31/05/2018 FUNCTIONS-PYTHON | 23 Test: def say_hi(): print("Hello World!") print("say hi!") def three_three(): print(33) # calling the functions say_hi() print() three_three()
  • 24. TEST RESULT… 31/05/2018 FUNCTIONS-PYTHON | 24 Output: Hello World! say hi! 33
  • 25. TASK 31/05/2018 FUNCTIONS-PYTHON | 25 Define and call a simple function shout() shout() prints the phrase with "!" concatenated to the end  takes no arguments  indented function code does the following  define a variable for called phrase and initialize with a short phrase  prints phrase as all upper-case letters followed by "!"  call shout at the bottom of the cell after the function def (Tip: no indentation should be used)
  • 26. FUNCTION WITH PARAMETERS 31/05/2018 FUNCTIONS-PYTHON | 26  print()and type()are examples of built-in functions that have parameters defined  type() has a parameter for a Python Object and sends back the type of the object
  • 27. ARGUMENTVS PARAMETER 31/05/2018 FUNCTIONS-PYTHON | 27  an argument is a value given for a parameter when calling a function  type is called providing an Argument - in this case the string "Hello"  Parameters are defined inside of the parenthesis as part of a function def statement  Parameters are typically copies of objects that are available for use in function code type(“Hello”) def say_this(phrase): print(phrase)
  • 28. DEFAULT ARGUMENT 31/05/2018 FUNCTIONS-PYTHON | 28  Default Arguments are used if no argument is supplied  Default arguments are assigned when creating the parameter list def say_this(phrase = "Hi"): print(phrase) say_this() say_this("Hello") Hi Hello Hi Hello
  • 29. TASK 31/05/2018 FUNCTIONS-PYTHON | 29 Define shout_this()and call with variable argument  define variable words_to_shout as a string gathered from user input()  Call shout_this() with words_to_shout as argument  get user input()for the string words_to_shout Hi Hello
  • 30. FUNCTION WITH RETURNVALUE 31/05/2018 FUNCTIONS-PYTHON | 30 type()returns an object type type()can be called with a float the return value can be stored in a variable Hi Hello object_type = type(2.33)
  • 31. FUNCTION WITH RETURNVALUE 31/05/2018 FUNCTIONS-PYTHON | 31 return keyword in a function returns a value after exiting the function Hi Hello def msg_double(phrase): double = phrase + " " + phrase return double
  • 32. TASK 31/05/2018 FUNCTIONS-PYTHON | 32 Define function print_doctor() that takes a parameter name get user input for variable full_name call the function using full_name as argument print the return value Hi Hello
  • 33. FUNCTION WITH MULTIPLE PARAMETERS 31/05/2018 FUNCTIONS-PYTHON | 33 Functions can have multiple parameters separated by commas Hi Hello def make_schedule(period1, period2): schedule = ("[1st] " + period1.title() + ", [2nd] " + period2.title()) return schedule student_schedule = make_schedule("mathematics", "history") print("SCHEDULE:", student_schedule)
  • 34. TASK 31/05/2018 FUNCTIONS-PYTHON | 34 Define make_schedule()adding a 3rd period to  Start with the above example code  add a parameter period_3  update function code to add period_3 to the schedule  call student_schedule()with an additional argument such as 'science'  print the schedule Hi Hello
  • 35. SEQUENCE/FLOW OF EXECUTION 31/05/2018 FUNCTIONS-PYTHON | 35 In programming, sequence refers to the order that code is processed Objects in Python, such as variables and functions, are not available until they have been processed Processing sequence flows from the top of a page of code to the bottom This often means that function definitions are placed at the beginning of a page of code Hi Hello
  • 36. SEQUENCE/FLOW OF EXECUTION 31/05/2018 FUNCTIONS-PYTHON | 36 Hi Hello have_hat = hat_available('green') print('hat available is', have_hat) def hat_available(color): hat_colors = 'black, red, blue, green, white, grey, brown, pink' return(color.lower() in hat_colors) --------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-1-95ed6a786fca> in <module>() ----> 1 have_hat = hat_available('green') 2 print('hat available is', have_hat) 3 def hat_available(color): NameError: name 'hat_available' is not defined In the statement have_hat = hat_available('green')the function hat_available()needs to be called after the function has been defined In the statement have_hat = hat_available('green') the function hat_available() needs to be called after the function has been defined
  • 37. TASK 31/05/2018 FUNCTIONS-PYTHON | 37 Change the Sequence to fix the NameError Hi Hello have_hat = hat_available('green') print('hat available is', have_hat) def hat_available(color): hat_colors = 'black, red, blue, green, white, grey, brown, pink' return(color.lower() in hat_colors)
  • 38. TASK 31/05/2018 FUNCTIONS-PYTHON | 38 Create and test market()  market()takes 2 string arguments: commodity & price  market returns a string in sentence form  gather input for commodity_entry and price_entry to use in calling market()  print the return value of market() Example of output: CommodityType: Basket costs Rs. 100/- Hi Hello
  • 39. TRAINING WORKBOOK 31/05/2018 FUNCTIONS-PYTHON | 39 https://notebooks.azure.com/adrijovin/libraries/functions- ge8151 Hi Hello 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 Python is a registered trademark of the Python Software Foundation, USA Microsoft Azure is a registered trademark of Microsoft Corporation, USA

Editor's Notes

  • #26: def yell_it(): phrase=input("Enter a word: ") print(phrase.upper()+"!") yell_it()
  • #27: def yell_it(): phrase=input("Enter a word: ") print(phrase.upper()+"!") yell_it()
  • #28: def yell_it(): phrase=input("Enter a word: ") print(phrase.upper()+"!") yell_it()
  • #29: def yell_it(): phrase=input("Enter a word: ") print(phrase.upper()+"!") yell_it()
  • #30: def yell_it(): phrase=input("Enter a word: ") print(phrase.upper()+"!") yell_it()
  • #31: def yell_it(): phrase=input("Enter a word: ") print(phrase.upper()+"!") yell_it()
  • #32: def yell_it(): phrase=input("Enter a word: ") print(phrase.upper()+"!") yell_it()
  • #33: def make_doctor(name): return "Dr. "+name full_name=input("Enter a name: ") print(make_doctor(full_name))
  • #34: def yell_it(): phrase=input("Enter a word: ") print(phrase.upper()+"!") yell_it()
  • #35: def make_doctor(name): return "Dr. "+name full_name=input("Enter a name: ") print(make_doctor(full_name))
  • #36: def make_doctor(name): return "Dr. "+name full_name=input("Enter a name: ") print(make_doctor(full_name))
  • #37: def make_doctor(name): return "Dr. "+name full_name=input("Enter a name: ") print(make_doctor(full_name))
  • #38: def make_doctor(name): return "Dr. "+name full_name=input("Enter a name: ") print(make_doctor(full_name))
  • #39: def make_doctor(name): return "Dr. "+name full_name=input("Enter a name: ") print(make_doctor(full_name))
  • #40: def yell_it(): phrase=input("Enter a word: ") print(phrase.upper()+"!") yell_it()