Professional Documents
Culture Documents
Cs 1101 - Ay2020-T5 - Discussion Forum Unit-2
Cs 1101 - Ay2020-T5 - Discussion Forum Unit-2
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.
/
Permalink
def echo(thingToEcho):
thingToEcho = str(thingToEcho) #Converts argument into string
print(thingToEcho + "..." + thingToEcho) #Echos argument and returns the res
ult
>>> echo(1)
1...1
The value 1 is an argument. The variable thingToEcho is a parameter. Parameters are like
variables inside functions that get assigned arguments. The variable thingToEcho got assigned
the value 1.
Arguments aren't only values though. Arguments are just the thing that a parameter holds.
>>> whatIsPi()
Pi is 3.141592653589793.
Then I was told to see what happens when I try to call that variable outside of the function:
>>> print(pi)
Traceback (most recent call last):
File "<pyshell#40>", line 1, in <module>
print(pi)
NameError: name 'pi' is not defined
The variable pi is only defined inside of the whatIsPi() function. It's not defined anywhere else.
So when I call it outside of the function, Python tells me it's not defined.
>>> sayHiTo("Lilly")
Hello, Lilly!
/
>>> petTurtle = "George"
>>> def sayHiToPet():
print("Hi " + petTurtle + "!")
>>> sayHiToPet()
Hi George!
And when you define a variable inside and outside of a function, Python will give you a different
answer depending where you ask it.
Inside of the function, pi equals a floating-point number, so when you call on the function and
the variable inside of it, it gives you what the variable pi is defined as inside the function:
>>> whatIsPi()
3.141592653589793.
Outside of the function, pi is defined as a string, so when you call on the variable outside of the
function, it gives you what it's defined as outside of the function:
>>> print(pi)
A yummy treat.
/
Re: DISCUSSION FORUM UNIT-2
by Virgil Vega - Monday, 29 June 2020, 9:32 AM
Very insightful explanation and interesting functions are defined. It feels kind of relax and
fun reading through your coding. You also reminded me that we need to convert the value
to string using str() function before we can concatenate them with other string values.
Thanks for reminding and nice piece of work. 10/10
54 words
Really good
Well explanatory
Keep it up
7 words
This was very well done and fun to read. I very much like this explanation of the functions
and this makes it very readable and understandable.
This post helped me learn quite a bit. Also with the value to str() was another reminder I
had forgotten.
46 words
Example 1
def addition(x,y):
print(x+y)
addition(2,4)
"x" and "y" are parameters and they are declared when defining the function
"2" and "4" are arguments and they are passed to the function when the function is called.
Example 2
/
addition(a,b)
Example 3
def func():
x = 8
print(x)
Explanation: any variable declaration or alteration defined within a function or a local scope
cannot affect a variable of another function or a global scope
Example 4
def func(g):
print (g)
g=2
print(g)
Explanation: It shows that the parameters in the function have no effect when reused outside
the function
/
Permalink Show parent
A very lean and neat display of input and output and also concise explanation. By going
through your coding, I learnt that we can define multiple variables using just one coding
line by separating the variables using comma. Thanks. 10/10.
41 words
very well done nice clear explanations on your code and good examples aswell
13 words
Hello,
Your explanation is very insightful, but I find that for example 3 the function called is
incomplete and it would have produced no effect if run. def func(): x = 8
Well done you did great your explanation is great and insightful.
11 words
Hello Abdulbasit,
Very nice examples! You provided your explanations in a very clean and concise way,
which I highly appreciate. Nice job!
22 words
Nice work! I'm not sure if example 4 shows what you're explanation says, but other than
that it's good.
19 words
Input:
#Define function
def valve_type(shape):
ba = 'ball'
pl = 'plug'
bu = 'butterfly'
ch = 'check'
print(shape+ ' valve')
Output:
ball valve
Explanation : Parameter is the code ‘shape’ at function-defining stage which is the name used
inside the function to refer any value passed as argument. The argument in the coding is the
value code ‘ball’ at function-calling stage which this value is assigned to the corresponding
parameter in the function.
Example 2:
Input:
valve_type('ball')
Output:
ball valve
/
Explanation: ball code is a string value is directly used as the argument in calling valve type
argument for the function. Variable pl represents the string value of plug .
Input:
ba = 'ball'
valve_type('2 inch full bore ' +ba)
Output:
Example 3:
Input:
def valve_type1(shape):
bal = 'ball'
valve_shape = shape + ' valve'
print(valve_shape)
print(valve_shape)
Output:
NameError:
def valve_type1(shape1):
print(valve_shape)
shape1 = "Kallang"
print(shape1)
valve_type1('ball')
Output:
Kallang
ball valve
Explanation:
The parameter "shape" is defined in both inside and outside of the function. Outside the
function, a global variable “shape” with a value of "Kallang" is created.
When the valve_type1 function execute,it will not use the global variable's value which is
"Kallang". Instead, it uses the value passed into this parameter via function argument, which is
"ball". Hence, the output for valve_type1('ball') will still be ball valve instead of Kallang.
It works fine to use the function parameter outside of the function as both do not interfere with
each other.
Global variable created by parameter outside of the function will always stored in the python
database and are not deleted. The argument passed to the function parameter during function /
force = 20kN spring force is required.
print(force)
spring_rate()
print(force)
Output:
Explanation:
A similar variable, “force” is defined inside the function and also outside of the function. For this
case, we can assume Python inteprete the coding by following the line sequence as there is no
function calling code. Initially, string values of '20kN spring force is required.' is stored in force
variable. Moving into the function context, another force variable is defined in the function and
does not interfere with the global variable defined outside of the function. This local variable
will only be used within the function context and deleted after the function code terminated.
spring_rate() function still returns the value of force variable in the function without being
replaced by the one defined outside of the function.
print(force) statement will return the value of global force variable which is 20kN instead of
200kN as the local variable has been deleted after the function code terminates.
Reference:
Downey, A. B. (2015). Think Python: How to Think Like a Computer Scientist, Version 2.2. 23:
Green Tea Press.
714 words
/
2 words
Excellent you post is great and insightful also your explanation is good.
12 words
Thanks Mark!
2 words
Also how do you format your posts with your code inside of a grey box?? It looks so clean
and I would like to be able to do that too!
/
In example 1 you define the function and show the output but didn’t show how you called
In order to make use of the local variables that you defined inside of the function you
would need to use them inside of the function or move them outside of the function to
access them.
valve_type()
Output:
ball valve
valve_type(ba) # here you can use any variable you made as an argument
Output:
/
ball valve
Re: DISCUSSION FORUM UNIT-2
by Chuan Wei Chai - Monday, 29 June 2020, 10:40 PM
Hi John,
Thanks for your insightful input!
I used the code below to embed the code under the advance button >> html option in
this text editor as mentioned by Caesar as well.
<pre>#
</pre>
46 words
Awesome! Very clean and concise explanations. You went right to the point, and your
whole post were very agreeable to the eyes. Nice job!
24 words
Shouldn't,
def spring_rate():
force = '20kN spring force is required.'
print(force)
be
def spring_rate():
force = '200kN spring force is required.'
print(force)
?
33 words /
lled function.
print(local_variable) # print local_variable variable.
print_to_screen(variable) # call the function print_to_screen with variable as the a
rgument.
in the above example value_in is a parameter and local _variable is a local variable and they
only exist within the defined function, whereas the variable variable is passed to the function
print_to_screen as an argument.
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.
Output
10
6
12
As we can see the function multiplies by 2, for instance if we pass a string into the function we
will multiply that string by 2 as shown below
print_to_screen('string')
stringstring
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.
/
print(local_variable_inside_function)
NameError: name 'local_variable_inside_function' is not defined
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.
def local_variable_function_2(parameter_inside_function):
local_variable_inside_function = parameter_inside_function
return local_variable_inside_function
local_variable_function_2(2)
print(parameter_inside_function)
If we try and access the parameter from outside of the function, we will receive a name error is
not defined as the parameter is not defined outside of this function.
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.
/
Second the function test_variable_outside_and_in() is called, the variable test_variable inside the
function is assigned 20. Finally, the print function is called with local test_variable as the
argument. Once the function has completed its task all local variables within are destroyed.
20
10
We can see from the outputs, although the variables have the same variable name they are still
unique variables, as variables assigned within functions only exist within that assigned function
and do not exist outside that assigned function.
Reference:
Downey, A. (2015). Think Python: How to think like a computer scientist. Green Tea Press. This
book is licensed under Creative Commons Attribution-NonCommercial 3.0 Unported (CC BY-NC
3.0).
746 words
I can see the effort put into this coding with clear function names and variable names
assigned to ease others to assess the coding. Comments are also given when needed to
explain the code nicely. Reference is used for this piece of work as well. Nice work well
done 10/10.
51 words /
Re: DISCUSSION FORUM UNIT-2
by Abdulbasit Rubeiyya - Sunday, 28 June 2020, 3:01 PM
Great work! Your post was well explained and nicely formatted.
10 words
Hi
It is very difficult to use python interpreter. I don't know how to use python interpreter and the
second thing is the exercise 2.2 is not understandable for me.
Can you please help me
35 words
/
by Tofunmi Adegoke - Sunday, 28 June 2020, 10:43 AM
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 tofunmi(a):
... print(a)
...
Here,def shows that I am defining a function, with the name : tofunmi, followed by a properly
indented body
Call :
>>>tofunmi(3)
3
The parameter is what is in the bracket when creating a function.Here, the parameter is a
The argument is what is required to be inputted when the function is called.Here, it is 3
Example 2
/
Here,the variable is p,which has a value of 5.it is the value that is now evaluated when the
function is called with the variable
good=20*3
>>>tofunmi(good=20*3)
>>>w=20*3
tofunmi(w=20*3)
Here,the expression is w=20*3 .This is true because it contains a variable,w ,value ,20 and 3 and
operator, * which is in accordance to the definition of expression as "combination of values ,
variables andd operators"
-Allen Downey ,Think Python How to Think Like a Computer Scientist 2nd Edition p.g 10.
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.
>>>def example_3(d):
... w=d*2
... print(w)
...
/
g y
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.
>>>name=2
>>>def good_boy(name):
... print ("name")
...
Now let's call the function and see if it will acknowledge "name" used outside:2 or name used in
the function i.e argument
>>>good_boy(50)
50
Explanation:When a parameter has the same name as a variable defined outside, instead of the
function using the variable defined outside, it will only reference the value that was passed to
the parameter. So, parameters will be used over variables of the same name within a function.
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.
Outside variable:
/
>>>f 8
Re: DISCUSSION FORUM UNIT-2
by Abdulbasit Rubeiyya - Sunday, 28 June 2020, 3:03 PM
good explanation and it would have been excellent if you had worked a little on
formating(designing) your work
19 words
Thank you
2 words
you have done well but on the example 2 3rd question you have passed a statement
good=20*3 not the expression 20*3
24 words
1: Define a function that takes an argument. Call the function. Identify what code is the
argument and what code is the parameter.
Solution
>>> def simple_printer(simp):
print(simp)
/
Variable UoP is the argument passed to the simple_printer function to produce University of
the People
>>> a = 50
>>> b = 25
>>> c = ((a * b) + 5) / 4
>>> simple_printer(c + 4)
317.75
Expression (c + 4) is the argument passed to the simple_printer function with results 317.75
3: Create a function with a local variable. Show what happens when you try to use that variable
outside the function. Explain the results.
Solution
>>> def weather_forcast(news):
day_of_the_week = news = 'Sunday Weather Forcast'
print(day_of_the_week)
print('The weather today is 25 degree celsius')
print('its a raining day')
>>> print(day_of_the_week)
Traceback (most recent call last):
File "<pyshell#141>", line 1, in <module>
print(day_of_the_week)
NameError: name 'day_of_the_week' is not defined
/
The error message shows that day of the week is not defined which indicates that
p ( g y)
>>> print(news)
Traceback (most recent call last):
File "<pyshell#144>", line 1, in <module>
print(news)
NameError: name 'news' is not defined
The error message shows that news is not defined which indicates that news is a local
parameter of the function weather_forcast.
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.
Solution
>>> def weather_forcast(news):
day_of_the_week = news = 'Sunday Weather Forcast'
print(day_of_the_week)
print('The weather today is 25 degree celsius')
print('its a raining day')
>>> day_of_the_week = ('Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday')
>>> print(day_of_the_week)
Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday
The Python interpreter gives a result that is independent of the function local variable. In
/
Python only the function caller is required to read the statement in the function and the
Re: DISCUSSION FORUM UNIT-2
by Mathew Ojo - Monday, 29 June 2020, 4:22 AM
Thanks
1 words
The one thing I see is calling the 'weather_forcast()' function instead of print and passing
the 'day_of _the_week' variable as the argument might illustrate your point about local vs
global variables better.
45 words
Hi John,
Example 1: Define a function that takes an argument. Call the function. Identify what code is the
argument and what code is the parameter.
Returns:
Today is
Thursday.
______________________________________________
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:
def today_is(day):
print("Today is ")
print(day)
Expression:
import datetime
todays_date = str(datetime.date.today())
def today_is(day):
print(day)
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.
import datetime
def today_is(day):
todays_date = datetime.date.today() # todays_date is a local variable
print("Today is ")
print(todays_date)
today_is(todays_date)
Returns:
Traceback (most recent call last):
File "/home/today_is.py", line 7, in today_is(todays_date)
NameError: name 'todays_date' is not defined
/
You will get an error stating that the argument in line 7 (in this case “todays date”) is not
p ( p) g pp
Returns:
Traceback (most recent call last):
File "/home/today_is.py", line 4, in print(temp)
NameError: name 'temp' is not defined
When you try to use the parameter of the function outside of the function( in this case trying to
use it in a print statement) you get a NameError. “temp” is not defined because it is not defined
anywhere inside of or outside of the function.
____________________________________
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.
temp = "cold"
def it_is_a(weather):
temp = "hot"
print("It is a " + weather +" day.")
it_is_a(temp)
Returns:
It is a cold day.
I am getting a warning on line 4 that says “local variable temp is assigned to but never used”
Line 1 - python sets the variable temp to “cold”
Line 2 - python defines the “it_is_a” function with one parameter
Line 3 - python sets the variable temp to “hot” inside the function
Line 4 - python knows to print the print expression when the “it_is_a” function is called
Line 5 - python reads the empty line as the end of the function
/
Line 6 python runs the “it is a” function and looks for “temp” to pass as the argument it
Re: DISCUSSION FORUM UNIT-2
by Leul Gebremichaele - Monday, 29 June 2020, 10:27 AM
Excellent explanation, well expressed. I have seen a clear concept of the examples
13 words
hi all good but you need to make sure you have indentations as you code wont run
17 words
Example 1: Define a function that takes an argument. Call the function. Identify what code is the
argument and what code is the parameter.
/
--------------------------------
Input
A=’Bro’
Function_three(A)
Output
Call Bro
------------------------------------------------------------
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.
Input
>>> def founction_five(z1,z2):
... y=z1+z2
... print(y)
>>> print(y)
Output
Traceback (most recent call last):
File "", line 1, in NameError: name 'y' is not defined
The variable y only excites in the function_five function. The interpreter can only read it when
executing the function. NameError shows that the variable needs to be defined outside the
function to be recognized by the Python interpreter.
=========================================
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.
Input
>>> def function_six(a):
/
return 2+a
p
>>> def function_eight(x1,x2):
... x1=15
... x2=15
... y=x1+x2
... print(y)
>>> function_eight(1,2)
Output
30
-----------
Input
>>> x1=15
>>> x2=15
>>> y=x1+x2
>>> print(y)
Ouput
30
--------------
Input
X1
Ouput
15
The variables inside function_eight function does not affect outside similar variables. This is
because these variables are independent of each other. The order reason is because the
interpreter executes code one at a time.
360 words
/
Permalink Show parent
Good job
Well explained, but try using small letter for function name as we've learnt
Good job!
17 words
hi their is no answer to question 4 and you need to format the code better
16 words
Explanation: We are first calling the function using a value as the argument, on the second
execution we are using a variable as the argument, and through the last call we are calling the
function using an expression as the argument.
------------------------------------------------------------------------------------------------------------------------------------------
--------
3) Create a function with a local variable. Show what happens when you
try to use that variable outside the function. Explain the results.
print(cars)
Traceback (most recent call last):
File "<input>", line 1, in <module>
NameError: name 'cars' is not defined
Explanation: The variable cars can only get called from the MyFavoriteCars function. The /
>>> name
Traceback (most recent call last):File "<pyshell#1>", line 1, in <module>
name
NameError: name 'name' is not defined
Explanation: The parameter name outputs an error message “NameError” when used outside
the function, saying that name is not defined.
------------------------------------------------------------------------------------------------------------------------------------------
--------
>>> print(name)
John Doe
Explanation: If the variable defined outside a function has the same name as the local variable
inside a function, the local variable will be displayed and will only return the outside variable
with the “print” command.#1> /
Re: DISCUSSION FORUM UNIT-2
by Cesar Castro - Tuesday, 30 June 2020, 3:55 AM
Exactly, John! I tried to correct it, but it passed 5 minutes, and I couldn't edit the post
anymore. Thank you for your comment!
24 words
Nice work in submitting codes in a clean format, keep up the good work.
14 words
excellent job well thought out and great presentation this is an example people should
look at also the first response i have read for awhile with correct indentations
28 words
Hi Cesar,
Great explanations, good job.
/
6 d
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 converte(fahrenheit):
print (celcius)
converte(90)
32.22222222222222
in my understanding a parameter is the place holder and the argumnet is the value by
itself which is assinged to the parameter
.in my example the parameter is fahreneit and the argument is 90 which I called the convert
function with 90
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.
2 deggree = 90
3 converte(45 + 45) --- expression as an argumnet (45 + 45 ) here the argument will be
evaluated be fore the function is called
4 convert(value = 90) keyword argument also we have a default argument which we can use
when ever
/
-- we will get an error the variable name is not defined because local variable only can be
accessed in side the function that they exist
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.
def user(name):
print(name)
user('leul') leul
print(name)
File "test.py", line 8, in <module>
print(name)
NameError: name 'name' is not defined
The parameter only be executed when the function called with the expected argument, It can
only be executed when the function is called with the parameter
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.
user()
print(name)
local variable
variable out side a function /
by Adrian Davies - Thursday, 2 July 2020, 3:22 AM
your formatting of the code was messed up and made some parts of the post hard to
read, their is some code with indentations and some with out also you need to read some
of your answers as they do not make sense
43 words
Hi Leul,
Thank you for your contribution to the forum, however, I agree with the comment, the
formatting of the code can be better.
24 words
/
an error.
Example 4. If a parameter name is used outside the function it would result in a NameError.
Here the parameter has local scope. Though it contains the same value, which was passed to it,
the parameter name cannot be used outside function.
Example 5. Here the variable 'var' is used for both inside function (local variable) and outside
function (global variable).
According to our reading, the variable inside the function is treated totally different from the
one outside function (Downey, 2015). /
References
Downey, A. (2015). Think Python: How to think like a computer scientist. Green Tea Press.
237 words
hi ll good but you need to read up on functions and what a method is to a function in
python
21 words
Example 1: Define a function that takes an argument. Call the function. Identify what code is the
argument and what code is the parameter.
my_name('Dave')
output:
Hello
I am Dave.
Value
def math(x,y):
print (x + y)
math(5,5)
Output :
10
Variable
line1 = 8
line2 = 8
def math(line1,line2):
x = line1 + line2
print(x)
math(line1,line2)
/
output
language()
print(string1)
output :
I am learning python
Hello, I am Dave!
In python, as we seen from above example variables are local to its defined function , and when
we have variable outside that function and when we call that variable it will output value which
is set outside body of the function, its different when you call that function it will use variable
value inside the function body.
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.
def example(string2):
x="This is test"
print(x)
x="This is test"
example(string2)
print(x)
output:
This is test
/
This is test
y
def math():
x = 10
y = 10
print (x + y)
math()
output :
20
print(x+y)
output :
10
when you call function it used variable value inside its body to output result, while when you
call any variable which has value outside function body even has same name to one inside
function body, it use value defined outside function body to output result.
468 words
Hi Dhaval,
Good effort, I like your explanation as well... /
by Chester Jasper - Monday, 29 June 2020, 10:57 PM
EX.1.
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> def print_hello():
print ("Hello, not really extra fun example, but will get better")
print(5*4**2)
print ("My life is really really fun right now")
print ("Work, study, kids,kids, kids, field, house, work")
>>> print_hello()
Hello, not really extra fun example, but will get better
80
My life is really really fun right now
Work, study, kids,kids, kids, field, house, work
>>>
- Arguments here are expressions like 5*4**2. The Argument can be any kind of arithmetic
expression or the part that tells the function what to do.
Ex.2.
>>> def print_hello():
a=15
b=85
print (a+b/5)
print (a)
print(b)
print ("This is all that's to it")
print("Variables, expressions and statements in a function")
>>> print_hello()
/
32 0
>>> local_variable()
25
>>> print (x)
Traceback (most recent call last):
File "", line 1, in print (x)
NameError: name 'x' is not defined
>>> Variable x was created inside the local_variable function and it will work only when the
function is used. It can’t be used outside the function, it’s not defined globally.
Ex.4.
>>> def argument_ex4():
x=math.pi/83+25
print (x)
>>> argument_ex4()
25.037850513898672
>>> print (x)
Traceback (most recent call last):
File "", line 1, in print (x)
NameError: name 'x' is not defined
>>> Argument here is math.pi/83+25
When you try to use it outside the defined function, happens the same thing as with variables. It
fails because it’s not defined globally.
Ex.5.
>>> x=34
>>> print(x)
/
34
Re: DISCUSSION FORUM UNIT-2
by Adrian Davies - Thursday, 2 July 2020, 3:40 AM
seems ok but its hard to read as you have no indents try formatting your post better there
are some really good examples in posts above
26 words
"Arguments here are expressions like 5*4**2. The Argument can be any kind of arithmetic
expression or the part that tells the function what to do."
I like this simplistic approach to the explanation of the argument and the expressions. This
is very well done in also explaining the mathematics side of it as well. Very concise, and it's
taught me about globally defined variables. Great job!
68 words
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 todo_1(first):
second = len(first)
if second > 12: /
( )
if second > 12:
print("Wow big word" * 5)
else:
print(second)
todo_1("multidimensional" * 3)
the variable second is a value of the length of the variable which is called in the parameters in
characters.
print(“Wow big word” * 5 is an expression. The if statement is the argument.
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.
def loco():
x = 45
print(x)
When i call x outside of the function in a print statement, it returns nothing because x was
defined inside the function and the only way to access that variable is to call the function.
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.
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
g , ,
a different value, it acts as if it is a new variable not related to the function, because it becomes
a global value.
342 words
Dear Roger!
First please keep code formatting because it is very important for python.
Speaking about Example1: as far as I see "first" is an argument and "cook" is a parameter.
Speaking about example 2: you need call function you defined in example 1 three times -
with the simple argument, with an expression and with a variable as an argument. As far
as I see you call it once (with the expression).
Speaking about example 3: python should return an error because X is not defined in the
global scope. I can say the same for example 4.
Thank you for your effort. I can understand how hard programming can be.
118 words
Good example of Question #2, call your function with three dif arguments.
12 words
#Example 1:
print_three('cat')
#Example 2:
print (rectangular(3))
/
p ( , ( g ))
#Example 3:
cool()
Example 5:
If a variable with the same name is defined inside the scope of function as well then it will print
the value given inside the function only and not the global value.
152 words
Great work Sahar I really like how you used comments to explain each and everyone of the
questions. /
Re: DISCUSSION FORUM UNIT-2
by Amanda Feliu - Wednesday, 1 July 2020, 8:13 AM
Example 1: Define a function that takes an argument. Call the function. Identify what
code is the argument and what code is the parameter.
... print(a)
... print(a)
...
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.
>>> florida_hurricanes(5)
5
This is a value.
/
This is a variable.
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.
... a = 20
... b = 15
... print(a)
...
>>> bread()
20
>>> print(b)
Because b a local variable that is a part of the function, b will not be printed out.
>>> x = 9
/
... return soil+water
...
>>> plants(6,2)
>>> soil+2
Because 'soil' is not global, and it is local inside of the function, it is not defined. Only in the
function will it work.
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.
...
/
>>> soil 20
Permalink Show parent
Nice work Amanda. Codes are understandable and clear as well good job
12 words
Hi Amanda,
I really like the way you have used the examples. Its very easy to understand than regular
way. Its helpful for begineer like me to understand. Good Job.
30 words
Very organized and simplified codes. It really shows that you have these concepts down. I'll
be sure to take some notes for myself, great post.
25 words
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 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 my_girls(girl1, girl2):
girl1 = 'Lisa'
girl2 = 'Tina'
family = girl1 + girl2
print(family)
/
Great work Birikty Though I was able to get the last one and had some confusion on 4
Re: DISCUSSION FORUM UNIT-2
by Owen James - Wednesday, 1 July 2020, 4:00 PM
1.)
>>> def argue_more(thing1):
... print(thing1 * 3)
...
>>> argue_more(2)
6
>>>
The parameter in this line of code is ‘thing1’ and the argument would be the ‘2’ that I enter in
place of the parameter.
2.)
>>> argue_more(4/2)
4.0
The argument ‘4/2’ was an expression.
>>> argue_more(7)
14
The argument was a value.
>>> fire = 9
>>> argue_more(fire)
18 /
This results in an error due to the expression not being able to access the past function’s local
variable.
4.)
>>> def argue_more(thing1):
... print(thing1)
...
>>>argue_more(1)
1
>>> thing1
Traceback (most recent call last):
File "", line 1, in
NameError: name 'thing1' is not defined
This also results in an error code due to the variable being local to the function.
5.)
>>> def argue_more(thing1):
... x = 2
... print(x - thing1)
...
>>> argue_more(4)
-2
>>> x+10
Traceback (most recent call last):
File "", line 1, in
NameError: name 'x' is not defined
>>> x = 10
>>> x + 7
/
17
Dear Owen!
For 5 one I can recommend you to create the external variable before calling the function -
it may be more visible that the internal variable cannot be changed from the outside even
if the names are the same. Speaking of others it's good work - everything is correct and
work as it should be.
55 words
I understand all except your number 5. Good work all the same.
12 words
Q: Example 1: Define a function that takes an argument. Call the function. Identify what code is
the argument and what code is the parameter.
/
Passing value itself:
>>> myTest("string")
function arg value: string
function type: <class 'str'>
>>> a = "1"
>>> myTest(a)
function arg value: 1
function type: <class 'str'>
Passing expression:
>>> myTest(1+2)
function arg value: 3
function type: <class 'int'>
Q: 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.
The argument of a function looks like a variable inside of the function. This variable creates on
function call so it is not accessible outside of function because of isolation as we saw it before.
Q: 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.
As we saw it before this is how isolation works. All the variables created locally lives only local
scope. If we re-define variable on the local level - this re-definition will not affect global variable
/
at all
Re: DISCUSSION FORUM UNIT-2
by Deborah Adewumi - Thursday, 2 July 2020, 11:06 AM
A comprehensive explanation. Wonderful examples. I wish I had done mine this way. Nice
job.
15 words
Input Output
>>>mathEasy()
Example 1: Define a function that takes an argument. Call the function. Identify what code is the
argument and what code is the parameter
parameters are the name written within the function definition and are fixed.
Arguments are the values passed in when the function is called, however they can change.
>>>mathEasy(10)
value
Input Output
>>>mathEasy()
Expression
Input Output
>>>mathEasy()
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.
Input Output
>>>print(y) >>>
281 words
Dear Faiza!
For example 2 you should pass three types of argument to the function, but you have
redefined the function and use argument for only first part of the example. You also did
not show variable definition there. For example 4 you did not explained the code
behaviour. And you did not example 5 as far as I see it.
Even so it's a good try, hope you'll make it better next time.
/
74 words
by Deborah Adewumi - Thursday, 2 July 2020, 11:04 AM
Nice use of the table, makes your work neat and comprehensive.
11 words
Example 1: Define a function that takes an argument. Call the function. Identify what
code is the argument and what code is the parameter.
A>>> type(100)
<class 'int'>
Here type is a function and the expression in the parentheses 100 is the argument.
>>> my_function()
Here I defined my_function to This is my first function; so when I called the function I get the
return value- This is my function
/
Here my_song is the argument and intromusic is 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.
This was my function in example 1 which I have used with different arguments
print(intromusic)
print(intromusic)
print(intromusic)
>>> my_song('lastmusic')
lastmusic
lastmusic
lastmusic
>>> firstmusic=10
>>> lastmusic=20
>>> my_song('firstmusic+lastmusic')
/
firstmusic+lastmusic
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.
def apple():
phone='iphone'
return
>>> apple()
phone= iphone
>>> phone
phone
phone is local variable for applefunction So when I try to use that variable outside of the
function its error
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
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.
>>> apple=10
apple=20
>>> food()
local apple: 20
global apple: 10
>>>
We used apple for both global and local variable and we get different results.
/
Permalink Show parent
Good example for Question #4. Don't forget to include a space so its more humanly
readable: i.e. print("hi " name + "" + message)
22 words
I could be wrong, I am new to this as well, but for example 1 wouldn't the argument be
'last music'? I think you got the parameter part right though. Someone please correct me if
I am mistaken and good work on your assignment.
44 words
1.
def multiplyBy9(num):
val = num * 9
print(val)
multiplyBy9(55)
/
495
py y ( )
x = -7
multiplyBy9(x + 2)-45
3. def fnn():
x = "local"
fnn()
print(y)
4. def plus(tot):
tot = tu
print (tot)
tot
When a parameter has the same name as a variable defined outside, the function will only
reference the value that was passed to the parameter.
5.
/
If a variable with the same name is defined inside the scope of function as well then it will print