Can a list contain items of different types?

Question

Is it possible to put different types of data in the same list variable?

Answer

A list in Python CAN contain different types of data. Each item in the list is separated by a comma and the entire list is enclosed in square brackets []. To add a new item to the list, just edit the list starting before the closing bracket. Add a comma and the new item. It doesn’t matter what the type is for the previous item.

In the example below, the elements list contains the name of an element and its atomic number and mass. The list then contains the same data for another element. The data types contained in the list are strings, integers, and floating point values

elements = [‘Hydrogen’, 1, 1.007825, ‘Helium’, 2, 4.00260]
13 Likes

[‘Hydrogen’, 1, 1.007825, ‘Helium’, 2, 4.00260, True]

We can also use Boolean values inside list.

4 Likes

All Python data types can be used and stored in lists :slight_smile:

2 Likes

Can i store a math problem within a list

eg:

[10, 20, 30, 40*10, 100]

Short answer: Yes, we can assign expressions of any form that can be evaluated.

3 Likes