0% found this document useful (0 votes)
11 views3 pages

Discussion 2

Uploaded by

adeelmshll
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views3 pages

Discussion 2

Uploaded by

adeelmshll
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Example 1:

#Function
def circle_area(r):
pi = 3.14
area = pi * r**2
print(area)

(circle_area(4))
Explanation:
A function is a set of statements that perform a specific and useful operation (Downey, 2015). As shown above the
function is constructed by using the “def” keyword in Python. In this function the parameter is the variable “r” and
the argument is the value that will be sent to “r” variable. This function takes a value “r” as a parameter and assigns
the argument to a parameter named “r”. When this function is called it calculates the circle area and print the result.

Example 2:

#Function
def circle_area(r):
pi = 3.14
area = pi * r**2
print(area)

circle_area(1)
var = 10
circle_area(var)
circle_area(1+4)

Explanation:
As shown in the code above, firstly, the circle_area is called and a value of 1 is passed as an argument and 3.14 is
printed as a result because 3.14 * 12 = 1. Moreover, for the second time when the function is called a variable (var =
10) is passed as an argument which has been previously declared and the result is 314.0. Lastly, an expression is
passed (1+4) as an argument which means 5 and the result is 78.5.
Example 3:

def user_name(name):
h = 'Hello, '
print(h,name)

user_name("Mashall")
print(h, "Kye")
Explanation:
I have created a function user_name with a parameter of “name”. This function takes a name as argument and then
prints Hello + name. Local variables are the variables that are declared inside a function or class and they cannot
outside a function and their scope is limited to function or class that they are defined. (Halder, 2023). The local
variable “h” inside the function has a string value of “Hello, “and is a local variable. If we want to access the value
of “h” outside of the function the interpreter will throw error shown below:

Traceback (most recent call last):


File "/Users/apple/PycharmProjects/csexcrcises/main.py", line 7, in <module>
print(h, "Kye")
NameError: name 'h' is not defined

One way to access the variable “h” outside the function is to use a keyword global in front of the variable which
makes it a global variable.

Example 4:
def sum_two_numbers(number_a, number_b):
sum = number_a + number_b
print(sum)

number_a = 2
number_b = 3
sum_two_numbers(2,2)
sum_two_numbers(number_a, number_b)
)

Explanation:
This function has two parameters number_a and number_b and calculates the sum of these two numbers. In Python
we can use the name of the parameters outside the function as variables because Python will treat them as
independent variables and not associate them with the parameters and the function will not use the variables outside
the function. The result of using the same name of variables as parameters in a function is shown below:

Example 5:

number = 10

def percentage_number (percentage, number):


sum_per = (percentage/100)*number
print(sum_per)

percentage_number(50, 10)
Explanation:
This function calculates percentage of a desired number and the variable “number “is a global variable. If we declare
a variable outside a function that it can be accessed through the whole program and inside a function as well
(Dhotre, 2023). So, in this function, “number” is global variable and its scope is the whole program and it is used
inside the function and it works with no error. The result of this program is 5.0 which is true.

References:
Downey, A. (2015). Think Python: How to think like a computer scientist. Green Tree Press.
https://greenteapress.com/thinkpython2/thinkpython2.pdf

Halder, N. (2023, March 21). Python Tutorial | Python Global variables, Local variables, and Nonlocal variables: A
Comprehensive Guide. Medium.com. https://medium.com/@HalderNilimesh/python-tutorial-python-
global-variables-local-variables-and-nonlocal-variables-a-comprehensive-a0217576f5b8

Dhotre, P. (2023). Scope of The Variables in Python. Https://Insideaiml.com/. Retrieved February 12, 2024, from
https://insideaiml.com/blog/Scope-of-the-variables-in-Python-1139

You might also like