+ 7

What are tuples in Python?

Please Answer!!!

30th Nov 2016, 4:07 PM
Jaydeep Khatri
Jaydeep Khatri - avatar
11 Antworten
+ 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.
30th Nov 2016, 12:03 AM
J Baughman
+ 4
Tuple in python is basically like arrays in other languages like Java.
30th Nov 2016, 3:23 PM
Resurektzz7
Resurektzz7 - avatar
+ 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
10th Nov 2016, 10:09 AM
Anitei Leonard
Anitei Leonard - avatar
+ 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?
10th Nov 2016, 4:51 PM
Eduard Alexandru
Eduard Alexandru - avatar
+ 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.
10th Nov 2016, 11:02 AM
Mateusz Wiezik
Mateusz Wiezik - avatar
+ 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.
2nd Dec 2016, 5:41 PM
Ravi garg
Ravi garg - avatar
+ 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.
5th Dec 2016, 4:37 PM
Ravi garg
Ravi garg - avatar
0
oh Ok, my bad. Thanks for the correction
10th Nov 2016, 11:06 AM
Anitei Leonard
Anitei Leonard - avatar
0
now also there is need to understand tuples
1st Dec 2016, 6:15 PM
Siddharth Verma
Siddharth Verma - avatar
0
Tupal is just like list which is represented by () instead [] and cannot update or change
5th Dec 2016, 1:12 PM
Nilanj Patel
Nilanj Patel - avatar
0
and then when u sense to know it all Python Gods introduced "namedtupel"
12th Dec 2016, 5:30 AM
Dilip Pandey
Dilip Pandey - avatar