Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is this construct called in python: ( x, y )

Tags:

python

What is this called in python:

[('/', MainPage)]

Is that an array .. of ... erhm one dictionary?

Is that

()

A tuple? ( or whatever they call it? )

like image 794
OscarRyz Avatar asked Aug 21 '09 19:08

OscarRyz


People also ask

What does X mean in Python?

X is a variable name, so could be any name allowed by python for variable names. As a variable , it's value will be different every time the loop circle ends, in this particular loop range(10) the value of x will start in 0 and next 1 , and next 2, until reach value of 10.

How A B C will be interpreted by Python?

In Python, and in some other languages, the expression a < b < c is equivalent to a < b and b < c . For example, the following code outputs “not increasing.” Now consider the analogous C code. int a = 3, b = 1, c = 2; if (a < b < c) printf( "increasingn" ); else printf("not increasingn");


4 Answers

Its a list with a single tuple.

like image 95
Shaun Avatar answered Oct 04 '22 10:10

Shaun


Since no one has answered this bit yet:

A tuple? ( or whatever they call it? )

The word "tuple" comes from maths. In maths, we might talk about (ordered) pairs, if we're doing 2d geometry. Moving to three dimensions means we need triples. In higher dimensions, we need quadruples, quintuples, and, uh, whatever the prefix is for six, and so on. This starts to get to be a pain, and mathematicians also love generalising ("let's work in n dimensions today!"), so they started using the term "n-tuple" for an ordered list of n things (usually numbers).

After that, a bit of natural laziness is all you need to drop the "n-" and we end up with tuples.

like image 42
John Fouhy Avatar answered Oct 04 '22 08:10

John Fouhy


Note that this:

("is not a tuple")

A tuple is defined by the commas, except in the case of the zero-length tuple. This:

"is a tuple",

because of the comma at the end. The parentheses just enforce grouping (again, except in the case of a zero-length tuple.

like image 34
jcdyer Avatar answered Oct 04 '22 09:10

jcdyer


That's a list of tuples.

This is a list of integers: [1, 2, 3, 4, 5]

This is also a list of integers: [1]

This is a (string, integer) tuple: ("hello world", 42)

This is a list of (string, integer) tuples: [("a", 1), ("b", 2), ("c", 3)]

And so is this: [("a", 1)]

In Python, there's not much difference between lists and tuples. However, they are conceptually different. An easy way to think of it is that a list contains lots of items of the same type (homogeneous) , and a tuple contains a fixed number of items of different types (heterogeneous). An easy way to remember this is that lists can be appended to, and tuples cannot, because appending to a list makes sense and appending to a tuple doesn't.

Python doesn't enforce these distinctions -- in Python, you can append to a tuple with +, or store heterogeneous types in a list.

like image 33
John Millikin Avatar answered Oct 04 '22 08:10

John Millikin