>> # Grocery list ... grocery_list = ['apples', 'bananas', 'oranges...">

For operator in python

I am confused about the "x" in the python code below.

>>> # Grocery list ... grocery_list = ['apples', 'bananas', 'oranges', 'milk'] >>> for x in grocery_list: ... print(x, len(x)) 

I am confused about the role of x in the above for. Is "x" the variable that is defined in the for statement, or is it something else? It seems to me that this is different from the way I used to define a variable, but I noticed that "x" can be anything, which makes me think that it is, in fact, a user variable.

Please, help.

+4
source share
8 answers

Yes, it is defined in the for statement. This is just a placeholder for an item in the list and can be called by anyone, for example.

 grocery_list = ['apples', 'bananas', 'oranges', 'milk'] for grocery in grocery_list: print(grocery, len(grocery)) 
+8
source

Python is a late-binding dynamic language. Although Python programmers and Python documentation often use the terms “variable” and “assignment,” the more precise terms are “name” and “binding”.

In your example, x is the name of the object. At each iteration through the loop, it is bound to the next object from your list. Python lists and most other Python containers, as well as many other Python object classes, have iteration functions. That is, they define functions by following a protocol that allows them to be used in for loops.

As you already noted, the Python name (similar to a “variable” in other languages) can be bound to any object of any type. A list can contain any mixture of object references. That way, when you iterate over a list, your "variable" (or loop name ) can be attached to objects of different types, potentially different types at each pass through the loop.

You can also have several names associated through “unpacking a tuple” at each stage of the iteration. For example, the following code snippet is a commonly used way of working with dictionaries:

 for key, value in some_dictionary.items(): # do something with each key and its associated value 

This form does not apply to dictionaries. The .items() dictionary method returns a sequence of two tuples of elements, and this form of the for loop can be used with any list or sequence that returns tuples of two elements (or even lists of two elements). Similarly, you can see that unpacking tuples is used for a sequence consisting of elements that contain the same number of elements:

 for category, item, quantity, price in some_invoice_query(): # do something with these items, etc. 

Conceptually, a “variable” in most programming languages ​​is a placeholder for data of a certain type (as well as the name by which this placeholder is referenced in the program source code). However, in Python (and other later cohesive dynamic languages), the name is a reference to an object and does not impose restrictions on the type or class of the object to which reference is made.

You might think roughly of Python names as if they were all void pointers in C.

+4
source

x is the name in your current namespace, and the objects in the grocery_list assigned to this name one by one.

+1
source

A variable will be assigned behind the scenes to each of the list values ​​in order. If the list contains links, then the link will be assigned to the loop variable, of course.

This is almost equivalent:

 for i in xrange(len(grocery_list)): x = grocery_list[i] # rest of code here 

But much cleaner and (potentially) faster. Name ( x ) is not significant, it can be anything.

After the loop is executed, the variable remains in scope, with the value of the last iteration that was performed. Therefore, if you use break to exit the loop, this will show:

 >>> for i in xrange(100): ... if i == 10: break ... >>> i 10 
+1
source

I think it’s good for you to consider x as a variable, which is defined in the for statement. Everything else is fine too. Python does not require a separate “definition” process; any variable is “defined” the first time it is assigned a value.

+1
source

x is a temporary variable that goes through the sequence. In lists x , each element of the list will go through.

 >>> grocery_list = ['apples', 'bananas', 'oranges', 'milk'] >>> for x in grocery_list: ... print(x, len(x)) ... apples 6 bananas 7 oranges 7 milk 4 >>> print(x) milk 

EDIT: Apparently, x remains undefined even after the for loop exits.

A few more examples should clarify the situation:

 >>> for x in 'some string': # x is bound to each character in the string ... print(x) ... s o m e s t r i n g >>> for x in (0, 1, 2, 3): # x is bound to each number in the tuple ... print(x) ... 0 1 2 3 >>> for x in [(0,0), (1,1), (2,2)]: # x is bound to each tuple in the list ... print(x) ... (0, 0) (1, 1) (2, 2) 
+1
source

In your example, x is a user-defined variable that will be assigned each value in the grocery_list .

0
source

You need to remember what the Python variable stores. It stores the location in memory where the object pointing to it is located. In other words, Python variables are basically pointers (void * s). They "know how to find their objects."

If you have x = 5 y = 3

assigning y = x actually transfers a copy of the memory address, where 5 is stored in y . Now x and y point to copy 3 in memory. Suppose you are trying to execute this

x = [1,2,3] for k in x: k = 0 What is happening? You pass a k copy of the memory address in which each element is stored. Then you assign k to point 0. Elements at x remain unperturbed.

Now do it

x = [[1,2,3], [4,5,6], [7,8,9]] for k in x: k[0] = 0

Then x contains a list

[[0, 2, 3], [0, 5, 6], [0, 8, 9]]

You can change the state of the object being changed through its memory address.

Morality: Python variables know WHERE to find their objects because they know where they are in memory. When you assign one variable to another, you pass the recipient variable a copy of the address of the donor variable. The loop variable in the for loop is another variable.

0
source

All Articles