Professional Documents
Culture Documents
I was looking at your response, very straight to the point clear and all examples were answered
accordingly and correct. Very well done for your effort.
Kind regards
Thando
31 words
PermalinkShow parentReply
PermalinkShow parentReply
def plus1(num):
# num is a parameter
# parameter is a name used inside a function to refer to the value passed as an argument
return num + 1
# 1 is a argument
# argument is a value provided to a function when the function is called
print(plus1(1))
# Result
# Example 1
#2
print('Example 2')
# argument is a value
print(plus1(1))
# argument is a variable
num = 2
print(plus1(num))
# argument is an expression
print(plus1(1+2))
# Result
# Example 2
#2
#3
#4
print('Example 3')
def print_three_times(str):
three_times = str * 3
print(three_times)
print_three_times('a')
# Result
# Example 3
# aaa
# print(three_times)
# NameError: name 'three_times' is not defined
# Explanation
# A NameError occurred when I try to use the variable outside the function, because the variable
defined inside the function is a local variable.
# A local variable can only used inside its function, so the error happend.
print('Example 4')
def print_four_times(unique_str):
output = unique_str * 4
print(output)
print_four_times('b')
# Result
# Example 4
# bbbb
# print(unique_str)
# NameError: name 'unique_str' is not defined
# Explanation
# A NameError occurred when I try to use the parameter outside the function, because we can't
use parameter outside the function.
# A parameter can only used inside its function, so the error happend.
print('Example 5')
output = 'output'
print(output)
print_four_times('c')
print(output)
# Result
# Example 5
# output
# cccc
# output
# Explanation
# After defining a variable named `output`, both the variable named `output` and the local
variable also named `output` exist.
# First, when I call `print(output)`, the content of the variable named `output` is the string of
`output`.
# Secondly, when I call `print_four_times('c')`, the content of the local variable named `output` is
`cccc`.
# Then, when I call `print(output)`, the content of the variable remains the string of `output`.
# So we can see that we can define and use a variable and a local variable with the same name
separately.
337 words
PermalinkShow parentReply
Example 1 :
def multiplying(a , b):
c=a*b
print(a, " * ", b , " = " ,c)
return c
print("MULTIPLICATION")
x = multiplying(int(input("Enter the value of a : ")),int(input("Enter the value of b : ")))
Example 2 :
def mul(a,b):
return a*b
print(mul(3,4))
Output - 12
a) VALUES
• When passing value arguments 3 and 4 on the function mul(3,4)in Line 3, the function mul(3,4)
does computation and returns the value of 12. Therefore, integers can be multiplied by each
other.
def mul(a,b):
return a*b
x=(15)
print(mul(x,3))
Output - 45
b) VARIABLES
• In Line 4, x is a variable argument. The function mul() evaluates x as 15 and returns the value
of 45.
def mul(a,b):
return a*b
x=(4)
print(mul(((x*5)+5),4))
Output - 100
c) EXPRESSIONS
• In Line 4, x is an expression argument. The function mul() evaluates x as (4*5)+5 = 25, the
function become mul(25,4) which returns 100.
What I can say I have learnt from this example is that arguments can be in different forms, i.e.
they can be in a form of a value, variable or expression
Example 3 :
def adding(a,b,c):
d=a+b+c
print(d)
a=10
b=20
c=30
adding(d)
Output - Traceback (most recent call last):
File "C:/Users/ Softwares/Python/adding.py", line 7, in adding(d)
NameError: name 'd' is not defined. Did you mean: 'id'?
• Local variable d at Line 2 is inside the function. In Line 7, the function finds a name error
because the variable was not defined globally so when interpreter reads Line7, it reads d as a new
variable which was not defined initially that is why the function is not printing out a value. See
below, an example showing a function with globally defined variables that returns a value of 60.
def adding(a,b,c):
d=a+b+c
print(d)
a=10
b=20
c=30
adding(a,b,c)
Output - 60
Example 4 :
def areaofasquare(a,b):
print("The area of a square is")
square = a*b
return(square)
print(a,b)
• In Line 1, the parameters a and b are passed in the function areaofasquare(). When calling print
function at Line 5, arguments a and b are not recognized by the interpreter because they were not
defined or initialized from the first place that why line 5 give us an error.
Example 5 :
university = "UoPeople"
def institution(university):
university = "UoPeople"
print("I study Computer Science at " + university )
institution(university)
PermalinkShow parentReply
PermalinkShow parentReply
PermalinkShow parentReply
def print_characters(Elisabeth):
print(len(Elisabeth))
Exemple 2:
- a value:
>>print_characters ('Hi everyone, my name is Elisabeth')
33
- a variable:
For this, I will need to add a variable. Let's say the letter 'a' is equal to the name Steeve and the
letter 'b' is equal to the name Sophia:
def print_characters(Elisabeth):
print(len(Elisabeth))
a= ('Steeve')
b= ('Sophia')
>>print_characters ('Steeve')
6
- an expression:
now, we can use the expression that will add the names Steeve and Sophia together.
>>print_characters (a+b)
12
Example 3:
I defined a function about the objectives of soccer. Here, the two variables are team objectives 1
and 2.
Exemple 4:
def
178 words
PermalinkShow parentReply
Thank you very much for sharing, this is a really good work. I think your work helped me
understand the difference between a parameter and an argument. From what I understood from
the book a parameter is a variable that is used in the definition of a function or method to
represent a value that will be passed to it. In your example the parameter is name. An argument,
on the other hand, is a value that is passed to a function or method when it is called. In your
example the argument is Alice.
Best wishes,
Maeva
100 words
PermalinkShow parentReply
Best wishes,
Maeva
110 words
PermalinkShow parentReply
Great work, thank you very much for sharing so much details this is really helpful.
About your question I agree with you I was confused by the differences between parameter and
argument.
In general, a parameter is a variable that is used in the definition of a function or method to
represent a value that will be passed to it. An argument, on the other hand, is a value that is
passed to a function or method when it is called. So, when the book says "Inside the function, the
arguments are assigned to variables called parameters," it means that the values that are passed to
the function when it is called (the arguments) are assigned to the variables that are used in the
function definition to represent those values (the parameters).
Best wishes,
Maeva
137 words
PermalinkShow parentReply
Glad to meet you all. Firstly, I want to apologize because I submitted my discussion
forum so late.
For example1,
>>>def welcome(name):
>>>welcome(‘Hsu Mon’)
In this example, the function is welcome. The argument is ‘Hsu Mon’ and the parameter is
‘name’.
>>>def welcome(name):
The argument ‘Hsu Mon’ is a value in the first function. In the second function, the argument
‘name’ is a variable. In the third function, the argument "‘My name is, ‘ + name" is an
expression which combines with a string ‘My name is, ‘ with the
For example 3,
>>>def new_function():
age = 18
print(age)
>>>new_function()
18
In the first case, a function ‘new_function’ is defined with a local variable 'age'. When the
function is called, it prints out the age value of 18. The statement ‘print(age)’ needs to execute
inside the function.
>>>def new_function();
age = 18
>>>new_function()
print(age)
In this case, this will output just an error. Because it is just executed outside of the function that
causes the variable ‘age’ to be not defined.
For example 4,
>>>def shop(eggs):
print(eggs)
>>>shop(100)
100
In this example, a function ‘shop’ is defined with a parameter ‘eggs’. When the function is
called, an argument value ‘100’ is passed to the parameter and printed. If ‘shop(100) is executed
outside the function, it will produce an error.
>>>a = 20
>>>def my_func();
a = 18
>>>my_func()
In this example, there are two cases. One is the inside function, and the other is the outside
function. In the inside function, when the function is called, it prints the value of a, which is 20.
The outside function has the same name as the inside function, but that doesn’t affect their
values. The inside function is defined as a local variable.
In Unit 1, we have learned ‘The Way of the Program, which is the foundation course of
Python programming, and for me, it’s quite easy. But in Unit 2, I struggle a little bit. Learning
the function definition looks easy, but in reality, it is difficult. Because of my illness, I can’t
concentrate on my courses. That makes my assignments late. I’m really sorry for all of you.
443 words
PermalinkShow parentReply
Re: Discussion Assignment - Unit 2
PermalinkShow parentReply
PermalinkShow parentReply
PermalinkShow parentReply
Re: Discussion Assignment - Unit 2
PermalinkShow parentReply
PermalinkShow parentReply
PermalinkShow parentReply
Re: Discussion Assignment - Unit 2
## Example 2:
def print_func(arg):
print(arg)
## Example 3:
def my_func():
local_variable = 42
print(local_variable)
#In this example, we define a function called my_func() that has a local variable called
local_variable. When we try to print out the value of local_variable outside the function, we'll get
a NameError because the variable doesn't exist in the global scope. This is because the
local_variable is defined only within the scope of the my_func() function.
## Example 4:
def print_number(number):
print(number)
print_number(5)
print(number)
#In this example, we define a function called print_number() that takes a parameter called
number. When we try to print out the value of number outside the function, we'll get a
NameError because the variable doesn't exist in the global scope. This is because the number
variable is defined only within the scope of the print_number() function.
## Example 5:
message = "Hello"
def my_func():
message = "World"
print("Inside function:", message)
my_func()
print("Outside function:", message)
#In this example, we define a variable called message outside the my_func() function and
another variable with the same name inside the function. When we call the my_func() function, it
prints out the value of the message variable defined inside the function, which is "World". When
we print out the value of message outside the function, we get the value of the message variable
defined outside the function, which is "Hello". This is because the message variable defined
inside the function is a local variable that only exists within the function's scope and doesn't
affect the value of the message variable defined outside the function.
380 words
PermalinkShow parentReply
def print_twice(maeva):
print(maeva)
print(maeva)
print_twice(‘researcher’)
researcher
researcher
In this example, print_twice is a function that takes an argument. When the function is called, it
prints the value of the parameter named maeva twice. The argument “researcher” is repeated
twice when the function is called.
Example 2:
In this example, we call the function print_twice three times with different types of arguments.
print_twice(22)
22
22
A variable holds a value that is passed to the function as an argument. The name of the variable
we pass as an argument (caroline) has nothing to do with the name of the parameter (maeva).
print_twice('papillon '*2)
papillon papillon
papillon papillon
Example 3:
print(feline)
NameError: name 'feline' is not defined
In this example, we define a function print_twice that contains a local variable feline. When we
try to print feline outside of the function, we get a NameError because feline is not defined in the
global namespace. When we create a variable inside a function, it is local, which means that it
only exists inside the function.
Example 4:
def print_twice(feline):
print(feline)
print(feline)
print(feline)
NameError: name 'feline' is not defined
In this example, we define a function print_twice that takes a parameter feline. When we try to
print feline outside of the function, we get a NameError because feline is not defined in the
global namespace.
Example 5:
call = 22
print(call)
22
In this example, we define a function call_twice that contains a local variable call. This variable
is defined outside of the function by 22 and has the same name as the local variable inside the
function. When we call call_twice, we can access the local variable within the function. When
we print call outside of the function, we get the value that was assigned to it in the global
namespace.
What are some best practices for naming variables and functions in Python, and why is it
important to follow these conventions?
399 words
PermalinkShow parentReply
greeting("Adam")
In this example, name is the parameter and "Adam" is the argument. The greeting() function
takes one parameter called name, which is used in the function body to print a greeting message.
Example 2:
def greeting(name):
print("Hello,"+ name + "!")
greeting("Adam")
greeting("Umar")
greeting("Baba")
In the first call to greeting(), "Adam" is a string literal that is used as an argument. In the second
call, "Umar" is also a string literal. In the third call, "Baba" is another string literal.
Example 3:
def my_func():
x = 42
my_func()
print(x)
in this example, x is a local variable inside the my_func() function. When the function is called,
x is defined and assigned the value 42. However, because x is a local variable, it only exist
within the function's scope. When the function finishes executing, the variable x is destroyed and
is no longer accessible. Therefore, when we try to print x outside the function, a NameError is
raised, indicating that x is not defined.
Example 4:
def double(x):
result = x *2
return result
x=5
print(double(x))
print(x)
In this example, x is defined outside the double() function and is passed as an argument to the
function. The parameter of the function is is also named x, but this does not affect the value of
the variable x defined outside the function. When we print x after calling double() , we see that
the value of x is still 5.
Example 5:
def my_func():
x = 42
print("inside the function, x is",x)
x = 10
my_func()
print("outside the function, x is",x)
In this example, x is defined both outside and inside the my_func() function. When the function
is called, a new variable named x is created with the value 42, and this variable only exists within
the function's scope. When we print x inside the function, we see that its value is 42. When we
print x outside the function, we see that its value is still 10, which is the value of the x variable
defined outside the function.
Question: How does python handle function calls with a variable number of arguments, such as
*args and **kwards?
References
382 words
PermalinkShow parentReply
EXAMPLE 2
greeting("Simon")
Good morning Simon
^^^^^^^^^^^^^^^^
This above is the value argument.
visitor = "Katrina"
greeting(visitor)
Good morning Katrina
^^^^^^^^^^^^^^^^^
This above is the variable argument.
EXAMPLE 3
def increase(value):
number = 10
print(value +10)
increase(13)
23
^^^^^^^^^^^^^^
The variable number is the local variable i.e. accessed only within the function.
print(number)
Traceback (most recent call last):
File "", line 1, in print(number)
NameError: name 'number' is not defined
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The above code generates a name error showing that the number variable cannot be accessed
outside the function.
EXAMPLE 4
def add_next_two(value):
number = (value + 1) + (value + 2)
print(number)
add_next_two(4)
11
number = 7
print(number)
7
^^^^^^^^^^^^^^^^^^^^^^^^
The code above shows a parameter name can be used outside the function and will not interfere
with the operation of the function.
EXAMPLE 5
def previous(value):
print(value - 1)
previous(100)
99
value = 43
print(value)
43
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The code above shows that when a local variable is defined in a function, its significance is
restricted only within the function (outside the function its not existing) and for that reason a
variable can be defined outside the function with the same name without interrupting the
working of the function.
QUESTION.
With regards to the order of execution of functions in specific, are they all executed at once first
considering they can be placed anywhere in the program and still be used?
Thanks.
299 words
PermalinkShow parentReply
PermalinkShow parentReply
Re: Discussion Assignment - Unit 2
PermalinkShow parentReply
PermalinkShow parentReply
PermalinkShow parentReply
PermalinkShow parentReply
PermalinkShow parentReply
PermalinkShow parentReply
PermalinkShow parentReply
greet("Alice")
In this example, greet() is a function that takes one parameter, name. The argument passed when
calling the function is "Alice".
Example 2:
def greet(name):
print("Hello, " + name + "!")
greet("Alice")
greet(name="Bob")
person = "Charlie"
greet(person)
In the first call to greet(), "Alice" is a literal value passed as an argument. In the second call, the
argument is specified by name. In the third call, a variable is passed as the argument.
Example 3:
def function():
x = 42
function()
print(x)
In this example, function() declares a local variable x. The print() statement outside the function
raises a NameError because x is not defined in that scope.
Example 4:
def greet(name):
print("Hello, " + name + "!")
greet("Alice")
print(name)
In this example, greet() takes a parameter name. When the print() statement is called outside the
function, a NameError is raised because name is not defined in that scope.
Example 5:
x = "global"
def function():
x = "local"
print(x)
function()
print(x)
In this example, x is defined as "global" outside the function. Inside the function, a new variable
x is defined and assigned the value "local". When print(x) is called inside the function, it prints
"local". When print(x) is called outside the function, it prints "global", the value of the variable
defined outside the function.
PermalinkShow parentReply
:Input
:Output
welcome backShahad
:Example 2
:Input
:def greet_line(name)
print('welcome back'+name)
I call the function three time using greet_line() with different types#
.greet_line('shahad') #a value
welcome backshahad
.line_name='noor #a variable
greet_line(line_name)
welcome backnoor
:Example 3
:)(def FunctionA
z=10-2*5
:Example 4
:def FunctionA(z)
FunctionA(15)
z=1105
FunctionA(z)
.x is parameter, but when we used outside the function it become not related to the parameter #
:Example 5
n=17
:)(def name_Fun
n=100
)(name_Fun
.the value inside 100 # the variable inside a function mean it only exists inside
..the value outside 17 # the variable outside is only focus on the variable in the outside
208 words
PermalinkShow parentReply
PermalinkShow parentReply
PermalinkShow parentReply
Re: Discussion Assignment - Unit 2
PermalinkShow parentReply
Example 1: Define a function that takes an argument. Call the function. Identify what code is the
argument and what code is the parameter.
def address(name):
print (“Hello! Dear “ + name + where do you live?”)
address(“Jemal”)
Output:
Hello! Dear Jemal, where do you live?
# On creating a function which leads to an argument, first, I write the function which is address
with the parameter called name, and formatting the result which how I need to see on the output
display. Then, to pass an argument and call the function, I type the address and instead of the
name I put the name I need to ask.
So, the code “Jemal” is the argument and the code ‘name’ 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.
# Value argument: is when the value of the argument exactly transcribed into a new variable.
Example ---
def address(name):
print (“Hello! Dear “ + name + where do you live?”)
address(“Tomas”)
Output:
Hello! Dear Jemal, where do you live?
---------------------------------------------------------------------------------------------------------------------
----------
# Variable argument: is used to write a function that can accept a variable of inputs.
Example ---
def address(name):
print (“Hello! Dear “ + name + where do you live?”)
character = “Nur”
address(character)
Output:
Hello! Dear Tomas, where do you live?
---------------------------------------------------------------------------------------------------------------------
----------
# An expression argument: is used to pass values as argument to a function.
Example ---
def address(name):
print (“Hello! Dear “ + name + where do you live?”)
address(“Hana”.upper ())
Output:
Hello! Dear HANA, where do you live?
=====================================================================
=====================================================================
==========
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 ---
def function():
x = 21
function()
print(“Output the function, x =”, x)
Output:
NameError: name 'x' is not defined
# When we try to print the value of x outside of the function, it gives ‘NameError’ because x is a
local variable and not in the global.
=====================================================================
=====================================================================
==========
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.
def character(name):
print(“Hello! “ + name)
character(“John”)
print (John)
Output:
NameError: name 'John' is not defined
# When we try to call the character() function, print value of name outside of the function, it
gives NameError because name is only mentioned in local and not defined in the global.
=====================================================================
=====================================================================
==========
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.
color = “Red”
def my_type():
color = “Blue”
print(“Inside the function, color =”, color)
my_type()
print(“Outside the function, color =”, color)
Output:
Inside the function, color = Blue
Outside the function, color = Red
# As the type of color inside and outside of the function is differ the results differ also in the
output display. And not any Error messages appear on the output, because the global variable
color has its own name outside the function as well as the local variable.
=====================================================================
=====================================================================
==========
End your discussion post with one question related to programming fundamentals learned in this
unit from which your colleagues can formulate a response or generate further discussion.
# Mention all types of arguments with examples that can be passed to a function?
605 words
PermalinkShow parentReply
PermalinkShow parentReply
PermalinkShow parentReply
greeting("Tray")
#For this example, The function is "greeting" and "name" is the parameter. The argument passed
to the function greeting, Is my name, Tray.
#example 2
def greeting(name):
print("Hello, " + name)
greeting("Tray")
greetee = "Prof"
greeting(greetee)
greeting("good morning, " + greetee + '!')
#For this example. The first shows an argument as a value being just a String. The second is an
argument as a Variable which means i can change the value of Greetee and
#the code will change. And the third is an expression, having a combination of multiple values,
Variables, or operations and a call to functions.
#example 3
def my_function():
x= 100
my_function()
print(x)
#when you try to use a local variable outside its scope, you end up with an error saying "x is not
defined".
#This is because x is only defined within the scope of the function.
#example 4
def timesTwo(num1):
num2 = 2
return num1* num2
result = timesTwo(5)
print(num1)
#In this example, when you try and print num1 outside the function we get an error "NameError"
#because Num1 is only defined within the scope of the function "timesTwo"
#example 5
var = 100
def inside_func():
var = 50
print("inside function ", var)
inside_func()
#In this example, The print statement inside the function assigns the value of var to the local
# variable. Outside the function, The value of var can't be changed or affected by the value set
inside the function.
# var is assigned the global value of 100 outside the function, and the local variable doesn't affect
the outside function.
My question to everyone is how would you use Variable in multiple functions? Can the same be
applied to using a function in other functions?
298 words
PermalinkShow parentReply
36 words
PermalinkShow parentReply
19 words
PermalinkShow parentReply
58 words
PermalinkShow parentReply
Example 2:
value_arg = "John"
greet_person(42) # Value argument: 42
greet_person(value_arg) # Variable argument:
value_arg greet_person("Ada" * 2) # Expression argument: "Ada" * 2
Example 3:
def print_local_variable():
local_var = "I am a local variable."
print(local_var)
print_local_variable()
# print(local_var) # Uncommenting this line will cause an error.
# Explanation:
# The local_var is defined inside the function and is not accessible outside the function scope.
# Trying to access it outside the function will result in a NameError.
Example 4:
def print_unique_parameter(unique_param):
print(f"The unique parameter is: {unique_param}")
print_unique_parameter("Hello, World!")
# print(unique_param) # Uncommenting this line will cause an error.
# Explanation: # The 'unique_param' is a parameter of the function, and its scope is limited to the
function.
# Trying to access it outside the function will result in a NameError.
Example 5:
global_var = "I am a global variable."
def print_conflicting_variables():
global_var = "I am a local variable."
Question for further discussion: What are some best practices for managing variable scope and
avoiding naming conflicts between global and local variables in your code?
258 words
PermalinkShow parentReply
Additionally, I have a fresh insight from your answer in Example4, which describes an “f-string”
format like “print(f"Life is food and {python}")”. It gave me a helpful opportunity to dive into
its functionality.
53 words
PermalinkShow parentReply
>QUESTION:
Yes, I was confused and it was a bit difficult to understand the difference between a parameter
and an argument. As Gilles kindly mentioned in the reply to your post, I have the same opinion
and understanding for this difference now.
73 words
PermalinkShow parentReply
Great job! Your examples of codes are all simple, easy to understand and additionally really fun
to read because words which are used as elements of codes are friendly.
Especially, I am really grateful to you because your result and insight for Example5 made me
notice my misunderstanding, in which I had thought that a variable defined before a function is
called is to be overwritten by a variable which had the same name as it when it was called inside
the function.
85 words
PermalinkShow parentReply
Nice job and I can find clear understandings of the meanings of your codes by way of the
appropriate comments you put after each code.
But, I found some points that did not fit the requirements for this assignment. For instance,
example 3 requires us to construct a program and to examine what happens when we intend to
use a variable which is defined inside the program from outside a function.
In the case of your example3, “y=("book")” seems to represent a definition of a variable internal
of a function. If you use this variable after the function has been executed, it can meet the
requirement of this assignment.
PermalinkShow parentReply
PermalinkShow parentReply
Re: Discussion Assignment - Unit 2
ex1(1, 5)
[Output]
6
You find the addition of 1 and 5!
[Explanation]
In this case, the arguments are defined to be input in the statement of the definition of the
function “ex1”. There are two arguments to be passed by an executor of this code. Therefore,
you can find two arguments passed in the execution of the code “ex1(1, 5)”.
The parameters are defined as “arg1” and “arg2” as well. The arguments passed when this
function is called are assigned to these two parameters.
Example 2:
(1)for a value
[Code]
def ex1(arg1, arg2):
result1 = arg1 + arg2
print(result1)
print("You find the addition of " + str(arg1) + " and " + str(arg2) + "!")
[Output]
1003
You find the addition of 3 and 1000!
(2)for a variable
[Code]
def ex1(arg1, arg2):
result1 = arg1 + arg2
print(result1)
print("You find the addition of " + str(arg1) + " and " + str(arg2) + "!")
ex1(variable1, variable2)
[Output]
1212
You find the addition of 505 and 707!
(3)for an expression
[Code]
def ex1(arg1, arg2):
result1 = arg1 + arg2
print(result1)
print("You find the addition of " + str(arg1) + " and " + str(arg2) + "!")
n = 33
[Output]
250
You find the addition of 150 and 100!
Example 3:
[Code]
import math
circle(10)
print(diameter)
print(area)
[Output]
The diameter of the circle is 20
The are of the circle is 314.1592653589793
Traceback (most recent call last):
File "/home/daisukeikari/df.py", line 11, in print(diameter)
NameError: name 'diameter' is not defined
[Explanation]
The valuable “diameter” is defined inside the function “circle”, which is recognised as a local
variable. A local variable becomes invalid after the execution of the function which involves the
local variable. Hence, you cannot use it outside the function.
Example 4:
[Code]
def triangle(base, height): #Two arguments are assigned to each parameter
triangle(10, 5)
print(base)
print(height)
[Output]
The area of the triangle is 25.0!
Traceback (most recent call last):
File "/home/daisukeikari/df.py", line 7, in print(base)
NameError: name 'base' is not defined
[Explanation]
In this case, two arguments defined in the statement of the definition of the function are assigned
to each parameter, base and height. These parameters are valid until the function has been
executed, which means they are invalid after it and you cannot use them outside of the function.
Example 5:
[Code]
area = "An area of a triangle is defined as a half of multiplication of base and height!"
[Output]
An area of a triangle is defined as a half of multiplication of base and height!
25.0
The area of the triangle is 25.0!
>>>
[Explanation]
In this case, when I define the variable “area” before the definition as a local variable, the output
indicates that the first definition for “area” is overwritten by the second one, a local variable.
623 words
PermalinkShow parentReply
PermalinkShow parentReply
PermalinkShow parentReply
PermalinkShow parentReply
PermalinkShow parentReply
PermalinkShow parentReply
PermalinkShow parentReply
PermalinkShow parentReply
PermalinkShow parentReply
Re: Discussion Assignment - Unit 2
PermalinkShow parentReply
PermalinkShow parentReply
PermalinkShow parentReply
Re: Discussion Assignment - Unit 2
PermalinkShow parentReply
def say_hi(name):
print("Hello" +name)
say_hi("Asmaa")
C:\Users\D\PycharmProjects\pythonProject2\venv\Scripts\python.exe C:\Users\D\
PycharmProjects\pythonProject2\tryme.py
HelloAsmaa
Process finished with exit code 0
--------------------------------------------------------------------------------
EXAMPLE(2):
X=("Asmaa")#(X=("Asmaa") ) is an expression
say_hi(X) #(X) is variable
C:\Users\D\PycharmProjects\pythonProject2\venv\Scripts\python.exe C:\Users\D\
PycharmProjects\pythonProject2\tryme.py
HelloAsmaa
---------------------------------------------------------------------------------------------------
EXAMPLE(3):
#1
def my_room(things):
my_room("y")
C:\Users\D\PycharmProjects\pythonProject2\venv\Scripts\python.exe C:\Users\D\
PycharmProjects\pythonProject2\tryme.py
#2
def my_room(things):
my_room("y")
C:\Users\D\PycharmProjects\pythonProject2\venv\Scripts\python.exe C:\Users\D\
PycharmProjects\pythonProject2\tryme.py
y=("book")
^
-------------------------------------------------------------
EXAMPLE(4):
#(1)
def food_menu(day):
C:\Users\D\PycharmProjects\pythonProject2\venv\Scripts\python.exe C:\Users\D\
PycharmProjects\pythonProject2\tryme.py
# (2)
def food_menu(day):
food_menu("monday")
C:\Users\D\PycharmProjects\pythonProject2\venv\Scripts\python.exe C:\Users\D\
PycharmProjects\pythonProject2\tryme.py
day
^
--------------------------------------------------------------------
EXAMPLE (5):
#(1)
def food_menu(day):
T=("and juice")# I made a local variable
food_menu(" monday")
C:\Users\D\PycharmProjects\pythonProject2\venv\Scripts\python.exe C:\Users\D\
PycharmProjects\pythonProject2\tryme.py
#(2)
def food_menu(day):
T=("and juice")
food_menu(" monday")
T=("toy") #The variable outside the function has the same name but a different value
print(T)
C:\Users\D\PycharmProjects\pythonProject2\venv\Scripts\python.exe C:\Users\D\
PycharmProjects\pythonProject2\tryme.py
toy
Process finished with exit code 0
-------------------------------------------------------------
###
My question is my friends what is the difference between Local variable and Global variable?
295 words
PYTHON 2.rtf
PermalinkShow parentReply
def say_hi(name):
print("Hello" +name)
say_hi("Asmaa")
C:\Users\D\PycharmProjects\pythonProject2\venv\Scripts\python.exe C:\Users\D\
PycharmProjects\pythonProject2\tryme.py
HelloAsmaa
--------------------------------------------------------------------------------
EXAMPLE(2):
X=("Asmaa")#(X=("Asmaa") ) is an expression
say_hi(X) #(X) is variable
C:\Users\D\PycharmProjects\pythonProject2\venv\Scripts\python.exe C:\Users\D\
PycharmProjects\pythonProject2\tryme.py
HelloAsmaa
---------------------------------------------------------------------------------------------------
EXAMPLE(3):
#1
def my_room(things):
my_room("y")
C:\Users\D\PycharmProjects\pythonProject2\venv\Scripts\python.exe C:\Users\D\
PycharmProjects\pythonProject2\tryme.py
the things at my room book
#2
def my_room(things):
my_room("y")
C:\Users\D\PycharmProjects\pythonProject2\venv\Scripts\python.exe C:\Users\D\
PycharmProjects\pythonProject2\tryme.py
y=("book")
^
-------------------------------------------------------------
EXAMPLE(4):
#(1)
def food_menu(day):
food_menu("monday")
C:\Users\D\PycharmProjects\pythonProject2\venv\Scripts\python.exe C:\Users\D\
PycharmProjects\pythonProject2\tryme.py
# (2)
def food_menu(day):
food_menu("monday")
C:\Users\D\PycharmProjects\pythonProject2\venv\Scripts\python.exe C:\Users\D\
PycharmProjects\pythonProject2\tryme.py
day
^
--------------------------------------------------------------------
EXAMPLE (5):
#(1)
def food_menu(day):
C:\Users\D\PycharmProjects\pythonProject2\venv\Scripts\python.exe C:\Users\D\
PycharmProjects\pythonProject2\tryme.py
#(2)
def food_menu(day):
T=("and juice")
food_menu(" monday")
T=("toy") #The variable outside the function has the same name but a different value
print(T)
C:\Users\D\PycharmProjects\pythonProject2\venv\Scripts\python.exe C:\Users\D\
PycharmProjects\pythonProject2\tryme.py
toy
-------------------------------------------------------------
###
My question is my friends what is the difference between Local variable and Global variable?
295 words
PYTHON 2.rtf
PermalinkShow parentReply
def say_hi(name):
print("Hello" +name)
say_hi("Asmaa")
C:\Users\D\PycharmProjects\pythonProject2\venv\Scripts\python.exe C:\Users\D\
PycharmProjects\pythonProject2\tryme.py
HelloAsmaa
--------------------------------------------------------------------------------
EXAMPLE(2):
X=("Asmaa")#(X=("Asmaa") ) is an expression
say_hi(X) #(X) is variable
C:\Users\D\PycharmProjects\pythonProject2\venv\Scripts\python.exe C:\Users\D\
PycharmProjects\pythonProject2\tryme.py
HelloAsmaa
---------------------------------------------------------------------------------------------------
EXAMPLE(3):
#1
def my_room(things):
my_room("y")
C:\Users\D\PycharmProjects\pythonProject2\venv\Scripts\python.exe C:\Users\D\
PycharmProjects\pythonProject2\tryme.py
#2
def my_room(things):
my_room("y")
C:\Users\D\PycharmProjects\pythonProject2\venv\Scripts\python.exe C:\Users\D\
PycharmProjects\pythonProject2\tryme.py
y=("book")
^
-------------------------------------------------------------
EXAMPLE(4):
#(1)
def food_menu(day):
food_menu("monday")
C:\Users\D\PycharmProjects\pythonProject2\venv\Scripts\python.exe C:\Users\D\
PycharmProjects\pythonProject2\tryme.py
my dish will be chickenmonday
# (2)
def food_menu(day):
food_menu("monday")
C:\Users\D\PycharmProjects\pythonProject2\venv\Scripts\python.exe C:\Users\D\
PycharmProjects\pythonProject2\tryme.py
day
^
--------------------------------------------------------------------
EXAMPLE (5):
#(1)
def food_menu(day):
food_menu(" monday")
C:\Users\D\PycharmProjects\pythonProject2\venv\Scripts\python.exe C:\Users\D\
PycharmProjects\pythonProject2\tryme.py
#(2)
def food_menu(day):
T=("and juice")
food_menu(" monday")
T=("toy") #The variable outside the function has the same name but a different value
print(T)
C:\Users\D\PycharmProjects\pythonProject2\venv\Scripts\python.exe C:\Users\D\
PycharmProjects\pythonProject2\tryme.py
toy
-------------------------------------------------------------
###
My question is my friends what is the difference between Local variable and Global variable?
295 words
PYTHON 2.rtf
PermalinkShow parentReply
PermalinkShow parentReply
Yes in the beginning I was a little bit confused about what is an argument and what is a
parameter. But now I understand that the parameter is the name you put inside the parenthesis of
the function definition; and an argument is the code you put inside the parenthesis when you call
the function.
150 words
PermalinkShow parentReply
Code:
def sayHello(name):
sayHello(gilles)
This code defines a function called ‘sayHello’. The function has a parameter called ‘name’. The
body of the function has a print statement that will print ‘Hi <name>’ when the function is
called.
The second statement of the code call the sayHello function with the argument ‘gilles’
Output:
sayHello(gilles)
Hi gilles
When the code is ran it prints ‘Hi gilles’. ‘gilles’ is the argument that was passed when we called
the function
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.
Code:
def sayHello(name):
user = 'john'
# in the code bellow we are calling the function with the value 'gilles'
sayHello('gilles')
# in the code bellow we are calling the function with the variable 'user'
sayHello(user)
# in the code bellow we are calling the function with the expression "user + ' doe'"
ouput:
Hi gilles
Hi john
Hi john doe
The first output print out the value ‘gilles’. The second output is the ‘user’ variable. The third
output is the expression ‘user + ‘ doe’’ which prints out john doe.
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.
Code:
def localVariable():
print(var)
print(var)
The above code define a function called ‘localVariable’. Inside the function we assign the value
‘i am a local variable’ to a variable called ‘var’. Then we have a print statement with ‘var’ as
argument.
Then outside the function we have a print statement using ‘var’ as the argument.
Output:
Because I define the variable ‘var’ inside the function ‘’ocalVariable' that variable is only
accessible inside that function. So in the code when I try to print it with the ‘print(var)’ code, that
will create an error because i’m using ‘var’ outside of the function.
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.
def localParam(foo):
print(foo)
print(foo)
In the above code we defined a function called ‘localParam’ that takes a parameter called ‘foo’.
Inside the function we have a print statement to print ‘foo’
Ouput:
The parameter of a function is considered a local variable of that function in python. So we can
not use a parameter of a function outside of that function. In my code I try to use the parameter
outside of the function with the print statement; so we get an error telling us that ‘foo’ is not
defined.
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.
Code:
def my_function():
my_function()
In the above code we defined a variable called ‘x’ outside the function and we also defined a
variable with the same name inside the function called ‘myfunction’. When my function is called
we will print the value of x that is inside my function. Outside the function we use the print
statement to also print the value of x.
Output:
Inside the function, the local variable x is assigned the value 20, and when we print it, we get 20.
Outside the function, the global variable x still has the value 10, which is what we see when we
print it.
Programming question
I really like the math module and the fact that we can do all kinds of math calculation with it. I
am eager to know what other built in modules exist in python. Do you know about any other
modules?
752 words
PermalinkShow parentReply
def print_a(cafe):
>>> print_a("Starbucks")
In this case, `cafe` is the parameter because it’s a variable for argument. When you want to use
this function, you have to replace `cafe` to other words. Argument is “Starbucks” in this function
that is assigned to the parameter `cafe`.
Ex.2
>>>print_a(“McCafe”)
>>> print_a(coffee_shop)
Secondly, `coffee_shop` is a variable that equals `Doutor`. I use `coffee_shop` for variable
>>> print_a("Tully's "+"coffee")
Finally, the expression of this case is `“Tully’s “+”coffee”`. As a result, this evaluates to
`”Tully’s coffee”` and added into the function.
Ex.3
def player_info(name,club):
player=name+club
>>> name1="Trent"
>>> player_info(name1,club1)
I define `player` for local variable in this function. When I try to use this variable outside the
function, I got the NameError which means this variable is not defined. This is because once you
finished your function, your local variable are destroyed so you can use it anymore.
Ex.4
def store(name):
>>> store("KFC")
>>> print(name)
Ex.5
size='huge'
def normal():
size='narrow'
>>> normal()
My town is narrow
>>> size
'huge'
When I defined same variable inside and outside of the function and I called normal(), local
variable which is `narrow` was applied to the results. This results indicate that local variable is
the priority when the program runs. And then, the variable `huge` which is outside of the
function became effective when the function `normal()` completes execution.
Overall, I learned that making own function is convenient and effective to run code and at the
same time, you have to be careful how to use it. Also, I understand that function is something
that given one value, gives another value and it’s not a summary of the process.
424 words
PermalinkShow parentReply
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.
def cube(n): #here n is a parameter
return n*n*n
print (cube (10)) #here 10 is a value
m=12
print (cube(m)) #here m is a variable
print(cube(m+10-5)) #here (m+10-5) is a expression
output: -1000
1728
4913
Explanation: - Using the cube function created in e.g., I have created a variable m that contains
the value 12 & then and then I called a function in that function I create an expression (m+10-5).
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.
n=5
def cube(n):
ans=n*n*n #here ans is a local variable
print(ans)
output: -Traceback (most recent call last):
File "C:/Users/mskan/OneDrive/Desktop/p1.py", line 4, in print(ans)
NameError: name 'ans' is not defined. Did you mean: 'abs'?
Explanation: - The cube function in this e.g., contains a local variable n. When we try to access
this variable outside the cube function, Python returns the Name error as it is unable to identify
the local variable n. The range of local variable n is limited only inside the cube function.
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.
def cube(n):
return n*n*n
print(cube (2)) #8
print(n)
output: -8
Traceback (most recent call last):
File "C:/Users/mskan/OneDrive/Desktop/p1.py", line 4, in print(n)
NameError: name 'n' is not defined
Explanation: - Here I created a function named cube and gave the unique name and after that I
got the output show me the NameError: name 'n' is not defined because variable n is not exist
outside cube ().
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.
var=5 #here var is an outer variable
def sample():
var=10 # here var is a inner variable
print(var) #this statement print the value of inner variable
print(var) #this statement print the value of outer variable
output: -5
Explanation: - The variable var defined outside the sample function is a global variable. The
variable var defined inside the sample function is a local variable. When the code is executed,
python will print return the value of the global variable var=5.
497 words
PermalinkShow parentReply
PermalinkShow parentReply
Resources
PermalinkShow parentReply
Example 1: Define a function that takes an argument. Call the function. Identify what code
is the argument and what code is the parameter.
Code:
def first(thomas):
Output:
>>> first("bob")
bob last
Explanation:
The function creates the parameter, which is called by the argument when it is run.
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.
Code:
def first(thomas):
Output for Value:
>>> first(“bob”)
bob last
Explanation:
Output for Variable:
>>> first(apple)
Give an error because there is no function defined for the variable apple.
Output for Expression:
>>> first("bob" + 20)
Explanation:
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.
Code:
def first(thomas):
Output:
>>> print(thomas)
Explanation:
Variables are local. They can only be used inside the created function. To use them outside the
function, I would need to define that variable outside the function. Here is an example that does
not give an error, since it is defined outside the function.
Code:
def first(thomas):
thomas = 20
Output:
>>> print(thomas)
20
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.
Code:
def first(thomas):
Output:
>>> print(first)
<function first at 0x00000291E2229080>
Explanation:
The function created a function object. So when the name of this parameter is called, it prints the
object code.
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.
Code:
def first(thomas):
thomas = 20
Outputs:
>>> print(thomas)
20
>>> first("bob")
bob last
Explanation:
This example is the solution I gave to example 3 to solve the issue of the error. I ran two outputs
to test the inside and the outside variable. It simply inputs the variable it will call the outside
variable. If I input the function with the variable, it will call the variable with the rest of the
function.
QUESTION:
Did anyone else first confuse parameter with argument, since they are so
similar, especially with this sentence from the book “Inside the function,
the arguments are assigned to variables called parameters”?