Apr-09-2020, 01:57 PM
thisset = {"apple", "banana", "cherry"} for x in thisset: print(x)
what does x reprsent in this code ?
|
Apr-09-2020, 01:57 PM
thisset = {"apple", "banana", "cherry"} for x in thisset: print(x)
Apr-09-2020, 02:03 PM
(This post was last modified: Apr-09-2020, 02:03 PM by pyzyx3qwerty.)
x basically represents all the items in the set, and prints them out.
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela Need help on the forum? Visit help @ python forum For learning more and more about python, visit Python docs
Apr-09-2020, 02:09 PM
but the thing is i can change it to anything...i can even call it 'you' or 'she'...and it will act on it in the same way - the set it still being printed...
Apr-09-2020, 02:25 PM
yes,
x is variable, or name as we call them in python.you iterate over a set in the for loop and in each iteration x takes the value from the set
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
Apr-09-2020, 02:28 PM
okay, got it !
thanks !
Apr-09-2020, 02:31 PM
Yes the loop variable can be called whatever you want.
The only job it has is to take on the value of the next element in iterable each time through the loop.A general rule if the iterable data collection has some stuff that can be given useful loop variable name,then do that.>>> colors = ['Red', 'Blue', 'Green'] >>> for color in colors: ... print(color) ... Red Blue GreenIs clearer than. >>> colors = ['Red', 'Blue', 'Green'] >>> for rubberduck in colors: ... print(rubberduck) ... Red Blue Green
Apr-09-2020, 02:49 PM
thank you too !
certainly that is a better habit... pyzyx3qwerty thank you !
Apr-10-2020, 07:03 PM
(This post was last modified: Apr-10-2020, 07:03 PM by astral_travel.)
another one which is simple, but i'm trying to understand the underlying theme...
how can this function be defined with 'food' and when it is called it is given the list argument ? def my_function(food): for x in food: print(x) fruits = ["apple", "banana", "cherry"] my_function(fruits)
in the function definition (line 1)
food is a parameter.when you call the function on line 7, you pass fruits as arguments to the function. That is the actual value of food in this particular call of the function.There are positional and keyword parameters/arguments. check the links for more details. In this case food is positional parameter.read our tutorial on functions: https://python-forum.io/Thread-Basic-Functions also glossary for parameters and arguments
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
Apr-11-2020, 10:34 AM
(This post was last modified: Apr-11-2020, 10:37 AM by pyzyx3qwerty.)
food is one of the things defined under the function. You will frequently see that as you go through functions.
So, for example: def abc(name): print(name)In the above lines, name is one of the parameters. Now, n = "pyzyx3qwerty" abc(n)in the above lines, it will n out because n represents name in the lines
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela Need help on the forum? Visit help @ python forum For learning more and more about python, visit Python docs |
|