Invoking Functions
The first step to understand functions from a practical standpoint is using them. The fancy tech name we give to the action of "using" a function is "invoke a function". Python has many functions that we can just use out of the box. For example, print
is a function (check the notes below) that lets you send characters to the console. Let's see a few different examples of invoking the print
function:
print("Hello World")
print(42)
my_name = "Santiago"
print(my_name)
print("Santiago")
In this case, the function name is print
. The only parameter that it receives is "something to print". So we invoke print
passing as an argument that thing that we want to print in the screen.
Check the examples in the editor and complete the assignment: use the len
and sum
function to calculate length_of_hello_world
and sum_of_first_10_numbers
.
Solution 1
print("Hello World")
# The `len` function
print("Length of the string RMOTR:")
length_of_rmotr = len('RMOTR')
print(length_of_rmotr)
# The `sum` function
print("Sum of the first 5 numbers")
sum_of_first_5 = sum([1, 2, 3, 4, 5])
print(sum_of_first_5)
# Your assignments:
# Use the `len` function to replace -1 with the length of the string "Hello World"
length_of_hello_world = len('Hello World')
# Use the `sum` function to replace -1 with the result of the sum of the first 10 numbers
# First 10 numbers being: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 (not including 0)
sum_of_first_10_numbers = sum([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])