tuple in python

Solutions on MaxInterview for tuple in python by the best coders in the world

showing results for - "tuple in python"
Juan Martín
27 Sep 2018
1x = ("apple", "banana", "cherry")
2y = list(x)
3y[1] = "kiwi"
4x = tuple(y)
5
6print(x)
Emanuele
27 Apr 2018
1# A tuple is a sequence of immutable Python objects. Tuples are
2# sequences, just like lists. The differences between tuples
3# and lists are, the tuples cannot be changed unlike lists and
4# tuples use parentheses, whereas lists use square brackets.
5tup1 = ('physics', 'chemistry', 1997, 2000);
6tup2 = "a", "b", "c", "d";
7
8# To access values in tuple, use the square brackets for
9# slicing along with the index or indices to obtain value
10# available at that index.
11tup1[0] # Output: 'physics'
Manuela
24 May 2016
1my_tuple = 3, 4.6, "dog"
2print(my_tuple)
3
4# tuple unpacking is also possible
5a, b, c = my_tuple
6
7print(a)      # 3
8print(b)      # 4.6
9print(c)      # dog
Andrea
17 Aug 2019
1tupel = ('banana',10,True)
2print(tupel[2])
Simeon
18 May 2018
1#a tuple is basically the same thing as a
2#list, except that it can not be modified.
3tup = ('a','b','c')
Brandon
10 Jan 2018
1Tup1 = (1) 
2
3print(Tup1)
4
5print(type(Tup1))
6
7
8# To crate a tuple you must have at least 2 ELEMENTS
9
10tup3 = (1, 2, 3, 4, 5, 6, 7, 8, 9)
11tup4 = ('a', 'b', 'c')
12
13defseq = 2,5
14print(defseq)
15print(type(defseq))
16
17# 2 or more integers assigned to a variable will become tuple automaticaly 
18
19
20tuple = (1, 2, 3, 4, 5, 6, 7, 8, 9)
21
22print("Original", tuple)
23
24print("Single element", tuple[2])
25
26print("Definate range", tuple[2:5])
27
28#print("Start a definate", tuple[])
29
30# tup = (3, 5, 7, 9)
31# del tup
32# print("It'll show an error")
33# print(tup, "It'll show an error")
34
35# print("Length of tuple is ", len(tup))
36
37
38# CONCATENATE 
39
40tup3 = (8,2)
41print(tup3+tuple)
42
43print(("Hello Students ") * 3)
44
45print("Membership =", 8 in tup3)
46
47print(max(tup3))
48print(min(tup3))
49
50quo, rem  = divmod(100, 3)
51
52print("Quotient =", quo)
53print("Remainder = ", rem)
54
55com_tup1 = (1, 2, 3, 4) # sum is 10
56com_tup2 = (7, 8, 9, 2) # sum is 26 
57
58print(com_tup1, com_tup2)
59print(com_tup1 > com_tup2, "Tuple 1 is greater than tuple 2")
60print(com_tup1 < com_tup2, "Tuple 2 is greater than tuple 2")
61print(com_tup1 == com_tup2, "They're both the same ") 
62
63if com_tup1 > com_tup2:
64    print("Tuple 1 is greater than tuple 2")
65elif com_tup1 < com_tup2:
66    print("Tuple 2 is greater than tuple 1")
67else:
68    print("They're both the same")
69
70# Comparision is not the length, it's the sum  
71
72# Tuples are compared position by position: the first item of the first tuple is compared to the first item of the second tuple; if they are 
73# not equal (i.e. the first is greater or smaller than the second) then that's 
74# the result of the comparison, else the second item is considered, then the third and so on.
75
76# Nested Tuples
77
78toppers = ('Arnav', 101, 'High School', ('999-6969-420', 'helloworld@gmail.com')), ('Mayank', 107, 'Inter', ('999-6969-420', 'helloworld@gmail.com')), ('Manish', 103, 'Higher Sec', ('999-6969-420', 'helloworld@gmail.com'))
79
80print(toppers)
81
82for i in toppers:
83    print(i)
84
85print(com_tup1.index(3))
86print(com_tup1.count(3))
87
88# Zip() function - takes 2 or more sequences and zips into a list (creats a list with tuples)
89
90tup_zip = (1, 2, 3, 4)
91list_zip = ['a', 'b', 'c', 'd']
92
93print(list(zip(tup_zip, list_zip)))
94
95# Sorted() function. The ssort() function doesn't work because it is immutable 
96
97# sorted to sort a tuple of values  
98
99tup_sort = (1, 4, 3, 5, 3, 4, 7, 2, 3, 9)
100print(sorted(tup_sort))
similar questions
queries leading to this page
tuple in python