Write A Python Script To Create A Tuple With Different Data Types

In this article, let's look at how to write a  Python program to create a table containing multiple data types.  The multiple data types are integer string floating point value  Complex data type and  Boolean values.

 Let's create an Empty double and let's try to ask a user to enter all the data types  as  input to the Python program.

 

Write A Python Script To Create A Tuple With Different Data Types

 

 


Write A Python Script To Create A Tuple With Different Data Types

 first take a variable named as my_tuple and initialize it to an Empty tuple.  Now ask a user to enter an integer and store it into a variable named as number and append it to my_tuple.

my_tuple = tuple()

number = int(input("Enter a Number : "))
my_tuple += (number,)




Now take another variable named as text and ask a user to enter a string and then append to a my_tuple using +.  Similarly take another variable named as fraction and ask a user to enter a float and append to my_tuple.

text = input("Enter a String : ")
my_tuple += (text,)

fraction = float(input("Enter a Floating Point Number : "))
my_tuple += (fraction,)




Take another variable named complex_number and ask the user to enter a complex number appended to  tuple.  Similarly, take a Boolean variable and ask the user to enter a Boolean value that is true or false and append it to a tuple.

complex_no = complex(input("Enter a Complex Number (eg. a+bj): "))
my_tuple += (complex_no,)

flag = bool(input("Enter a Boolean (eg. True/ False) : "))
my_tuple += (flag,)





Once all the file data types are present in the tuple using the print statement and follow print all the data types of tuple as shown below.

print("Tuple containing multiple data types are ..")
for item in my_tuple:
   print(item)
 
 
 
 
Output
=============

Enter a Number : 34
Enter a String : hello
Enter a Floating Point Number : 12.56
Enter a Complex Number (eg. a+bj): 4+5j
Enter a Boolean (eg. True/ False) : False
Tuples containing multiple data types are ..
34
hello
12.56
(4+5j)
True




Python Code
=============================


my_tuple = tuple()

number = int(input("Enter a Number : "))
my_tuple += (number,)

text = input("Enter a String : ")
my_tuple += (text,)

fraction = float(input("Enter a Floating Point Number : "))
my_tuple += (fraction,)

complex_no = complex(input("Enter a Complex Number (eg. a+bj): "))
my_tuple += (complex_no,)

flag = bool(input("Enter a Boolean (eg. True/ False) : "))
my_tuple += (flag,)

print("Tuples containing multiple data types are ..")
for item in my_tuple:
   print(item)

 


Conclusion
===================

Execute the above Python program by providing multiple data type inputs like integer floating point then string Complex type and Boolean values as a input note down the results

Comment down below  if you have any suggestions to improve the above Python program.  



Post a Comment

0 Comments