Check whether an element exists in a tuple or not

In this python tuple program, we will check whether an element exists in a tuple or not.

Steps to solve the program
  1. Create a tuple.
  2. Use an if-else statement to check whether an element exists in the tuple.
  3. Print True if the element exists in the tuple else print False.
				
					tup = ('p','y','t','h','o','n')
if 'p' in tup:
    print("True")
else:
    print("False")
				
			

Output :

				
					True
				
			

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

add a list in the tuple.

Python tuple program to get elements from the tuple using indexing

In this python pandas program, we will get elements from the tuple using indexing.

Steps to solve the program
  1. Create a tuple.
  2. Get the 2nd element from the front using tuple_name[1].
  3. Get the 3rd element from the last using tuple_name[-3].
  4. Print the output.
				
					tup = ('s','q','a','t','o','o','l','s')
print("2nd element from the front in the String: ",tup[1])
print("3rd element from the last in the String: ",tup[-3])
				
			

Output :

				
					2nd element from the front in the String:  q
3rd element from the last in the String:  o
				
			

convert a tuple into a string.

check whether an element exists in a tuple or not.

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.

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.