Create a tuple having squares of the elements from the list

In this python tuple program, we will create a tuple having squares of the elements from the list.

Steps to solve the program
  1. Create a tuple and convert it to a list using list().
  2. Use for loop to iterate over elements in the list.
  3. Find the index of the element using index().
  4. Get the square of the element.
  5. Now replace the square with the old element in the list.
  6. Now convert that list to a tuple using tuple().
  7. Print the output.
				
					tup = (1,3,5,7,6)
print("Origianl tuple: ",tup)
a = list()
for i in list(tup):
    b = i**2
    a.append(b)
tup = tuple(a)
print("After sqauring elements: ",tup)
				
			

Output :

				
					Origianl tuple:  (1, 3, 5, 7, 6)
After sqauring elements:  (1, 9, 25, 49, 36)
				
			

multiply adjacent elements of a tuple.

Python tuple program to add a list in the tuple

In this python tuple program, we will add a list in the tuple.

Steps to solve the program
  1. Create a list and a tuple.
  2. Convert the tuple into a list using list() and add another list to it using+ “.
  3. Now convert the combined list to a tuple using tuple().
  4. Print the output.
				
					list1 = [12,67]
tup = (6,8,4)
result = tuple(list(tup) + list1)
print(result)
				
			

Output :

				
					(6, 8, 4, 12, 67)
				
			

check whether an element exists in a tuple or not.

find sum of elements in a tuple.

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.