Answer to Question #181889 in Python for FORTUNE AIDOO

Question #181889

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 4: Create a function that takes an argument. Give the function parameter a unique name. Show what happens when you try to use that parameter name outside the function. Explain the results.
1
Expert's answer
2021-04-16T03:49:29-0400
"""
example 1
example(test_value) 
test_value its local variable 
test_value -this is a function parameter
example(5)
5 -this is the function argument

"""

def example(test_value):
    print(test_value*5)
example(5)
"""
example 2
example(12) -function call as argument value
example(variable) -function call as argument variable
example('test'[2]) -function call as argument expression
"""
variable = 4 
def example(test_value):
    print(test_value*5)
example(12)
example(variable)
example('test'[2])
"""
example 3 
local_variable is a scoped local variable щтдн within a function
print(local_variable)  - will cause an error because the scope 
of the variable is not available
print(local_variable)  -NameError: name 'local_variable' is not defined

"""
 def example(test_value):
    local_variable =5
    print(test_value*local_variable) 
print(local_variable) 
"""
example 4 
When using a variable name outside of a function, 
a global variable with its own scope and value will be created
uniq_name = "global"
print(uniq_name)
result value global vasriable:
will be displayed:
global
example('local') - working with a local variable uniq_name
will be displayed:
locallocal
"""
def example(uniq_name):
    uniq_name = 2*uniq_name
    print(uniq_name) 
uniq_name = "global"
print(uniq_name)
example('local') - working with a local variable

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

rezene haile
20.11.21, 19:33

nice work

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS