Chapter 1
Chapter 1
functions
INTRODUCTION TO FUNCTIONS IN PYTHON
Hugo Bowne-Anderson
Instructor
What you will learn:
Define functions without parameters
Define functions with one parameter
x = str(5)
print(x)
'5'
print(type(x))
<class 'str'>
16
square(4)
16
square(5)
25
def square(value):
new_value = value ** 2
return new_value
num = square(4)
print(num)
16
def square(value):
"""Returns the square of a value."""
new_value = value ** 2
return new_value
Hugo Bowne-Anderson
Instructor
Multiple function parameters
Accept more than 1 parameter:
result = raise_to_power(2, 3)
print(result)
even_nums = (2, 4, 6)
print(type(even_nums))
<class 'tuple'>
a, b, c = even_nums print(b)
print(c)
4
print(even_nums[1])
Uses zero-indexing
4
return new_tuple
result = raise_both(2, 3)
print(result)
(8, 9)
Hugo Bowne-Anderson
Instructor
What you have learned:
How to write functions
Accept multiple parameters
Function body
return new_tuple