A 'List' is a Collection that holds multiple values, of different Data Types. And in a 'List' the elements are ordered (We will explain it soon), the values are changeable and allows duplicate values.
The declaration of a 'List' 'Data Type' is super easy. You can place multiple values inside square brackets '[ ]' and Python will understand that it is a 'List'.
x = [5, "John", "Python"] print(x)
So, in the above code we have created a 'List' using square brackets '[ ]'.
And put an Integer type value (i.e. 5) and two String type value (i.e. 'John' and 'Python')
And initialised to the variable 'x'.
So, we can see that two different data types are assigned to a 'List'.
In the next line we have printed the 'List' using the print statement.
Now, if we see the output,
x = [5, "John", "Python"] for i in x: print(i)
Similarly, in the above code we have created a 'List' using square brackets '[ ]'.
And initialised to the variable 'x'.
In the next line we have used the 'for loop' to Iterate through the 'List'.
for i in x: print(i)
Now, if we see the iterations of for loop,
In the first Iteration the first value of the 'List' 'x' (i.e. 5) is taken and put into the variable 'i'.
And the print statement, prints the value of 'i'.
Similarly, in the second Iteration the second value of the 'List' 'x' (i.e. 'John') is taken and put into the variable 'i'.
And the print statement, prints the value of 'i'.
Similarly, in the third Iteration the third value of the 'List' 'x' (i.e. 'Python') is taken and put into the variable 'i'.
And the print statement, prints the value of 'i'.
Now, if you see the final output. You can find that the values of the 'List' are displayed in the same way they were inserted.
i.e. First '5' is printed, then the name 'John' and finally 'Python' is printed.
And if you see the List,
It is printed in the same order it was inserted. And this is why a List is said to be 'Ordered'.
Next, let us see, how to access the elements of the List in the next tutorial.