Create your own Python code examples that demonstrate each of the following. Do not copy examples from the book or any other source. Try to be creative with your examples to demonstrate that you invented them yourself.
Example 1: Define a function that takes an argument. Call the function. Identify what code is the argument and what code is the parameter.
Example 2: Call your function from Example 1 three times with different kinds of arguments: a value, a variable, and an expression. Identify which kind of argument is which.
Example 3: Create a function with a local variable. Show what happens when you try to use that variable outside the function. Explain the results.
# Example 1:
def my_func(num, power=2): # 'num', 'power' - parameters
return num ** power
print(my_func(2, 3)) # 2, 3 - arguments
# Example 2:
print(my_func(2)) # value
var = 12
print(my_func(var)) # variable
print(my_func(var * 2 - 15)) # expression
# Example 3:
def my_func(num):
power = 2 # local variable
return num ** power
print(my_func(5)) # 25
print(power) # NameError: name 'power' is not defined
# variable 'power' is not exist outside my_func
Comments
Leave a comment