0% found this document useful (0 votes)
5 views19 pages

Chapter 1

User-defined functions

Uploaded by

Saba Samankan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views19 pages

Chapter 1

User-defined functions

Uploaded by

Saba Samankan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

User-defined

functions
INTRODUCTION TO FUNCTIONS IN PYTHON

Hugo Bowne-Anderson
Instructor
What you will learn:
Define functions without parameters
Define functions with one parameter

Define functions that return a value

Later: multiple arguments, multiple return values

INTRODUCTION TO FUNCTIONS IN PYTHON


Built-in functions
str()

x = str(5)

print(x)

'5'

print(type(x))

<class 'str'>

INTRODUCTION TO FUNCTIONS IN PYTHON


Defining a function
def square(): # <- Function header
new_value = 4 ** 2 # <- Function body
print(new_value)
square()

16

INTRODUCTION TO FUNCTIONS IN PYTHON


Function parameters
def square(value):
new_value = value ** 2
print(new_value)

square(4)

16

square(5)

25

INTRODUCTION TO FUNCTIONS IN PYTHON


Return values from functions
Return a value from a function using return

def square(value):
new_value = value ** 2
return new_value
num = square(4)

print(num)

16

INTRODUCTION TO FUNCTIONS IN PYTHON


Docstrings
Docstrings describe what your function does
Serve as documentation for your function

Placed in the immediate line after the function header

In between triple double quotes """

def square(value):
"""Returns the square of a value."""
new_value = value ** 2
return new_value

INTRODUCTION TO FUNCTIONS IN PYTHON


Let's practice!
INTRODUCTION TO FUNCTIONS IN PYTHON
Multiple Parameters
and Return Values
INTRODUCTION TO FUNCTIONS IN PYTHON

Hugo Bowne-Anderson
Instructor
Multiple function parameters
Accept more than 1 parameter:

def raise_to_power(value1, value2):


"""Raise value1 to the power of value2."""
new_value = value1 ** value2
return new_value

Call function: # of arguments = # of parameters

result = raise_to_power(2, 3)

print(result)

INTRODUCTION TO FUNCTIONS IN PYTHON


A quick jump into tuples
Make functions return multiple values: Tuples!
Tuples:
Like a list - can contain multiple values

Immutable - can’t modify values!

Constructed using parentheses ()

even_nums = (2, 4, 6)

print(type(even_nums))

<class 'tuple'>

INTRODUCTION TO FUNCTIONS IN PYTHON


Unpacking tuples
Unpack a tuple into several print(a)
variables:
2
even_nums = (2, 4, 6)

a, b, c = even_nums print(b)

print(c)

INTRODUCTION TO FUNCTIONS IN PYTHON


Accessing tuple elements
Access tuple elements like second_num = even_nums[1]
you do with lists:
print(second_num)
even_nums = (2, 4, 6)

4
print(even_nums[1])

Uses zero-indexing
4

INTRODUCTION TO FUNCTIONS IN PYTHON


Returning multiple values
def raise_both(value1, value2):
"""Raise value1 to the power of value2
and vice versa."""

new_value1 = value1 ** value2


new_value2 = value2 ** value1

new_tuple = (new_value1, new_value2)

return new_tuple

result = raise_both(2, 3)

print(result)

(8, 9)

INTRODUCTION TO FUNCTIONS IN PYTHON


Let's practice!
INTRODUCTION TO FUNCTIONS IN PYTHON
Bringing it all
together
INTRODUCTION TO FUNCTIONS IN PYTHON

Hugo Bowne-Anderson
Instructor
What you have learned:
How to write functions
Accept multiple parameters

Return multiple values

Up next: Functions for analyzing Twitter data

INTRODUCTION TO FUNCTIONS IN PYTHON


Basic ingredients of a function
Function Header

def raise_both(value1, value2):

Function body

"""Raise value1 to the power of value2


and vice versa."""

new_value1 = value1 ** value2


new_value2 = value2 ** value1

new_tuple = (new_value1, new_value2)

return new_tuple

INTRODUCTION TO FUNCTIONS IN PYTHON


Let's practice!
INTRODUCTION TO FUNCTIONS IN PYTHON

You might also like