Python tuple program to convert a tuple into a string

In this python tuple program, we will convert a tuple into a string.

Steps to solve the program
  1. Create a tuple having characters and create an empty string.
  2. Use for loop to iterate over characters in the tuples and add each character to the empty string.
  3. Print the output.
				
					tup = ('s','q','a','t','o','o','l','s')
str1 = ''
for char in tup:
    str1 += char
    
print("String: ",str1)
				
			

Output :

				
					String:  sqatools
				
			

add an item to a tuple.

get the 2nd element from the front and the 3rd element from the back of the tuple.

Python tuple program to add an item to a tuple

In this python tuple program, we will add an item to a tuple.

Steps to solve the program
  1. Create a tuple.
  2. Convert the tuple into a list using list().
  3. Now add an item to the list using append().
  4. Then convert the list into a tuple using tuple().
  5. Print the output.
				
					tup = (18,65,3,45)
print("Old tuple: ",tup)
tup = list(tup)
tup.append(15)
tup = tuple(tup)
print("New tuple: ",tup)
				
			

Output :

				
					Old tuple:  (18, 65, 3, 45)
New tuple:  (18, 65, 3, 45, 15)
				
			

assign values of tuples to several variables and print them.

convert a tuple into a string.

Program to assign values of tuples to several variables

In this python tuple program, we will assign values of tuples to several variables and print them.

Steps to solve the program
  1. Create a tuple.
  2. Assign values of tuples to several variables by using variable_names=tuple.
  3. Print the variable name and their value.
				
					tup =  (6,7,3)
(a,b,c) = tup
print("a: ",a)
print("b: ",b)
print("c: ",c)
				
			

Output :

				
					a:  6
b:  7
c:  3
				
			

create a tuple and find an element from it by its index no.

add an item to a tuple.

Create a tuple and find an element from the tuple by its index number

In this python tuple program, we will create a tuple and find an element from the tuple by its index number.

Steps to solve the program
  1. Create a tuple.
  2. Find the element from the tuple having index number 2 using tuple_name[index number].
  3. Print the output.
				
					tup = (4,8,9,1)
print("Number in the tuple with index 2: ",tup[2])
				
			

Output :

				
					Number in the tuple with index 2:  9
				
			

create a tuple with different datatypes.

assign values of tuples to several variables and print them.

Program to create a tuple with different datatypes

In this python tuple program, we will create a tuple with different datatypes.

Steps to solve the program
  1. You can simply create a tuple with different datatypes by adding elements with different datatypes in () to create a  tuple.
  2. Print the output.
				
					tup = (2.6,1,'Python',True,[5, 6, 7],(5, 1, 4),{'a':123,'b':456})
print("Tuple: ",tup)
				
			

Output :

				
					Tuple:  (2.6, 1, 'Python', True, [5, 6, 7], (5, 1, 4), {'a': 123, 'b': 456})
				
			

create a list of tuples from a list having a number and its square in each tuple.

create a tuple and find an element from it by its index no.

Create a list of tuples from a list having a number and its square in each tuple

In this python tuple program, we will create a list of tuples from a list having a number and its square in each tuple.

Steps to solve the program
  1. Take a list as input.
  2. Use the list comprehension method to create a list of tuples from a list having a number and its square in each tuple.
  3. Use for loop to iterate over elements in the list and use pow() to get the square of the elements.
  4. Perform the above operations inside a list to achieve list comprehension.
  5. Print the output.
				
					list1 = [4,6,3,8]

tup = [(val, pow(val, 2)) for val in list1]
print(tup)
				
			

Output :

				
					[(4, 16), (6, 36), (3, 9), (8, 64)]
				
			

find the minimum value from a tuple.

create a tuple with different datatypes.

Find the minimum value from a tuple

In this python tuple program, we will find the minimum value from a tuple.

Steps to solve the program
  1. Create a tuple.
  2. Find the minimum value from a tuple using min().
  3. Print the output.
				
					tup = (36,5,79,25)
print("Minimum value: ",min(tup))
				
			

Output :

				
					Minimum value:  5
				
			

find the maximum value from a tuple.

create a list of tuples from a list having a number and its square in each tuple.

Program to create a tuple with 2 lists of data

In this python tuple program, we will create a tuple with 2 lists.

Steps to solve the program
  1. Take two lists as input.
  2. Combine elements having the same index no using zip() and convert that list into a tuple using tuple().
  3. Print the output.
				
					list1 = [4, 6, 8]
list2 = [7, 1, 4]
tup =tuple(zip(list1,list2))
print(tup)
				
			

Output :

				
					((4, 7), (6, 1), (8, 4))
				
			

