Home > Enterprise >  How we go from food argument to printing Fruits values by my_function?
How we go from food argument to printing Fruits values by my_function?

Time:12-29

def my_function(food):
  for x in food:
    print(x)

fruits = ["apple", "banana", "cherry"]

my_function(fruits)

I'm on https://www.w3schools.com/python/python_functions.asp

I just don't understand how we go from food to printing fruits. my_function is (food). How does it become Fruits?

CodePudding user response:

Looking at the code:

def my_function(food):
  for x in food:
    print(x)

fruits = ["apple", "banana", "cherry"]

my_function(fruits)

The first three lines define (def) a function. In this case, the function is named my_function. Python looks at the whitespace starting the second and third line, and sees that you want these to be part of my_function.

my_function takes a single argument which can be provided by another part of your code when it wants to run the function. The label it uses for this argument is food.

Looking at the code inside the function

for x in food:
  print(x)

the function is expecting a list that it can iterate over, passing each value from the list to the print function.

Now that you have the function defined, you can run it from the rest of the code.

In this case, there are two other things the code does:

  1. It defines a variable called fruits and assigns it a list of strings - "apple", "banana", and "cherry".
  2. It then runs the function my_function with the argument being the variable fruits.

So when the function runs, the argument that it receives from the rest of the code is the list which is contained in fruits, but it can refer to it locally as food and perform the operations on that list that it does (which is printing all the items in the list).

If you consider the following code, it may be easier to see why we do this:

def my_function(food):
  for x in food:
    print(x)

fruits = ["apple", "banana", "cherry"]

vegetables = ["lettuce", "carrot", "potato"]

my_function(fruits)

my_function(vegetables)

Here, we define two lists fruits and vegetables. We then run the function my_function providing each of these, but the function only ever knows the argument as food. That way it can do all the same operations (printing the items in the list) without needing to know what we called the variable in the rest of the code.

Hopefully that helped. If I've made a mess of the explanation or you have any other questions, let me know!

CodePudding user response:

I'm new myself, but the best way I can describe it is:

You made a function called my_function() which in this case takes in the input of food shown by the parentheses brackets. my_function(food)

So whenever you want to call my_function() later down the line, you will be required to give it an input.

The input in this case can be any iterable things. (strings, lists, tuples, sets, dictionaries, etc)

Iterables are anything that you can loop over. More on iterables : https://www.analyticsvidhya.com/blog/2021/07/everything-you-should-know-about-iterables-and-iterators-in-python-as-a-data-scientist/

So in a sense, food means fruits.

And x "turns" into each iterable value, first as apple then prints itself, then turns into banana and prints itself, etc.

For example, try to add a variable word = 'Poptart' Then you can call my_function() using word by typing my_function(word) which will give you

P
o
p
t
a
r
t

You can also look at W3School's loop page https://www.w3schools.com/python/python_for_loops.asp

Hope this helps in anyway.

  • Related