Python Forum
what does x reprsent in this code ?
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
what does x reprsent in this code ?
#1
thisset = {"apple", "banana", "cherry"}

for x in thisset:
  print(x)
Reply
#2
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
Reply
#3
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...
Reply
#4
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

Reply
#5
okay, got it !
thanks !
Reply
#6
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
Green
Is clearer than.
>>> colors = ['Red', 'Blue', 'Green']
>>> for rubberduck in colors:
...     print(rubberduck)    
...     
Red
Blue
Green
Reply
#7
thank you too !
certainly that is a better habit...

pyzyx3qwerty thank you !
Reply
#8
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)
Reply
#9
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

Reply
#10
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
Reply


Forum Jump:

User Panel Messages

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