find the maximum value from a tuple.

Python Tuple Practice Programs, Exercises

Python tuple is a data structure that can hold any number of objects. A tuple is an immutable sequence of values

1). Python tuple program to create a tuple with 2 lists of data.
Input lists:
list1 = [4, 6, 8]
list2 = [7, 1, 4]
Output= ((4, 7), (6, 1), (8, 4))

2). Python tuple program to find the maximum value from a tuple.
Input = (41, 15, 69, 55)
Output = 69

3). Python tuple program to find the minimum value from a tuple.
Input = (36,5,79,25)
Output = 5

4). Python tuple program to create a list of tuples from a list having a number and its square in each tuple.
Input = [4,6,3,8]
Output = [ (4, 16), (6, 36), (3, 27), (8, 64) ]

5). Python tuple program to create a tuple with different datatypes.
Output= ( 2.6, 1, ‘Python’, True, [5, 6, 7], (5, 1, 4), {‘a’: 123, ‘b’: 456})

6). Python tuple program to create a tuple and find an element from it by its index no.
Input = (4, 8, 9, 1)
Index = 2
Output = 9

7). Python tuple program to assign values of tuples to several variables and print them.
Input = (6,7,3)
Variables = a,b,c
Output:
a, 6
b, 7
c, 3

8). Python tuple program to add an item to a tuple.
Input= ( 18, 65, 3, 45)
Output=(18, 65, 3, 45, 15)

9). Python tuple program to convert a tuple into a string.
Input = (‘s’, ‘q’, ‘a’, ‘t’, ‘o’, ‘o’, ‘l’, ‘s’)
Output = Sqatools

10). Python tuple program to get the 2nd element from the front and the 3rd element from the back of the tuple.
Input = (‘s’, ‘q’, ‘a’, ‘t’, ‘o’, ‘o’ ,’l’, ‘s’)
Output=
q
o

11). Python tuple program to check whether an element exists in a tuple or not.
Input = ( ‘p’ ,’y’, ‘t’, ‘h’, ‘o’, ‘n’)
P in A
Output=
True

12). Python tuple program to add a list in the tuple.
Input:
L=[12,67]
A=(6,8,4)
Output:
A=(6,8,4,12,67)

13). Python tuple program to find sum of elements in a tuple.
Input:
A=(4,6,2)
Output:
12

14). Python tuple program to add row-wise elements in Tuple Matrix
Input:
A = [[(‘sqa’, 4)], [(‘tools’, 8)]]
B = (3,6)
Output:
[[(‘sqa’, 4,3)], [(‘tools’, 8,6)]]

15). Python tuple program to create a tuple having squares of the elements from the list.
Input = [(1,5,7), (3,6)]
Output = (1, 81, 25, 49, 36)

16). Python tuple program to multiply adjacent elements of a tuple.
Input = (1,2,3,4)
Output =  (2,6,12)

17). Python tuple program to join tuples if the initial elements of the sub-tuple are the same.
Input:
[(3,6,7),(7,8,4),(7,3),(3,0,5)]
Output:
[(3,6,7,0,5),(7,8,4,3)]

18). Python tuple program to convert a list into a tuple and multiply each element by 2.
Input = [12,65,34,77]
Output = (24, 130, 68, 154)

19). Python tuple program to remove an item from a tuple.
Input:
A=(p,y,t,h,o,n)
Output: (p,y,t,o,n)

20). Python tuple program to slice a tuple.
Input:
A=(5,7,3,4,9,0,2)
Output:
(5,7,3)
(3,4,9)

21). Python tuple program to find an index of an element in a tuple.
Input:
A=(s,q,a,t,o,o,l,s)
Index of a?
Output = 2

22). Python tuple program to find the length of a tuple.
Input:
A=(v,i,r,a,t)
Output=
5

23). Python tuple program to convert a tuple into a dictionary.
Input:
A=((5,s),(6,l))
Output = { s: 5, l: 6 }

24). Python tuple program to reverse a tuple.
Input = ( 4, 6, 8, 3, 1)
Output= (1, 3, 8, 6, 4)

25). Python tuple program to convert a list of tuples in a dictionary.
Input = [ (s, 2), (q, 1), (a, 1), (s, 3), (q, 2), (a, 4) ]
Output ={ s: [ 2, 3 ], q: [ 1, 2 ], a: [ 1 ,4 ] }

26). Python tuple program to pair all combinations of 2 tuples.
Input :
A=(2,6)
B=(3,4)
Output
[ (2, 3), (2, 4), (6, 3), (6, 4), (3, 2), (3, 6), (4, 2), (4, 6) ]

