We are going to learn a data structure called list before we go ahead to learn more on looping. Lists an be written as a list of comma-separated values (items) between square brackets.
>>> a = [ 1 , 342, 2233423, 'India', 'Fedora'] >>> a [1, 342, 2233423, 'India', 'Fedora']
Lists can keep any other data inside it. It works as a sequence too, that means
>>> a[0] 1 >>> a[4] 'Fedora'
You can even slice it into different pieces, examples are given below
>>> a[4] 'Fedora' >>> a[-1] 'Fedora' >>> a[-2] 'India' >>> a[0:-1] [1, 342, 2233423, 'India'] >>> a[2:-2] [2233423] >>> a[:-2] [1, 342, 2233423] >>> a[0::2] [1, 2233423, 'Fedora']
In the last example we used two :(s) , the last value inside the third brackets indicates step. s[i:j:k] means slice of s from i to j with step k.
To check if any value exists within the list or not you can do
>>> a = ['Fedora', 'is', 'cool'] >>> 'cool' in a True >>> 'Linux' in a False
That means we can use the above statement as if clause expression. The built-in function len() can tell the length of a list.
>>> len(a) 3