Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type(3,) returns an integer instead of a tuple in python, why?

type(3,) returns the int type, while

t = 3,
type(t)

returns the tuple type. Why?

like image 329
Bentley4 Avatar asked Apr 03 '12 21:04

Bentley4


People also ask

What is a 3 tuple in Python?

Tuple. Tuples are used to store multiple items in a single variable. Tuple is one of 4 built-in data types in Python used to store collections of data, the other 3 are List, Set, and Dictionary, all with different qualities and usage. A tuple is a collection which is ordered and unchangeable.

How do you change an int to a tuple?

There are multiple ways to convert a tuple to an integer:Access a tuple element at its index and convert it to an int, e.g. int(my_tuple[0]) . Sum or multiply the elements of the tuple. Convert a tuple of strings to a tuple of integers.

What does tuple return in Python?

Built-in Tuple Functions Returns item from the tuple with max value.

Does return give a tuple?

A Python function can return any object such as a tuple. To return a tuple, first create the tuple object within the function body, assign it to a variable your_tuple , and return it to the caller of the function using the keyword operation “ return your_tuple “.


1 Answers

Inside the parentheses that form the function call operator, the comma is not for building tuples, but for separating arguments. Thus, type(3, ) is equivalent to type(3). An additional comma at the end of the argument list is allowed by the grammar. You need an extra pair of parens to build a tuple:

>>> def f(x):
...     print x
... 
>>> f(3)
3
>>> f(3,)
3
>>> f((3,))
(3,)
like image 173
Sven Marnach Avatar answered Oct 02 '22 19:10

Sven Marnach