You are on page 1of 74

Hi All,

Here is the scenario for our second discussion forum:

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

Re: DISCUSSION FORUM UNIT-2


by Virgil Vega - Friday, 26 June 2020, 5:51 AM

I defined a function named echo:

def echo(thingToEcho):
thingToEcho = str(thingToEcho) #Converts argument into string
print(thingToEcho + "..." + thingToEcho) #Echos argument and returns the res
ult

I called the function:

>>> 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.

In this example the argument is a variable (greeting):

>>> greeting = "Hello there!"


>>> echo(greeting)
Hello there!...Hello there!

In this example the argument is the value, "Hee hee hee": /


pi = 3.141592653589793
print("Pi is " + str(pi) + ".")

>>> 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.

The same happens with a parameter.

Here's the function I used:

>>> def sayHiTo(name):


print("Hello, " + name + "!")

>>> sayHiTo("Lilly")
Hello, Lilly!

Here's where I tried to call the parameter:

/
>>> 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.

Consider these lines of code:

>>> pi = "A yummy treat."


>>> def whatIsPi():
pi = 3.141592653589793
print(pi)

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

You are correct! That was the echo() function.


8 words

Permalink Show parent

Re: DISCUSSION FORUM UNIT-2


by Chuan Wei Chai - Saturday, 27 June 2020, 9:29 PM

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

Permalink Show parent

Re: DISCUSSION FORUM UNIT-2


by Tofunmi Adegoke - Sunday, 28 June 2020, 10:57 AM

Really good
Well explanatory
Keep it up
7 words

Permalink Show parent

Re: DISCUSSION FORUM UNIT-2 /


You did great because you helped me to learn more from your post and the way you use it
to describe the code is fun and great.
27 words

Permalink Show parent

Re: DISCUSSION FORUM UNIT-2


by Amanda Feliu - Thursday, 2 July 2020, 8:18 AM

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

Permalink Show parent

Re: DISCUSSION FORUM UNIT-2


by Abdulbasit Rubeiyya - Saturday, 27 June 2020, 3:04 PM

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)

The output returned is 8

Now passing arguments


i - using an expression as arguments
addition(a+2,2)

The output returned is 9

Example 3

def func():

x = 8

print(x)

OUTPUT: NameError: name 'x' is not defined

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)

The output returned is 2

Explanation: It shows that the parameters in the function have no effect when reused outside
the function
/
Permalink Show parent

Re: DISCUSSION FORUM UNIT-2


by Chuan Wei Chai - Saturday, 27 June 2020, 9:32 PM

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

Permalink Show parent

Re: DISCUSSION FORUM UNIT-2


by Adrian Davies - Sunday, 28 June 2020, 4:21 PM

very well done nice clear explanations on your code and good examples aswell
13 words

Permalink Show parent

Re: DISCUSSION FORUM UNIT-2


by Mathew Ojo - Monday, 29 June 2020, 5:50 AM

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

Aside from that, the rest looks great.


38 words
/
by Mark Tanyous Monday, 29 June 2020, 6:04 AM

Well done you did great your explanation is great and insightful.
11 words

Permalink Show parent

Re: DISCUSSION FORUM UNIT-2


by Cesar Castro - Monday, 29 June 2020, 9:18 AM

Hello Abdulbasit,

Very nice examples! You provided your explanations in a very clean and concise way,
which I highly appreciate. Nice job!
22 words

Permalink Show parent

Re: DISCUSSION FORUM UNIT-2


by Virgil Vega - Tuesday, 30 June 2020, 12:43 AM

Nice work! I'm not sure if example 4 shows what you're explanation says, but other than
that it's good.
19 words

Permalink Show parent

Re: DISCUSSION FORUM UNIT-2


by Dhaval Patel - Tuesday, 30 June 2020, 6:40 AM

neat and clean representation of code, keep up the good work.


11 words
/
Example 1:

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:

