Write A Python Code To Create A List With Mixed Data Types

Given the list of multiple data types you have to write a python code that will accept all the multiple data types and store it into a Python list.  The multiple data types are in integer, floating point number, string,  Boolean value and complex number.

Let's accept all the  multiple data types from the user  and make a list of  mixed data types.

 

 

Write A Python Code To Create A List With Mixed Data Types

 



Write A Python Code To Create A List With Mixed Data Types

Let's take a variable named as my_list and initialize it to an empty list,  take a new variable named as number and ask the user to enter the number using the if function call and then convert it into an integer.

my_list = []
number = int(input("Enter a Number : "))
my_list.append(number)





Take another variable named as fraction and ask a user to enter a floating point number using input function call and then convert it into a float and then open the fraction to the my_list using append function call. Take a string variable and then using input function call accept the string and append it to  my_list.

fraction = float(input("Enter a Floating Point Number : "))
my_list.append(fraction)

text = str(input("Enter a String : "))
my_list.append(text)





Take another variable named as flag, use this variable to store the Boolean value and once the user enters the Boolean value true or false appended to the underscore list try the same with complex numbers.

flag = bool(input("Enter a Boolean (True / False) : "))
my_list.append(flag)

complex_number = complex(input("Enter a Complex Number : "))
my_list.append(complex_number)





Once the user enters multiple types of input then using the  for loop  iterate all the items present in the list and then print it.

print("List With Mixed Data Types")
for item in my_list:
   print(item)





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


my_list = []

number = int(input("Enter a Number : "))
my_list.append(number)

fraction = float(input("Enter a Floating Point Number : "))
my_list.append(fraction)

text = str(input("Enter a String : "))
my_list.append(text)

flag = bool(input("Enter a Boolean (True / False) : "))
my_list.append(flag)

complex_number = complex(input("Enter a Complex Number : "))
my_list.append(complex_number)

print("List With Mixed Data Types")
for item in my_list:
   print(item)





Output
===============
Enter a Number : 12
Enter a Floating Point Number : 45.33
Enter a String : hello
Enter a Boolean (True / False) : False
Enter a Complex Number : 3+4j
List With Mixed Data Types
12
45.33
hello
True
(3+4j)





Output
=============
Enter a Number : 68
Enter a Floating Point Number : 90.05
Enter a String : hi
Enter a Boolean (True / False) : True
Enter a Complex Number : 2+3j
List With Mixed Data Types
68
90.05
hi
True
(2+3j)





Conclusion
=================
Execute the above program by  providing multiple data types as input to the program and note down the results.

Comment down below if you have any suggestions to improve our Python program






Post a Comment

0 Comments