Python Forum
What is the purpose of this append command!
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What is the purpose of this append command!
#1
def square(x):
    return x * x


def my_calculation(x, y):
    result = []
    for i in y:
        result.append(x(i))
    return result


squares = my_calculation(square, [1, 2, 3, 4, 5])

print(squares)


sorry everyone, i am a beginner! in these codes i did not understand why we have to do ? Arrow "result.append(x(i))

or if anyone can explain me all steps, would be much appreciated.
thanks again.
Reply
#2
result.append(x(i))
result is a list
result.append() append an item to the list
x() x is a function passed to my_calculation and the () calls the function
i is the value obtained from iterating through the list y that is passed to my_calculation
Reply
#3
def square(x):
    return x * x
 
 
def my_calculation(x, y):
    result = []
    for i in y:
        result.append(x(i))
        print(type(x))
    return result
 
 
squares = my_calculation(square, [1, 2, 3, 4])
 
print(squares)
I als come to know that x is function by adding one extra line 9 in above code. But I am wondering there is no pre-defined function called 'x' in there. 'x' is argument in "def square(x)" only. Please enlighten.
Professional Dentist(32years) fell in love with Python during COVID-19 Lockdown.

"Nothing can stop you from learning new things except your own will"

Reply
#4
(Apr-29-2020, 03:49 AM)Shahmadhur13 Wrote: But I am wondering there is no pre-defined function called 'x' in there. 'x' is argument in "def square(x)" only.
well, there is x parameter in def my_calculation(x, y): too.

By the way, more meaningful names would be better and make easier to follow the code.
e.g. sqare takes a number as argument.
So it would be better to write it like this
def square(number):
    return number * number
same for my_calculation. First param is a function, second is sequence of numbers. e.g.
def my_calculation(func, numbers):
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
In the function my_calculation, x is the first parameter that is passed in. In your program, this is called at line 13. There, the first parameter is square, which is indeed a function defined earlier in your program.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  What is the useful purpose of Python? dorlow 2 1,734 Sep-16-2020, 04:22 PM
Last Post: JaneOgden91fCL
  print scripts example includes comma that seems to serve no purpose flour_power_33 5 2,797 Sep-02-2020, 03:32 AM
Last Post: flour_power_33
  Cant Append a word in a line to a list err "str obj has no attribute append Sutsro 2 2,575 Apr-22-2020, 01:01 PM
Last Post: deanhystad
  Purpose of the keyword "pass" Tim 3 3,090 Feb-24-2018, 07:58 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020