(i) Call function using a value

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 .

(iii) Call function using an expression

Input:

ba = 'ball'
valve_type('2 inch full bore ' +ba)

Output:

2 inch full bore ball valve

Explanation : An expression is a combination of values, variables, and operators (Downey,


2015).  '2 inch full bore' + ba is an expression used in the valve_type function.

Example 3:

Input:

def valve_type1(shape):
bal = 'ball'
valve_shape = shape + ' valve'
print(valve_shape)

print(valve_shape)

Output:

NameError:

name 'valve_shape' is not defined


/
E l ti
Input:

def valve_type1(shape1):

valve_shape = shape1 + ' valve'

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:

200kN spring force is required.


20kN spring force is required.

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

Permalink Show parent

Re: DISCUSSION FORUM UNIT-2


by Mark Tanyous - Monday, 29 June 2020, 6:06 AM

Excellent you post is great and insightful also your explanation is good.
12 words

Permalink Show parent

Re: DISCUSSION FORUM UNIT-2


by Chuan Wei Chai - Monday, 29 June 2020, 10:32 PM

Thanks Mark!
2 words

Permalink Show parent

Re: DISCUSSION FORUM UNIT-2


by John Kovarik - Monday, 29 June 2020, 8:48 AM

Good job Chuan!


Your definitions of terms are correct, I just noticed that these examples I tried might be
simplified or more functional by moving the variables out of the function.

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.

Example of using (local) variables inside of function:


Input:
def valve_type():
    ba = 'ball'
    pl = 'plug'
    bu = 'butterfly'
    ch = 'check'
    print(ba+ ' valve')        # using the variable inside of function here

valve_type()
Output:
ball valve

Example using (global) variables outside of the function:


Input:
ba = 'ball'
pl = 'plug'
bu = 'butterfly'
ch = 'check'
def valve_type(shape):
    print(shape+ ' 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>

Hope it helps. Do let me know if u face any issue :)

46 words

Permalink Show parent

Re: DISCUSSION FORUM UNIT-2


by Chuan Wei Chai - Monday, 29 June 2020, 10:48 PM

Should be Cesar instead of Caesar. Sorry for the typo my friend. :)


12 words
/
Thanks Chuan & Cesar!
I'm excited to try this out next time
12 words

Permalink Show parent

Re: DISCUSSION FORUM UNIT-2


by Cesar Castro - Monday, 29 June 2020, 9:42 AM

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

Permalink Show parent

Re: DISCUSSION FORUM UNIT-2


by Virgil Vega - Tuesday, 30 June 2020, 12:54 AM

Awesome! I think the last example has a typo though.

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.

print_to_screen(5) # Call function with 5 as the argument.


i_am_a_variable = 3 # Assign i_am_a_variable the value of 3
print_to_screen(i_am_a_variable) # Call function with i_am_a_variable as the argumen
t.
print_to_screen(2*3) # Call the function with expression 2*3 as the argument

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

As expected we have an undefined variable local_variable_inside_function, as we have yet not


defined the variable outside of the function, furthermore once the function has completed the
local variables assigned within that function are destroyed.

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)

When we call the function local_variable_function_2() and print parameter_inside_function we


get the following error

Traceback (most recent call last):


File "C:/untitled1/Disussion Forum Unit 2.py", line 67, in <module>
print(parameter_inside_function)
NameError: name 'parameter_inside_function' is not defined

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.

Third we call the print function with test_variable as the argument.

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

Permalink Show parent

Re: DISCUSSION FORUM UNIT-2


by Chuan Wei Chai - Saturday, 27 June 2020, 9:36 PM

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

almost perfect, neat arrangement and clear explanation of your work


10 words

Permalink Show parent

Re: DISCUSSION FORUM UNIT-2


by Virgil Vega - Tuesday, 30 June 2020, 12:56 AM

Great work! Your post was well explained and nicely formatted.
10 words

