Write A Python Program To Create A List With Different Data Types

Hi guys, in this article you will learn multiple data types that are supported in the Python program.  There are 6  basic data types that are supported in the Python program They are as listed below

The basic data types present in Python

  1. None
  2. Integer
  3. Floating point
  4. String
  5. Boolean value
  6. Complex data type


Let's accept these six data types as input to the Python program and  store into the list and demo the  python data types.

 

Write A Python Program To Create A List With Different Data Types

 

Write A Python Program To Create A List With Different Data Types

Initially let's take a list variable named data_types and assign an empty list to it. As the list is empty firstly happened a non type to it then ask the user to enter an integer and append and integer data type to it.

data_types = []

print("Firstly Appending None Type")
data_types.append(None)

integer = int(input("Enter an Integer : "))
data_types.append(integer)





Similarly take a fraction to show the floating point value,  text to store the  value,  A variable to store the boolean value, And finally a complex variable to store the complex number.  append all these variables to the list.

fraction = float(input("Enter an Floating Point Value : "))
data_types.append(fraction)

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

boolean = bool(input("Enter an Boolean Value (True/ False) : "))
data_types.append(boolean)

complex_no = complex(input("Enter Complex Data Type (eg 3+4j) : "))
data_types.append(complex_no)

 


Once you append all the data types to the list use the print function to print the multiple data types containing the list.

print("The list containing multiple data types are ...")
print(data_types)




Complete Python program
=========================

data_types = []

print("Firstly Appending None Type")
data_types.append(None)

integer = int(input("Enter an Integer : "))
data_types.append(integer)

fraction = float(input("Enter an Floating Point Value : "))
data_types.append(fraction)

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

boolean = bool(input("Enter an Boolean Value (True/ False) : "))
data_types.append(boolean)

complex_no = complex(input("Enter Complex Data Type (eg 3+4j) : "))
data_types.append(complex_no)

print("The list containing multiple data types are ...")
print(data_types)





Output
============

Firstly Appending None Type
Enter an Integer : 23
Enter an Floating Point Value : 4.5
Enter a String : hello
Enter an Boolean Value (True/ False) : False
Enter Complex Data Type (eg 3+4j) : 2+1j
The list containing multiple datatypes are ...
[None, 23, 4.5, 'hello', True, (2+1j)]



Output 2
==============

Firstly Appending None Type
Enter an Integer : 30000
Enter an Floating Point Value : 23.4598374
Enter a String : how are you
Enter an Boolean Value (True/ False) : True
Enter Complex Data Type (eg 3+4j) : -1+4j
The list containing multiple data types are ...
[None, 30000, 23.4598374, 'how are you', True, (-1+4j)]




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

The above Python program takes an empty list of data types and as the user to enter all the six data types they are integer floating point string Boolean and complex data types and finally the  program will print the. List

Execute the above Python program by providing random data type values as input to the Python program and not down the results.

Comment it down below if you have any queries regarding the above Python program



Post a Comment

0 Comments