Responsive Navbar with Google Search
☰ Menu
Home
Python
LaTeX
GNUPlot
Arduino
Feedback
Contact Us
Iterable Data Type: Tuple
Iterable Data Type
List
Tuple
String
Tuple: Program 1
'''built-in functions in list''' print(dir(list)) print('\n') '''built-in functions in tuple''' print(dir(tuple))
Run Code
Output 1
Tuple: Program 2
'''create tuple''' t = (1, 2, 3, 4, 5) print(t) print('\n') '''access tuple elements''' t = ('a', 'b', 'c', 'd') print(t[0], t[3]) print('\n') t = (10, 20, 30, 40, 50) print(t[-1], t[-2]) print('\n') '''slice tuple''' t = (100, 200, 300, 400, 500) print(t[1:4]) print('\n') '''tuple with mixed data types''' t = (1, 'hello', 3.14, True) print(t)
Run Code
Output 2
Tuple: Program 3
'''concatenate tuples''' t1 = (1, 2, 3) t2 = (4, 5, 6) t3 = t1 + t2 print(t3) print('\n') '''repeating tuples''' t = (1, 2, 3, 4) t_repeated = t * 3 print(t_repeated) print('\n') '''check if element exist''' t = (1, 2, 3, 4, 5) print(3 in t) print(6 in t) print('\n') '''iterate through tuple''' t = ('a', 'b', 'c', 'd') for item in t: print(item) print('\n') '''indexing tuple''' t = (100, 200, 300, 400) print(t.index(300))
Run Code
Output 3
Tuple: Program 4
'''indexing''' t = (100, 200, 300, 400) print(t.index(300)) print('\n') '''count element''' t = (1, 2, 2, 3, 4, 2, 5) print(t.count(2)) print('\n') '''unpacking tuple''' t = ('python', 'latex', 'gnuplot', 'arduino') p, q, r, s = t print(p, q, r, s) print('\n') '''tuple inside list''' l = [(1, 2), (3, 4), (5, 6)] for pair in l: print(pair) print('\n') '''Nested tuple''' t = ((1, 2), (3, 4), (5, 6)) print(t[1]) print(t[1][1])
Run Code
Output 4