Permalink Show parent

Re: DISCUSSION FORUM UNIT-2


by Dhaval Patel - Tuesday, 30 June 2020, 6:38 AM

nice and clean representation of codes, keep up the good work.


11 words

Permalink Show parent

Re: DISCUSSION FORUM UNIT-2


by Tahira Shareef - Sunday, 28 June 2020, 2:08 AM

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

I called the function tofunmi (3)


And the output is exactly as instructed in the body

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

III)calling the function with an expression

good=20*3
>>>tofunmi(good=20*3)
>>>w=20*3
tofunmi(w=20*3)

Output:TypeError: tofunmi() got an unexpected keyword argument 'w'

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

Permalink Show parent

Re: DISCUSSION FORUM UNIT-2


by Tofunmi Adegoke - Sunday, 28 June 2020, 7:01 PM

Thank you
2 words

Permalink Show parent

Re: DISCUSSION FORUM UNIT-2


by Adrian Davies - Sunday, 28 June 2020, 4:33 PM

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

Permalink Show parent

Re: DISCUSSION FORUM UNIT-2


by Tofunmi Adegoke - Sunday, 28 June 2020, 7:01 PM

Thank you very much for nice words


/
7 words
Permalink Show parent

Re: DISCUSSION FORUM UNIT-2


by Tofunmi Adegoke - Monday, 29 June 2020, 5:20 AM

Just observed that


Thanks !
4 words

Permalink Show parent

Re: DISCUSSION FORUM UNIT-2


by Mathew Ojo - Monday, 29 June 2020, 2:32 AM

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)

>>> simple_printer('Unit 2, Variables, Expression, Statements, & Functions')


Unit 2, Variables, Expression, Statements, & Functions

Argument code is 'Unit 2, Variables, Expression, Statements, & Functions'

Parameter code is (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

Permalink Show parent

Re: DISCUSSION FORUM UNIT-2


by John Kovarik - Monday, 29 June 2020, 9:11 AM

Very nice Mathew!


Great concise explanations, and a clean post.

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

Permalink Show parent

Re: DISCUSSION FORUM UNIT-2


by Mathew Ojo - Wednesday, 1 July 2020, 7:46 PM

Hi John,

Thanks for your feedback.


I am new to programming certainly as time goes on I will get to understand the types
of variables we have.
27 words /
13 words

Permalink Show parent

Re: DISCUSSION FORUM UNIT-2


by John Kovarik - Monday, 29 June 2020, 4:17 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 today_is(day):        # (day) is the parameter


print("Today is ")
print(day)

today_is("Thursday.")        # (“Thursday”) is the argument

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)

today_is("Thursday.")          # “Thursday” is a value /


y
2020-06-26

Expression:
import datetime
todays_date = str(datetime.date.today())
def today_is(day):
print(day)

today_is("Today is " + todays_date + ".")             # "Today is " + todays_date + "." is an


expression
Returns:
Today is 2020-06-26.
______________________________________________

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

Permalink Show parent

Re: DISCUSSION FORUM UNIT-2


by Adrian Davies - Thursday, 2 July 2020, 3:07 AM

hi all good but you need to make sure you have indentations as you code wont run
17 words

Permalink Show parent

Re: DISCUSSION FORUM UNIT-2


by Birikty Hagos - Thursday, 2 July 2020, 10:53 AM

Neat and nicely done.


Great Job
6 words

Permalink Show parent

Re: DISCUSSION FORUM UNIT-2


by Mark Tanyous - Monday, 29 June 2020, 5:51 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.
/
--------------------------------

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

/
Permalink Show parent

Re: DISCUSSION FORUM UNIT-2


by Tofunmi Adegoke - Tuesday, 30 June 2020, 5:00 AM

Good job
Well explained, but try using small letter for function name as we've learnt

Good job!
17 words

Permalink Show parent

Re: DISCUSSION FORUM UNIT-2


