+ 7
What are tuples in Python?
Please Answer!!!
11 Answers
+ 7
A tuple is an immutable sequence of objects. Tuples cannot be changed; tuple concatenation creates a new tuple object.
Example code:
# Tuples (immutable)
t1 = (1, 2, 3, 4)
t2 = t1
print(t1) ==> (1, 2, 3, 4)
id(t1) ==> 40630432
print(t2) ==> (1, 2, 3, 4)
id(t2) ==> 40630432
t2 = t2[:2] + (5,) + t2[2:]
print(t1) ==> (1, 2, 3, 4)
id(t1) ==> 40630432
print t2 ==> (1, 2, 5, 3, 4)
id(t2) ==> 63108848
# Lists (mutable)
L1 = [1, 2, 3, 4]
L2 = L1
id(L1) ==> 63475504
id(L2) ==> 63475504
L2.insert(2, 5)
print(L1) ==> [1, 2, 5, 3, 4]
print(L2) ==> [1, 2, 5, 3, 4]
id(L1) ==> 63475504
id(L2) ==> 63475504
The id() of each object makes it clear--you can appear to change a tuple in code, but the underlying object doesn't actually change. You're simply reassigning the variable name to another object.
Tuples are great if you have multiple return values from a function; you can return them all as a tuple, then 'unpack' them in the calling function to whatever variables you need.
+ 4
Tuple in python is basically like arrays in other languages like Java.
+ 3
A tuple is a sequence of objects. It's similar to a list, but tuples are immutable, so you cannot change the values of the objects inside a tuple, as you can do in a list, or append other objects. You can use tuples for example to store x-y coordinates of a point like this :
tup1 = (x, y), and then access the values like you would do with a list
print (tup1[0]) #returns x
+ 3
Doesn't that in fact create a new tuple from the old tuple + new element? So really, you don't modify a tuple you create a new extended one. Same with inserting at a certain index. Right?
+ 2
Yes you can add other elements to the tuple!!!
tuple+= (new_element,) it adds the new element to the end of the tuple, There is also a way of adding an item in between other tuple elements :
tuple=tuple[:index]+(4,)+tuple[index:] in this case number 4 will be in between index values.
edit:
It works exactly the same when you want to remove something from the tuple.
+ 1
tuple are same like lists(or arrays you've studied so far in C) but the difference is that they are immutable. immutable means we cannot reassign the value again to tuple by having it's index.
+ 1
tuple are same like lists(or arrays you've studied so far in C) but the difference is that they are immutable. immutable means we cannot reassign the value again to tuple by having it's index.
0
oh Ok, my bad. Thanks for the correction
0
now also there is need to understand tuples
0
Tupal is just like list which is represented by () instead [] and cannot update or change
0
and then when u sense to know it all
Python Gods introduced "namedtupel"