def manipulate_data(type,data):
if type=='list':
list=[1,4,9,16,25]:
return list.reverse
elif type=='set':
set={"a", "b", "c", "d", "e"}:
item.add("ANDELA"):
item.add("TIA"):
item.add("AFRICA"):
return set.result
elif type=='dictionary':
dictionary={"bread": 14, "grapes": 30, "biscuit": 8, "sugar": 16}:
return dictionary.keys
the FUNCTION talk
def myFunc( param1, param2):
# Begin of =myFunc= FUNCTION-BODY
# this =myFunc= function- has 2 PARAMETERS param1 and param2
# param1 and param2 PARAMETERS are used
# as -local- VARIABLES throughout the =myFunc= FUNCTION-BODY
print( param1 + " and " + param2 )
#End of =myFunc= FUNCTION-BODY
If you want to call/execute the myFunc function
you will have to add a pair of parentheses to myFunc
like
myFunc()
As the myFunc function was defined
as having 2 parameters
you have to provide 2 arguments
in our case 2 string VALUES “Alena” and “Lauren”
like
myFunc(“Alena”,“Lauren”)
some quotes from the outer-world:
argument is the value/variable/reference being passed in,
parameter is the receiving variable used within the function/block
OR
"parameters" are called “formal parameters”,
while “arguments” are called “actual parameters”.
++++ function with 1 parameter using return-statement
def myFunction( param1 ):
# //Begin of =myFunction= FUNCTION-BODY
# //=myFunction= function has 1 PARAMETER param1
# //this param1 PARAMETER is used as a -local- VARIABLE
# //throughout the =myFunction= FUNCTION-BODY
return param1;
# //End of FUNCTION-BODY
You have defined a myFunction function
which takes 1 parameter param1
this param1 parameter is used
as a variable throughout the =myFunction= FUNCTION-BODY.
If you want to call/execute this myFunction function
and this myFunction function was defined
as having 1 parameter param1
you will have to provide 1 argument
in our case a “number VALUE” 4
myFunction( 4 )
some quotes from the outer-world:
argument is the value/variable/reference being passed in,
parameter is the receiving variable used within the function/block
OR
"parameters" are called “formal parameters”,
while “arguments” are called “actual parameters”.
============================================
As you are using the return-statement in your myFunction function
you will only get a return-value no-display.
You can however capture this return-value in a variable
and then use the print-method to do a display.
theResult = myFunction( 4 )
print theResult
OR directly
print myFunction( 4 )
Thank you for your reply i ran the code below again and still having error.
def manipulate_data(string,data)
def manipulate_data(list,[1,4,9,16,25]
if string=='list':
return[25,16,9,4,1]
elif string=='set':
add("ANDELA+"TIA"+"AFRICA")
return("ANDELA,"TIA","AFRICA")
elif string=='dictionary':
return{1:'d',2:'i',3:'c',4:'t',5:'i',6:'o',7:'n',8:'a',9:'r',10:'y'}
else:
raise value error
My question is this “How do i make a function behaves differently”?
After a google search
python determine argument of function site:stackoverflow.com
and reading
= http://stackoverflow.com/questions/2225038/determine-the-type-of-an-object
( also check right-hand RELATED-Link column )
= http://stackoverflow.com/questions/847936/how-can-i-find-the-number-of-arguments-of-a-python-function?lq=1
def a_function(parameter):
#determine type of parameter
if isinstance(parameter,list):
print "parameter is a =list="
elif isinstance(parameter,dict):
print "parameter is a =dict="
elif isinstance(parameter,str):
print "parameter is a =string="
else:
print "what kind of parameter is this ???" + str(type(parameter))
#call the function with an ARGUMENT
a_function([])
a_function({})
a_function('string')
a_function(12)
a_function(1.23)
###=================================================
= http://stackoverflow.com/questions/19985145/how-do-i-create-a-python-set-with-only-one-element
!!= http://stackoverflow.com/questions/1952464/in-python-how-do-i-determine-if-an-object-is-iterable
Thank you for your swift response,i think im getting somewhere now,here is my code below.
def manipulate_data(string,data):
#accepts string and manipulate data
if string=='list':
return"list.reverse"
elif string=='set':
set.add("ANDELA")
set.add("TIA")
set.add("AFRICA")
return set.result
elif string=='dictionary':
return dictionary.keys
#call the function with an ARGUMENT
manipulate_data(list,[25,16,9,4,1])
manipulate_data(set,{"ANDELA","TIA","AFRICA"})
manipulate_data(dictionary,{'apples': 23, 'oranges': 15, 'mangoes': 3, 'grapes': 45})
But im getting this error after running it,
“Traceback (most recent call last):
File “prog.py”, line 16, in
NameError: name ‘dictionary’ is not defined”
Im stuck again ,dont know what that means,
And here is the question below,
Create a function manipulate_data that does the following,Accepts as the first parameter a string specifying the data structure to be used “list”, “set” or "dictionary,Accepts as the second parameter the data to be manipulated based on the data structure specified e.g [1, 4, 9, 16, 25] for a list data structure.
*Based off the first parameter
return the reverse of a list or add items "ANDELA"
, "TIA"
and "AFRICA"
to the set and return the resulting set
- return the keys of a dictionary.
@bodeayeni,
The Instructions:
Create a function manipulate_data that does the following,
-
Accepts as the first parameter a string
specifying the data structure to be used “list”, “set” or “dictionary”, -
Accepts as the second parameter the data to be manipulated
-
based on the data structure specified as first-parameter
e.g [1, 4, 9, 16, 25] for a list data structure.
*Based on the first parameter- return the reverse of a list
or - add items “ANDELA”, “TIA” and “AFRICA” to the set and return the resulting set
or - return the keys of a dictionary
def manipulate_data(string,data):
#return type(data)
print data
#accepts string and manipulate data
if string==‘list’:
print type(data)
print list(reversed(data))
return list(reversed(data))
elif string==‘set’:
data.add(“tia”)
#set.add(“TIA”)
#set.add(“AFRICA”)
return data
elif string==‘dictionary’:
‘’’
return type(data)
‘’’
#return type(data)
return data.keys()
print “i am dict”#call the function with an ARGUMENT
print(manipulate_data(‘list’,[6,25,16,9,4,1]))
print(manipulate_data(‘set’,{“ANDELA”,“TIA”,“AFRICA”}))
print(manipulate_data(‘dictionary’,{‘apples’: 23, ‘oranges’: 15, ‘mangoes’: 3, ‘grapes’: 45}))
print(manipulate_data(‘set’,{“ANDELA1”,“TIA1”,“AFRICA1”}))
( PS. You cleanup the mess !!! ) - return the reverse of a list
###Reference
Used google search
builtin functions site:python.org
https://docs.python.org/2/library/functions.html#
python reversing list site:stackoverflow.com
http://stackoverflow.com/questions/3940128/how-can-i-reverse-a-list-in-python
http://stackoverflow.com/questions/16819222/how-to-return-dictionary-keys-as-a-list-in-python-3-3