by Adrian Davies - Thursday, 2 July 2020, 3:13 AM

hi their is no answer to question 4 and you need to format the code better
16 words

Permalink Show parent

Re: DISCUSSION FORUM UNIT-2


by Cesar Castro - Monday, 29 June 2020, 8:59 AM

1) Define a function that takes an argument. Call the function. Identify


what code is the argument and what code is the parameter.

def function1(number): #number is the parameter


value = number * 2
print(value)

function1(35) # 35 is the argument /


function1(5) #Calling the function using a value as the argument
10

y = 26 #Calling the function using a variable as the argument


function1(x)
52

y = -4 #Calling the function using an expression as the argument


function1(y + 5)
1

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.

def MyFavoriteCars(porshe, lamborghini):


cars = porshe + lamborghini
print(cars)

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.
------------------------------------------------------------------------------------------------------------------------------------------
--------

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 ofeach variable as the program runs.

>>> def full_name():


name = "Cesar Castro"
print(name)

>>> name = "John Doe"


>>> full_name()
Cesar Castro

>>> 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

Permalink Show parent

Re: DISCUSSION FORUM UNIT-2


by Dhaval Patel - Tuesday, 30 June 2020, 6:23 AM

Nice work in submitting codes in a clean format, keep up the good work.
14 words

Permalink Show parent

Re: DISCUSSION FORUM UNIT-2


by Adrian Davies - Thursday, 2 July 2020, 3:15 AM

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

Permalink Show parent

Re: DISCUSSION FORUM UNIT-2


by Henry Dakat - Thursday, 2 July 2020, 8:27 AM

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):

   celcius = (fahrenheit -32)/1.8

   print (celcius)

  converte(90)
32.22222222222222

This convert function takes argument assigned to a parameter fahrenite

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. 

1 converte(90) value as an argumnet 

2  deggree = 90

      converte(deggree) -- i assinged 90 to a variable deggree which will become the parameter


for the argumnet 

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.

name = 'variable outside a function '


def user():
name= 'local variable'
print(name)
return

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

Permalink Show parent

Re: DISCUSSION FORUM UNIT-2


by Henry Dakat - Thursday, 2 July 2020, 8:25 AM

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

Permalink Show parent

Re: DISCUSSION FORUM UNIT-2


by Henry Dakat - Monday, 29 June 2020, 1:50 PM

Starting with the first example.

A parameter is a variable in a method definition, according to the reading materials it is a name


used inside a function to refer to the value passed as an argument (Downey, 2015).

/
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

CS D.F. Unit 2.docx

Permalink Show parent

Re: DISCUSSION FORUM UNIT-2


by Adrian Davies - Thursday, 2 July 2020, 3:29 AM

hi ll good but you need to read up on functions and what a method is to a function in
python
21 words

Permalink Show parent

Re: DISCUSSION FORUM UNIT-2


by Dhaval Patel - Monday, 29 June 2020, 10:36 PM

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 my_name(name):


print("Hello ")
/
print("I am " + str(name) + " ")
p
def my_name(name):
print("Hello ")
print("I am " + str(name) + ".")

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

Permalink Show parent

Re: DISCUSSION FORUM UNIT-2


by Henry Dakat - Thursday, 2 July 2020, 8:21 AM

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

Permalink Show parent

Re: DISCUSSION FORUM UNIT-2


by Amanda Feliu - Thursday, 2 July 2020, 8:24 AM

"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

Permalink Show parent

Re: DISCUSSION FORUM UNIT-2


by Roger Hoffman - Tuesday, 30 June 2020, 5:16 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 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.

See the results above.

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

Permalink Show parent

Re: DISCUSSION FORUM UNIT-2


by Paul Rudnitskiy - Thursday, 2 July 2020, 3:51 AM

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.

Speaking about example 5 it is correct.

Thank you for your effort. I can understand how hard programming can be.
118 words

