Python Variables, Expressions, Statements, and Functions Exercises

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: Construct a function with a local variable. Show what happens when you try to use that variable outside the function. Explain the results.

Example 4: Construct 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.

Example 5: Show what happens when a variable defined outside a function has the same name as a local variable inside a function. Explain what happens to the value of each variable as the program runs.

 #Define a function that takes an argument

def greet(name):
print("Hi,", name)
print("Welcome to York")

#Call the function
greet("Moaz")

#Output
Hi, Moaz
Welcome to York

def is a keyword that indicates that this is a function definition, (Downey, 2015, p.19). The name of the function is greet. The argument is “Moaz”, We pass “Moaz” as an argument.


The parameter is "name", this function assigns the argument to a parameter named "name".


Example 2:

#Call the function with the argument as a value:
greet("Moaz Alibrahim")
#Output
Hi, Moaz Alibrahim
Welcome to York

#Call the function with the argument is a variable:

student = "Moaz Alibrahim" #This is a variable

greet(student) #call the function

#Outputs
Hi, Moaz Alibrahim
Welcome th York

#Call the function with the argument is an expression:

greet("Moaz" + " " + "Taha" + " " + "Alibrahim")

#Outputs
Hi, Moaz Taha Alibrahim
Welcome th York

Example 3:

“When you create a variable inside a function, it is local, which means that it only exists inside the function”, (Downey, 2015, p.22). For example:


#Creat a function with a local variable:
def info():
my_name = "Moaz Alibrahim"
info()
print(my_name) #Trying to use a local variable outside of the function
#Output
print(my_name)
^^^^^^^
NameError: name 'my_name' is not defined

When the variable is local and we call it outside the function we will have the error that indicates that the function does not recognize the variable.


Example 4:

#Create a function that takes an argument:
def greet(name):
message = "Hello " + name
print(message)
print(name) #Trying to use that parameter name outside the function.

  print(name)
          ^^^^
NameError: name 'name' is not defined

“parameter: A name used inside a function to refer to the value passed as an argument.” (Downey, 2015, p.26). So the parameter ‘name’ can’t be used outside a function.

Example 5:

#Global variable
university_name = "University of the York"
def the_study():
#Local variable with the same name as the global variable
university_name = "York"
print("Inside the function: University Name =", university_name)
#Call the function
the_study()
#Outside the function, access the global variable
print("Outside the function: University Name =", university_name)
#Output:
Inside the function: University Name = York
Outside the function: University Name = University of the York
Stephen Olubanji Akinpelu

Previous Post Next Post