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.

Find the maximum value from a tuple

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

Steps to solve the program
  1. Create a tuple.
  2. Find the maximum value from a tuple using max().
  3. Print the output.
				
					tup = (41, 15, 69, 55)
print("Maximum value: ",max(tup))
				
			

Output :

				
					Maximum value:  69
				
			
Solution2 :
				
					# take input tuple with multiple values
tup = (41, 15, 69, 101, 55)
# initiate a variable to store max value
max_val = 0
# interate through tuple values using loop
for val in tup:
    # check if val is greater than max_val
    if val > max_val:
        # assign variable value to max_val.
        max_val = val
    else:
        continue

print("maximum value :", max_val)
				
			

Output :

				
					maximum value : 101
				
			

create a tuple with 2 lists of data.

find the minimum value from a 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.