27). Python tuple program to remove tuples of length i.
Input = [ (2, 5, 7), (3, 4), ( 8, 9, 0, 5) ]
i=2
Output= [ (2, 5, 7), ( 8, 9, 0, 5) ]

28). Python tuple program to remove tuples from the List having an element as None.
Input = [(None, 2), (None, None), (5, 4), (1,6,7)]
Output= { (5, 4), (1, 6, 7) }

29). Python tuple program to remove Tuples from the List having every element as None.
Input = [(None,), (None, None), (5, 4), (1,6,7),(None,1)]
Output = [(5, 4), (1,6,7),(None,1)]

30). Python tuple program to sort a list of tuples by the first item.
Input = [ (1, 5), (7, 8), (4, 0), (3, 6) ]
Output = [ (1, 5), (3, 6), (4, 0), (7, 8) ]

31). Python tuple program to sort a list of tuples by the second item.
Input = [ (1, 5), (7, 8), (4, 0), (3, 6) ]
Output = [ (4, 0), (1, 5), (3, 6), (7, 8) ]

32). Python tuple program to sort a list of tuples by the length of the tuple.
Input = [(4, 5, 6), ( 6, ), ( 2, 3), (6, 7, 8, 9, 0 ) ]
Output=
[(6,),(2,3),(4,5,6),(6,7,8,9,0)]

33). Python tuple program to calculate the frequency of elements in a tuple.
Input=
(a, b, c, d, b, a, b)
Output=
{ a:2, b:3, c:1, d:1 }

34). Python tuple program to filter out tuples that have more than 3 elements.
Input=
[ (1, 4), (4, 5, 6), (2, ), (7, 6, 8, 9), (3, 5, 6, 0, 1) ]
Output= [(7, 6, 8, 9), (3, 5, 6, 0, 1)]

35). Python tuple program to assign the frequency of tuples to each tuple.
Input=
[ (s,q), (t, o, o, l), (p, y), (s, q) ]
Output= {(‘s’, ‘q’): 2, (‘t’, ‘o’, ‘o’, ‘l’): 1, (‘p’, ‘y’): 1}

36). Python program to find values of tuples at ith index number.
Input=
[ (1, 2, 3), (6, 5, 4), (7, 6, 8), (9, 0, 1) ]
I = 3
Output= (9,0,1)

37). Python tuple program to test whether a tuple is distinct or not.
Input=
(1,2,3,4)
(3,4,5,4,6,3)
Output=
Tuple is distinct
Tuple is not distinct

38). Python tuple program to convert a tuple to string datatype.
Input=
A=(4,1,7,5)
Output=
The given tuple is (4,1,7,5)

39). Python tuple program to remove empty tuples from a list of tuples.
Input=
[ (”,), (‘a’, ‘b’), (‘a’, ‘b’, ‘c’), (‘d’), () ]
Output=
[ (‘a’, ‘b’), (‘a’,  ‘b’,  ‘c’),  (‘d’) ]

40). Python tuple program to sort a tuple by its float element.
Input=
[(3,5.6),(6,2.3),(1,1.8)]
Output=
[(1,1.8),(6,2.3),(3,5.6)]

41). Python tuple program to count the elements in a list if an element is a tuple.
Input=
[1,6,8,5,(4,6,8,0),5,4)]
Output=
4

42). Python tuple program to multiply all the elements in a tuple.
Input=
(5,4,3,1)
Output=
60

43). Python tuple program to convert a string into a tuple.
Input=
“Sqatools”
Output=
(S,q,a,t,o,o,l,s)

44). Python tuple program to convert a tuple of string values to a tuple of integer values.
Input=
( ‘4563’, ’68’, ‘1,’ )
Output=
( 4563, 68, 1)

45). Python tuple program to convert a given tuple of integers into a number.
Input=
(4, 5, 3, 8)
Output=
4538

46). Python tuple program to compute the element-wise sum of tuples.
Input=
(1, 6, 7)
(4, 3, 0)
(2, 6, 8)
Output=
(7, 15, 15)

47). Python tuple program to convert a given list of tuples to a list of lists.
Input=
A=[(1,5),(7,8),(4,0)]
Output =
[ [1, 5], [7, 8], [4, 0] ]

48). Python tuple program to find all the tuples that are divisible by a number.
Input=
[(10,5,15),(25,6,35),(20,10)]
Output=
[(10,5,15),(20,10)]

49). Python tuple program to find tuples having negative elements.
Input=
[ (1, 7), (-4, -5), (0, 6), (-1, 3) ]
Output=
[(-4,-5),(-1,3)]