Permalink Show parent

Re: DISCUSSION FORUM UNIT-2 /


Re: DISCUSSION FORUM UNIT-2
by Roger Hoffman - Thursday, 2 July 2020, 9:29 AM

Good example of Question #2, call your function with three dif arguments.
12 words

Permalink Show parent

Re: DISCUSSION FORUM UNIT-2


by Sahar Alhammadi - Wednesday, 1 July 2020, 5:59 AM

#Example 1:

def print_three (cat):   # (cat)  an argument     


  print (cat)                  # (cat) here cat is a parameter
  print (cat)
  print (cat)             

print_three('cat')

#Example 2:

def rectangular (s):     # a value


  print (s*s)

print (rectangular(3))

/
p ( , ( g ))

#Example 3:

def cool():              # local variable inside the function


 s = "local"
 print (s)

cool()

def cool():              # run to a nameerror: name 's' is not defined 


  y = "local"          # because we are trying to access a local variable 's'
                             # in a global scope whereas it only works inside cool()
cool ()
print (s)

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

Permalink Show parent

Re: DISCUSSION FORUM UNIT-2


by Faiza Sheikh - Thursday, 2 July 2020, 1:11 AM

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.

>>> def florida_hurricanes(a):

...     print(a)

...     print(a)

...

>>> florida_hurricanes('Get the toilet paper!')

Get the toilet paper!

Get the toilet paper!

The argument is ('Get the toilet paper!'), the parameter is (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)

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.

>>> def bread():

...     a = 20

...     b = 15

...     print(a)

...

>>> bread()

20

>>> print(b)

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

NameError: name 'b' is not defined

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

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

NameError: name 'soil' is not defined

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.

>>> def plants(soil,water):

...     return soil+water

...

/
>>> soil 20
Permalink Show parent

Re: DISCUSSION FORUM UNIT-2


by Faiza Sheikh - Thursday, 2 July 2020, 1:09 AM

Nice work Amanda. Codes are understandable and clear as well good job
12 words

Permalink Show parent

Re: DISCUSSION FORUM UNIT-2


by Preyesh Hirachan - Thursday, 2 July 2020, 7:06 AM

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

Permalink Show parent

Re: DISCUSSION FORUM UNIT-2


by Owen James - Thursday, 2 July 2020, 9:48 AM

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

Permalink Show parent

Re: DISCUSSION FORUM UNIT-2 /


100
The argument multiplyby4 (25) is 25

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.

• Call the function with the argument is a value


>>> multiplyby4(-12)
-48
The argument is -48
• Call the function with the an argument is variable
>>> x=4.8
>>> multiplyby4(x)
19.2
The argument is x equal to 4.8
• Call the function with the argument is variable that is an expresion
>>> y=-12
>>> multiplyby4(y+3)-15
-36
The argument is (y+3) while y is equal to -12

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 my_function(sister1, sister2, sister3):


print("My fovorite sister is" + sister[4])
my_function("Tina", "Lisa", "Noami")

>>> print ("my foverite sister is" + sister [4])


/
File "", line 1, in print(x)
NameError: name 'x' 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.
>>> def my_girls(girl1, girl2):
girl1 = 'Lisa'
girl2 = 'Tina'
family = girl1 + girl2
print(family)

>>> my_girls (1,2)


Lisa Tina

>>> girl1 = 'Lisa'


>>> girl2 = 'Tina'
>>> family = girl1 + girl2
>>> print (family)
Lisa Tina
>>> girl1
'Lisa'
364 words

Permalink Show parent

Re: DISCUSSION FORUM UNIT-2


by Faiza Sheikh - Thursday, 2 July 2020, 1:05 AM

/
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

Permalink Show parent

Re: DISCUSSION FORUM UNIT-2


by Owen James - Thursday, 2 July 2020, 9:27 AM

Ah, that makes sense. Thank you for your input.


9 words

Permalink Show parent

