Answer to Question #236336 in Python for Ashkan

Question #236336


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.



1
Expert's answer
2021-09-12T18:47:51-0400
# 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

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS