Python tuple program to compute the element-wise sum of tuples

In this python tuple program, we will compute the element-wise sum of tuples.

Steps to solve the program
  1. Take 3 tuples as input.
  2. Use the map() function and inside the map function use sum and combine the tuples using zip() to compute the element-wise sum of tuples.
  3. Convert the output from the map function to a tuple using tuple().
  4. Print the output.
				
					a = (1, 6, 7)
b = (4, 3, 0)
c = (2, 6, 8)
print(f"Original tuples: {a},{b},{c}")
s1 = map(sum, zip(a,b,c))
result = tuple(s1)
    
print("Element wise sum of the given tuples: ",result)
				
			

Output :

				
					Original tuples: (1, 6, 7),(4, 3, 0),(2, 6, 8)
Element wise sum of the given tuples:  (7, 15, 15)
				
			

convert a given tuple of integers into a number.

convert a given list of tuples to a list of lists.

convert a given tuple of integers into a number

In this python tuple program, we will convert a given tuple of integers into a number.

Steps to solve the program
  1. Take a tuple of integers as input and create an empty string.
  2. Use a for loop to iterate over integers from the tuple.
  3. Add integers to the string after converting them to string datatype using str(), so that we can convert them into a single number.
  4. Print the output.
				
					tup = (4,5,3,8)
str1 = ""
print("Original tuple: ",tup)
for val in tup:
    str1 += str(val)
num = int(str1)
print("Integer: ",num)
				
			

Output :

				
					Original tuple:  (4, 5, 3, 8)
Integer:  4538
				
			

convert a tuple of string values to a tuple of integer values.

compute the element-wise sum of tuples.

Convert a tuple of string values to a tuple of integer values

In this python tuple program, we will convert a tuple of string values to a tuple of integer values.

Steps to solve the program
  1. Take a tuple of string values as input.
  2. Create an empty list.
  3. Use for loop to iterate over each value from the tuple.
  4. Add the value to the list using append() after converting it to the integer using int().
  5. Print the output.
				
					tup = ('4563','68','1',)
print("Original tuple: ",tup)
l = []
for val in tup:
    if type(val) is str:
        l.append(int(val))
tup = tuple(l)
print("After converting to integers: ",tup)
				
			

Output :

				
					Original tuple:  ('4563', '68', '1')
After converting to integers:  (4563, 68, 1)
				
			

convert a string into a tuple.

convert a given tuple of integers into a number.

Python tuple program to convert a string into a tuple

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

Steps to solve the program
  1. Take a string as input.
  2. Now convert the string into a tuple using tuple().
  3. Print the output.
				
					str1 = "Sqatools"
print("String: ",str1)
tup = tuple(str1)
print("After converting string to a tuple: ",tup)
				
			

Output :

				
					String:  Sqatools
After converting string to a tuple:  ('S', 'q', 'a', 't', 'o', 'o', 'l', 's')
				
			

multiply all the elements in a tuple.

convert a tuple of string values to a tuple of integer values.

Python tuple program to multiply all the elements in a tuple

In this python tuple program, we will multiply all the elements in a tuple.

Steps to solve the program
  1. Take a tuple as input.
  2. Create a variable named product and assign its value equal to 1.
  3. Use a for loop to iterate over each number in the tuple.
  4. During iteration multiply the product variable with each number in the tuple.
				
					tup = (5,4,3,1)
print("Origianl tuple: ",tup)
product = 1
for val in tup:
    product *= val
print("Multiplication of elements in the tuple: ",product)
				
			

Output :

				
					Origianl tuple:  (5, 4, 3, 1)
Multiplication of elements in the tuple:  60
				
			

count the elements in a list if an element is a tuple.

convert a string into a tuple.

Program to count the elements in a list if an element is a tuple

In this python tuple program, we will count the elements in a list if an element is a tuple.

Steps to solve the program
  1. Take a list as input.
  2. Use a for loop to iterate over each element in the list.
  3. If the type of an element is a tuple then count the number of elements in it and print the output.
  4. Use type() and len() for this purpose.
  5. Print the output.
				
					l = [1,6,8,5,(4,6,8,0),5,4]
print("Original list: ",l)
for val in l:
    if type(val) is tuple:
        print(f"Elements in the tuple {val}: ",len(val))
				
			

Output :

				
					Original list:  [1, 6, 8, 5, (4, 6, 8, 0), 5, 4]
Elements in the tuple (4, 6, 8, 0):  4
				
			

sort a tuple by its float element.

multiply all the elements in a tuple.

Python tuple program to sort a tuple by its float element

In this python tuple program, we will sort a tuple by its float element.

Steps to solve the program
  1. Take a list of tuples having float elements in it as input.
  2. We can see that inside each tuple float element is at index 1 in each tuple.
  3. Sort the tuple by its float element using sorted().
  4. Inside sorted pass the lambda function to the key to sort the tuple by float element i.e. sorted(tup,key = lambda x: float(x[1])).
  5. Print the output.
				
					l = [(3,5.6),(6,2.3),(1,1.8)]
print("Original list of tuples: ",l)
l1 = sorted(tup,key = lambda x: float(x[1]))
print("After sorting: ",l1)
				
			

Output :

				
					Original list of tuples:  [(3, 5.6), (6, 2.3), (1, 1.8)]
After sorting:  [(1, 1.8), (6, 2.3), (3, 5.6)]
				
			

remove empty tuples from a list of tuples.

count the elements in a list if an element is a tuple.

Python tuple program to remove empty tuples from a list of tuples

In this python tuple program, we will remove empty tuples from a list of tuples.

Steps to solve the program
  1. Take a list of tuples as input.
  2. Use for loop to iterate over each tuple.
  3. If the length of the tuple is equal to 0 i.e. tuple is empty then remove that tuple from the list using remove().
  4. Print the output.
				
					l = [(" ",),('a','b'),('a','b','c'),('d',),()]
print("Original list of tuples: ",l)
for tup in l:
    if len(tup) == 0:
        l.remove(tup)
print("After removing empty tuples: ",l)
        
				
			

Output :

				
					Original list of tuples:  [(' ',), ('a', 'b'), ('a', 'b', 'c'), ('d',), ()]
After removing empty tuples:  [(' ',), ('a', 'b'), ('a', 'b', 'c'), ('d',)]
				
			

convert a tuple to string datatype.

sort a tuple by its float element.

Python tuple program to convert a tuple to string datatype

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

Steps to solve the program
  1. Take a tuple input.
  2. Convert the tuple to a string datatype using str().
  3. Print the output.
				
					tup = (4,1,7,5)
print("Original tuple: ",tup)
tup1 = str(tup)
print("After converting to a string: ",tup1)
print("Type of new tuple: ",type(tup1))
				
			

Output :

				
					Original tuple:  (4, 1, 7, 5)
After converting to a string:  (4, 1, 7, 5)
Type of new tuple:  <class 'str'>
				
			

test whether a tuple is distinct or not.

remove empty tuples from a list of tuples.

Python tuple program to test whether a tuple is distinct or not

In this python tuple program, we will test whether a tuple is distinct or not.

Steps to solve the program
  1. Take a tuple as input.
  2. Create a set of input tuple it will remove the duplicate elements.
  3. Now check whether the length of the tuple is equal to the length of the new tuple i.e. length of set of tuple using an if-else statement.
  4. If they are equal then the tuple is distinct else the tuple is not distinct.
  5. Print the output.
				
					tup = (1,2,3,4)
if len(set(tup)) == len(tup):
    print("Tuple is distinct")
else:
    print("Tuple is not distinct")
				
			

Output :

				
					Tuple is distinct
				
			
				
					tup1 = (3,4,5,4,6,3)
if len(set(tup1)) == len(tup1):
    print("Tuple is distinct")
else:
    print("Tuple is not distinct")
				
			

Output :

				
					Tuple is not distinct
				
			

find values of tuples at ith index number.

convert a tuple to string datatype.