Re: DISCUSSION FORUM UNIT-2


by Deborah Adewumi - Thursday, 2 July 2020, 11:08 AM

I understand all except your number 5. Good work all the same.
12 words

Permalink Show parent

Re: DISCUSSION FORUM UNIT-2


by Paul Rudnitskiy - Wednesday, 1 July 2020, 7:16 PM

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'>

Passing variable with value:

>>> 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.

>>> def myTestVar(input):


...     inVar = 2
...     print ("result is:", (input + inVar))
...
>>> myTestVar(3)
result is: 5
/
>>> def myTestUniqVar(mtArg):
...     print("function parameter is", mtArg)
...
>>> myTestUniqVar(mtArg="test")
function parameter is test
>>> print("outside value of mtArg is", mtArg)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'mtArg' is not defined

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.

>>> testVar = "outside"


>>> def myTestVarConflict():
...     testVar = "inside"
...     print("test var:", testVar)
...
>>> myTestVarConflict()
test var: inside
>>> print("ext var value:", testVar)
ext var value: outside

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

Permalink Show parent

Re: DISCUSSION FORUM UNIT-2


by Faiza Sheikh - Wednesday, 1 July 2020, 11:51 PM

Input Output

>>>def mathEasy(): >>> hey bud

             print ("hey bud")

>>>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.

Example of parameter def location(park) #park is the parameter


/
              return a + 10  

>>>mathEasy(10)

value

Input Output

>>>def mathEasy(): >>>10

              print ("10")

   

>>>mathEasy()

Expression

Input Output

>>>def mathEasy(): >>>hello how are you?

              print ("hello how are you?")

   

>>>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

>>>def rainbow(red): Traceback (most recent call last):

    y = 'it is yellow'   File "<string>", line 4, in <module>

    NameError: name 'y' is not defined

>>>print(y)    >>> 

281 words

Permalink Show parent

Re: DISCUSSION FORUM UNIT-2


by Paul Rudnitskiy - Thursday, 2 July 2020, 3:32 AM

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

Permalink Show parent

Re: DISCUSSION FORUM UNIT-2


by Preyesh Hirachan - Thursday, 2 July 2020, 6:08 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.

A>>> type(100)

<class 'int'>

Here type is a function and the expression in the parentheses 100 is the argument.

B>>> def my_function():

           print("This is my first function")

>>> my_function()

This is my first 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 

>>> def my_song(intromusic):

           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'

           print("phone= ", phone)

           return

>>> apple()

phone=  iphone

>>> phone

Traceback (most recent call last):

 File "<pyshell#12>", line 1, in <module>

   phone

NameError: name 'phone' is not defined

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

>>> def food():

           apple=20

           print("local apple:", apple)

           

>>> food()

local apple: 20

>>> print('global apple:", apple)

     

SyntaxError: EOL while scanning string literal

>>> print ("global apple:", apple)

global apple: 10

>>> 

We used apple for both global and local variable and we get different results.

/
Permalink Show parent

Re: DISCUSSION FORUM UNIT-2


by Roger Hoffman - Thursday, 2 July 2020, 9:27 AM

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

Permalink Show parent

Re: DISCUSSION FORUM UNIT-2


by Owen James - Thursday, 2 July 2020, 9:36 AM

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

Permalink Show parent

Re: DISCUSSION FORUM UNIT-2


by Deborah Adewumi - Thursday, 2 July 2020, 10:41 AM

1.
def multiplyBy9(num):
val = num * 9
print(val)

multiplyBy9(55)
/
495
py y ( )

The function with the argument that is an expression

x = -7

multiplyBy9(x + 2)-45

the argument is (x + 2) while x is equal to -9

3. def fnn():
x = "local"
fnn()
print(y)

Output: NameError: name 'y' is not defined.


It shows an error because I tried to access a local variable in a global scope. Such is not
accepted in python.

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

You might also like