Introduction to Lists in Python

26-02-23 Ahmed Obaid 1802 0

Lists can be defined as a collection of values ​​or items from different types of data. Python lists are of a mutable type which means that we may modify its element after it is included. The items in the list are separated by a comma ( , ) and enclosed in square brackets [ ]. Lists are used in Python to store sequences of different types of data. Each element of the sequence is assigned a number - its position or index. The first index is zero, the second index is one, and so on.

List properties in Python

The list contains the following properties:


  • The items in the list are separated by a comma ( , ) and enclosed in square brackets [ ]

  • List item can be accessed by index.

  • Menu items are subject to change. Which means we can modify its elements

  • The list can store different types of data. Such as integers, strings, and objects.



Lists example in Python

In the following example, we will define a variable called list that contains a list of texts.

Example:

# Define a variable that contains a list containing only strings
list = ['Ahmed ', 'Obaid']
# Print the listundefined print(list)

 The output will be:

 ['Ahmed', 'Obaid']

 In the following example, we will define a variable named list that contains a list of integers.

 Example:

 # Define a variable that contains a list containing only integers
 list = [1, 2, 3, 4, 5, 6, 7]
 # Print the list
 print(list)

 The output will be:

 [1, 2, 3, 4, 5, 6, 7]

 In the following example, we will define a variable called list that contains a list of integers and strings.

 Example:

# Define a variable that contains a list containing integers and strings
 list = [55 , "Python" , 20]
 # Print the list
 print(list)

 The output will be:

 [55, 'Python', 20]

 External sources:

 Built-in functions - official Python documentation

 If you have any questions or concerns, leave them in the comments



Tags


Python python data types python lists

Share page