50). Python tuple program to find the tuples with positive elements.
Input=
[ (1, 7), (-4, -5), (0, 6), (-1, 3) ]
Output=
[ (1, 7), (0, 6) ]

51). Python tuple program to remove duplicates from a tuple.
Input=
(6, 4, 9, 0, 2, 6, 1, 3, 4)
Output=
(6, 4, 9, 0, 2, 1, 3)

52). Python tuple program to extract digits from a list of tuples.
Input=
[ (6, 87, 7), (4, 53), (11, 28, 3) ]
Output=
[ 1, 2, 3, 4, 5, 6, 7, 8 ]

53). Python tuple program to multiply ith element from each tuple from a list of tuples.
Input=
[ (4, 8, 3), (3, 4, 0), (1, 6, 2) ]
i=1
Output=
192

54). Python tuple program to flatten a list of lists into a tuple.
Input=
[ [s], [q], [a], [t], [o], [o], [l], [s] ]
Output=
(s, q, a, t, o, o, l, s)

55). Python tuple program to flatten a tuple list into a string.
Input=
[ (s, q, a), (t, o), (o, l, s) ]
Output=
‘s q a t o o l s’

56). Python tuple program to convert a tuple into a list by adding the string after every element of the tuple.
Input=
A=(1, 2, 3, 4), b=’sqatools’
Output=
[1, “sqatools”, 2, “sqatools”, 3, “sqatools”, 4, “sqatools”]

57). Write a program to convert a tuple to tuple pair.
Input=
(1, 2, 3)
Output=
[ (1, 2), (1, 3) ]

59). Python tuple program to convert a list of lists to a tuple of tuples.
Input=
[ [‘sqatools’], [‘is’], [‘best’]]
Output=
( (‘sqatools’), (‘is), (‘best’) )

60). Python tuple program to extract tuples that are symmetrical with others from a list of tuples.
Input=
[ (a, b, c), (d, e), (c, b, a) ]
Output=
(a, b, c)

61). Python tuple program to return an empty set if no tuples are symmetrical.
Input=
[(1, 5, 7), (3, 4), (4, 9, 0)]
Output=
set()

62). Python tuple program to remove nested elements from a tuple.
Input=
( ‘s’, ‘q’, ‘a’, (‘t’, ‘o’, ‘o’, ‘l’, ‘s’), ‘i’, ‘s’, ‘b’, ‘e’, ‘s’, ‘t’ )
Output=
(‘s’, ‘q’, ‘a’, ‘i’, ‘s’, ‘b’, ‘e’, ‘s’ ,’t’)

63). Python tuple program to sort a tuple by the maximum value of a tuple.
Input=
[ (1, 5, 7), (3, 4, 2), (4, 9, 0) ]
Output=
[ (4, 9, 0), (1, 5, 7), (3, 4, 2) ]

64). Python tuple program to sort a list of tuples by the minimum value of a tuple.
Input=
[(1,5,7),(3,4,2),(4,9,0)]
Output=
[(4,9,0),(1,5,7),(3,4,2)]

65). Python tuple program to concatenate two tuples.
Input=
(‘s’,’q’,’a)
(‘t’,’o’,’o,’l’)
Output=
((‘s’,’q’,’a),(‘t’,’o’,’o,’l’))

66). Python tuple program to order tuples by external list.
Input=
a=[(‘very’,8),(‘i’,6),(‘am,5),(‘happy’,0)]
List=[‘i’,’am’,’very’,’happy’]
Output=
[(‘i’,6),(‘am’,5),(‘very’,8),(‘happy’,0)]

67). Python tuple program to find common elements between two lists of tuples.
Input=
A=[(1,5),(4,8),(3,9)]
B=[(9,3),(5,6),(5,1),(0,4)]
Output=
{(3,9),(1,5)}

68). Python tuple program to convert a binary tuple to an integer.
Input=
A=(1,0,0)
Output=
4
Explanation=
2^2+0+0=4

69). Python tuple program to count the total number of unique tuples.
Input=
[ (8, 9), (4, 7), (3, 6), (8, 9) ]
Output=
3

70). Python tuple program to calculate the average of the elements in the tuple.
Input=
(5, 3, 9, 6)
Output=
5.75

71). Python tuple program to swap tuples.
Input=
A=(7,4,9)
B=(3,)
Output=
A=(3,)
B=(7,4,9)

72). Python tuple program to check the type of the input and return True if the type is a tuple and False if it is not a tuple.
Input=
A=( 7, 4, 9, 2, 0 )
Output=
True

73). Python tuple program to find the last element of a tuple using negative indexing.
Input=
A=(‘p’,y’,’t’,’o’,’n’)
Output=
n