Discussion WK 2Discussion WK 2Discussion WK 2Discussion WK 2Discussion WK 2 PDF

Title Discussion WK 2Discussion WK 2Discussion WK 2Discussion WK 2Discussion WK 2
Course Engineerig
Institution University of Uyo
Pages 4
File Size 89.1 KB
File Type PDF
Total Downloads 64
Total Views 161

Summary

Discussion WK 2Discussion WK 2Discussion WK 2Discussion WK 2Discussion WK 2Discussion WK 2Discussion WK 2Discussion WK 2Discussion WK 2Discussion WK 2Discussion WK 2Discussion WK 2Discussion WK 2Discussion WK 2Discussion WK 2Discussion WK 2Discussion WK 2...


Description

1. A function that takes an argument, call the function, identify argument and identify parameter: def laugh_thrice(hahaha): print(hahaha) print(hahaha) print(hahaha) laugh_thrice('hohoho') Output: hohoho hohoho hohoho From the code above, laugh_thrice(hahaha) is the function that takes argument, hahaha is the code’s parameter while “hohoho” is the argument 2. Call function in question 1 with a. Value” laugh_thrice('hehehe') Output: hehehe hehehe hehehe

b. Variable: Lols = 'Laugh Out Loud' laugh_thrice(Lols) Output: Laugh Out Loud Laugh Out Loud Laugh Out Loud c. Expression: laugh_thrice('hihihi' * 5)

Output: hihihihihihihihihihihihihihihi

hihihihihihihihihihihihihihihi hihihihihihihihihihihihihihihi

3. Create a function with a local variable:

def local_var(name): name = ‘John Ataha’ print(name) local_var(‘him’) Output: John Ataha This is a function with a local variable name When the variable name is called outside the function, it shows an error stating that name is not defined:

def local_var(name): name = 'John Ataha' print(name) local_var('him') print(name) Output: John Ataha Traceback (most recent call last): File "C:/Users/user/AppData/Local/Programs/Python/Python3632/UNIT2python.py", line 36, in print(name) NameError: name 'name' is not defined

4. Create a function that takes argument, give it a unique parameter name:

def local_Para(unique_para): unique_para=’Parameters are local too just like local variable’ print(unique_para)

local_Para(‘unique_para’) output:

Parameters are local too just like local variable

Use the parameter name outside the function:

def local_Para(unique_para): unique_para='Parameters are local like local variable' print(unique_para) local_Para('unique_para') print(unique_para) Output: Parameters are local like local variable Traceback (most recent call last): File "C:/Users/user/AppData/Local/Programs/Python/Python3632/UNIT2python.py", line 58, in print(unique_para) NameError: name 'unique_para' is not defined There is an error stating that unique_para is not defined. It means function parameters are local to the function. 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: def outside_var(outside): outside = 'I am an outside variable but this function have a local variable that is named like me, so what do you think I will do?' print(outside) outside_var('what will happen') outside='I am an outside variable'

print(outside)

Output: I am an outside variable but this function have a local variable that is named like me, so what do you think I will do? I am an outside variable Python executes both codes. Python executes the local variable where it is called and the outside variable where it is called as well. The local variable is executed whenever the function is called correctly and the global variable executes when it is called outside the function....


Similar Free PDFs