Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

for statement in python

Tags:

python

I'm confused about "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 x's role in the for statement above. Is "x" a variable that is being defined within the for statement, or is it something else? It just seems different than how I am used to defining a variable, but I noticed "x" can be anything, which makes me think it is, in fact, a user-defined variable.

Please help.

like image 240
MSey Avatar asked Dec 05 '22 22:12

MSey


2 Answers

Yes it's defined within the for statement. It's just a placeholder for an element in the list and can be called anything, e.g.

grocery_list = ['apples', 'bananas', 'oranges', 'milk']
for grocery in grocery_list:
  print(grocery, len(grocery))
like image 76
ralphtheninja Avatar answered Dec 07 '22 11:12

ralphtheninja


Python is a late-binding dynamic language. While Python programmers and the Python documentation frequently use the terms "variable" and "assignment" the more precise terms are "name" and "binding."

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

As you've noted a Python name (analogous to a "variable" in other languages) can be bound to any object of any type. A list can contain any mixture of object references. Thus, when you iterate over a list your "variable" (or loop name(s)) can be bound to objects of different types, potentially different types on each pass through the loop.

You can also have multiple names bound through "tuple unpacking" at each step through the iteration. For example the following snippet of code is a commonly used way to deal with dictionaries:

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

This form isn't specific to dictionaries. The .items() method of dictionaries returns a sequence of two item tuples and this form of for loop could be used with any list or sequence which returned two-item tuples (or even two-item lists). Similarly you could see tuple unpacking used on sequence consisting of items which contain a uniform number of items:

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 a name by which that placeholder is referred throughout a program's source code). However, in Python (and other late-binding dynamic languages) a name is a reference to an object and conveys no constraint regarding the type or class of object to which the reference is made.

You can, rather crudely, think of Python names as if they were all void pointers in C.

like image 26
Jim Dennis Avatar answered Dec 07 '22 11:12

Jim Dennis