Python Tuple Initialization with one value – Quick and Easy Thumb Rule:

To create a tuple with one element, you need to do this:

>>> my_tuple = (1,)                  # Note the trailing comma after the value 1
>>> type(my_tuple)
<type ‘tuple’>

But if you do this

>>> my_tuple = (1)
>>> type(my_tuple)
<type ‘int’>

you don’t get a tuple. I thought that just putting a value inside ( )
would make a tuple. Apparently that is not the case. I hate ugly code
so it would be clean if Python would convert anything put into ( ) to
be a tuple, even if just one value was put in use comma after the value, For example:  “my_tuple = (1,) “.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s