Py Toolbox 1
Py Toolbox 1
Py Toolbox 1
functions
P Y T H O N D ATA S C I E N C E TO O L B O X ( PA R T 1 )
Hugo Bowne-Anderson
Instructor
You’ll learn:
De ne functions without parameters
x = str(5)
print(x)
'5'
print(type(x))
<class 'str'>
square()
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):
"""Return 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)
Tuples:
Like a list - can contain multiple values
even_nums = (2, 4, 6)
print(type(even_nums))
<class 'tuple'>
a, b, c = even_nums print(b)
print(c)
print(even_nums[1]) 4
Uses zero-indexing
4
return new_tuple
result = raise_both(2, 3)
print(result)
(8, 9)
Hugo Bowne-Anderson
Instructor
You’ve learned:
How to write functions
Accept multiple parameters
Function body
return new_tuple
Hugo Bowne-Anderson
Instructor
Next chapters:
Functions with default arguments
Nested functions