How do you add numbers to a tuple in python?
I am learning Python and creating a database connection. While trying to add to the DB, I am thinking of creating tuples out of information and then add them to the DB. Show What I am Doing: I am taking information from the user and store it in variables. Can I add these variables into a tuple? Can you please help me with the syntax? Also if there is an efficient way of doing this, please share... EDIT Let me edit this question a bit...I only need the tuple to enter info into the DB. Once the information is added to the DB, should I delete the tuple? I mean I don't need the tuple anymore.
Nathan 7,5146 gold badges46 silver badges70 bronze badges asked Sep 4, 2009 at 18:36
Tuples are immutable; you can't change which variables they contain after construction. However, you can concatenate or slice them to form new tuples:
And, of course, build them from existing values:
John R Perry 3,6832 gold badges35 silver badges59 bronze badges answered Sep 4, 2009 at 18:39
John MillikinJohn Millikin 193k39 gold badges211 silver badges222 bronze badges 5 You can start with a blank tuple with something like
phoenix 6,3684 gold badges36 silver badges44 bronze badges answered Dec 16, 2011 at 18:43
DanielDaniel 2,8662 gold badges14 silver badges12 bronze badges 5 In Python 3, you can use
The byte code is almost the same as
The only difference is answered Jun 22, 2018 at 10:34
Yang BoYang Bo 3,4403 gold badges21 silver badges33 bronze badges 4 Another tactic not yet mentioned is using appending to a list, and then converting the list to a tuple at the end:
returns
I sometimes use this when I have to pass a tuple as a function argument, which is often necessary for the numpy functions. answered May 7, 2014 at 23:20
ehontzehontz 1,06010 silver badges7 bronze badges 2 It's as easy as the following:
However, tuples in Python are immutable, so you cannot append variables to a tuple once it is created. answered Sep 4, 2009 at 18:39
mipadimipadi 385k88 gold badges515 silver badges475 bronze badges " once the info is added to the DB, should I delete the tuple? i mean i dont need the tuple anymore." No. Generally, there's no reason to delete anything. There are some special cases for deleting, but they're very, very rare. Simply define a narrow scope (i.e., a function definition or a method function in a class) and the objects will be garbage collected at the end of the scope. Don't worry about deleting anything. [Note. I worked with a guy who -- in addition to trying to delete objects -- was always writing "reset" methods to clear them out. Like he was going to save them and reuse them. Also a silly conceit. Just ignore the objects you're no longer using. If you define your functions in small-enough blocks of code, you have nothing more to think about.] answered Sep 4, 2009 at 20:06
S.LottS.Lott 376k78 gold badges503 silver badges771 bronze badges As other answers have noted, you cannot change an existing tuple, but you can always create a new tuple (which may take some or all items from existing tuples and/or other sources). For example, if all the items of interest are in scalar variables and you know the names of those variables:
to be used, e.g, as in this example:
of course this one case would be more simply expressed as answered Sep 4, 2009 at 18:47
Alex MartelliAlex Martelli 823k163 gold badges1202 silver badges1379 bronze badges I'm pretty sure the syntax for this in python is:
once set, tuples cannot be changed. answered Sep 4, 2009 at 18:39
Jon WJon W 15.2k6 gold badges36 silver badges47 bronze badges Can we add values in tuple?You can't add elements to a tuple because of their immutable property.
How do you add items to a tuple in Python?Use the + operator to add an element to a tuple
Use the syntax tuple + new_element , where new_element is a single or multiple item tuple, to append new_element to the end of tuple .
How do you add tuple values together?Concatenating and Multiplying Tuples
Concatenation is done with the + operator, and multiplication is done with the * operator. Because the + operator can concatenate, it can be used to combine tuples to form a new tuple, though it cannot modify an existing tuple. The * operator can be used to multiply tuples.
How do you extend a tuple?There's no append() or extend() method for tuples in Python. Tuples are immutable data types so you can't remove the element from it. However, you can extend the tuple using concatenate or slice method.
|