473,387 Members | 1,569 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Make Python create a tuple with one element in a clean way

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 (without having to use
that ugly looking comma with no value after it).
Jun 27 '08 #1
4 1625
Hello,
so it would be clean if Python would convert anything put into ( ) to
be a tuple, even if just one value was put in (without having to use
that ugly looking comma with no value after it).
if it worked that way, it will absolutely mess up Python syntax, because
we mathematicians are used to use parentheses to force explicit operator
precedence, to group expressions, etc.

Should (1+1)*2 yield 4 or (2,2) ?

Jun 27 '08 #2
On May 11, 10:54 pm, wxPytho...@gmail.com wrote:
I thought that just putting a value inside ( )
would make a tuple. Apparently that is not the case.
It's not the case at all. Check out the Tuples & Sequences section in
the python docs at http://docs.python.org/tut/node7:

"A tuple consists of a number of values separated by commas"

So it's not the parentheses that define it as a tuple, but the comma.
>>my_tuple = 1,
my_tuple
(1,)
>>type(my_tuple)
<type 'tuple'>

Hope this helps.

- alex23
Jun 27 '08 #3
On May 11, 10:54 pm, wxPytho...@gmail.com wrote:
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'>
You needn't at all. You could simply do this:
>>your_tuple = 1,
You see, it's not the parentheses that make the tuple.

But if you do this
>my_tuple = (1)
type(my_tuple)

<type 'int'>

you don't get a tuple.
For which the BDFL should make us eternally grateful.
it would be clean if Python would convert anything put into ( ) to
be a tuple
You seriously want 2*(3+4) to return (7,7)? You call that "clean"?!

At least type(my_tuple) would always return 'tuple,' whether it was or
not. ;)

Jun 27 '08 #4
MC
Hi!
You want 2*(3+4) to return (7,7)?
For have that: 2*(3+4,)


--
@-salutations

Michel Claveau
Jun 27 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jinming Xu | last post by:
Hello everyone, While embedding my C++ program with Python, I am impeded by the conversion from a Python Tuple to a C++ array. I hope to get some assistance from you guys. I have a sequence...
3
by: Rakesh | last post by:
In my Python code fragment, I want to write a code fragment such that the minimum element of a tuple is subtracted from all the elements of a given tuple. When I execute the following python...
5
by: jeremy.d.brewer | last post by:
Hi, I'm rather new to Python, and I've just written my first Python C module. I was wondering if some more experience Pythonista would look over what I've written and given me some pointers (or...
28
by: Steven Bethard | last post by:
Ok, I finally have a PEP number. Here's the most updated version of the "make" statement PEP. I'll be posting it shortly to python-dev. Thanks again for the previous discussion and suggestions!...
7
by: Steven Bethard | last post by:
I've updated PEP 359 with a bunch of the recent suggestions. The patch is available at: http://bugs.python.org/1472459 and I've pasted the full text below. I've tried to be more explicit about...
5
by: Johann C. Rocholl | last post by:
The following is my first attempt at adding a taint feature to Python to prevent os.system() from being called with untrusted input. What do you think of it? # taint.py - Emulate Perl's taint...
5
by: vml | last post by:
I have a python com object which contains a method to inverse an array in vb 6 the definition of the class is : class Fop: _public_methods_ = def SqVal(self,*val): #vol=(val,val)...
0
by: DarrenWeber | last post by:
# Copyright (C) 2007 Darren Lee Weber # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free...
0
by: Delaney, Timothy (Tim) | last post by:
ssecorp wrote: Because immutability imposes a lot of restrictions and performance characteristics that mutable objects don't have. For example, compare building up a list and a tuple...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.