Sequence in Python

Swadhin Ray
CERTIFIED EXPERT
Published:
Updated:
Sequence is something that used to store data in it in very simple words. Let us just create a list first.
To create a list first of all we need to give a name to our list which I have taken as “COURSE” followed by equals sign and finally enclosed by square braces as like below:
 
Python 2.7.8 (default, Jun 30 2014, 16:03:49) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> COURSE = []

Open in new window


 
Now we have to add all the elements within the square brackets. So now let us add the elements within it.
 
Python 2.7.8 (default, Jun 30 2014, 16:03:49) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> COURSE = ['PYTHON', 'PERL','ORACLE','MYSQL','BASH']

Open in new window


 
After keeping the entire element that we want to be added within the list hit “enter”.
Now we have stored our list of elements with in a variable i.e. COURSE but internally python will assign numbers to each element of the list which are been stored starting with zero.
It assigns as like below order:
 
  • PYTHON  as 0 “zero”
  • PERL  as 1 “one”
  • ORACLE as 2 “two”
  • MYSQL as 3 “three”
  • BASH as 4 “four”
Even there are five elements are present but still it will start with zero and ends with four.
Sequence plays an important role because we can access member or element of the list by indexing.
 
So if we now want to access only Oracle from my list i.e. “COURSE” then I can simply call it by using indexing as like below:
 
Python 2.7.8 (default, Jun 30 2014, 16:03:49) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> COURSE = ['PYTHON', 'PERL','ORACLE','MYSQL','BASH']
>>> COURSE[2]
'ORACLE'

Open in new window


 
But the list COURSE is case sensitive.  For example if I want to access as below then I get list is undefined.
>>> course[2]

Open in new window


 
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    course[2]
NameError: name 'course' is not defined

Open in new window


 
Now in python they made the indexing in reverse way too which starts with negative (–ve) from end to beginning.  So in reverse way if we want to access then it will be like as below:
  • PYTHON  as -5 “negative five”
  • PERL  as -4 “negative four”
  • ORACLE as -3 “negative three”
  • MYSQL as -2 “negative two”
  • BASH as -1 “negative one”
 
>>> COURSE[-1]
'BASH'
>>> COURSE[-2]
'MYSQL'
>>> COURSE[-3]
'ORACLE'
>>> COURSE[-4]
'PERL'
>>> COURSE[-5]
'PYTHON'
>>> 

Open in new window


 
So list in python is indexed like two types i.e. forward starting from zero and other from backward starting with negative numbers. Stings in python can also be sequences  for example if I take a sting “sloba” then this will be indexed similarly what we have seen on index on our list “COURSE” which we have created. 
Example if we want to know what is the index 4 will be for string “sloba” :
 
>>> 'sloba' [4]
'a'

Open in new window


We see “a” because starting with zero the 4th index will be “a” for string “sloba”.
Now let us see what we are going to set when we say negative 4 for the same string.
 
>>> 'sloba' [-4]
'l'

Open in new window


We are getting letter “L” because if we start counting backward starting from negative one then -4 indexed on “L”.
For more information on sequence and list in Python visit https://www.python.org/doc/
1
2,767 Views
Swadhin Ray
CERTIFIED EXPERT

Comments (2)

aikimarkGet vaccinated; Social distance; Wear a mask
CERTIFIED EXPERT
Top Expert 2014

Commented:
You need to differentiate a sequence from a list.

Although you can address list items numerically, I do not think that there are internal number assignments made.  It is a wrong interpretation of "associated".
CERTIFIED EXPERT

Author

Commented:
>> List : A built-in Python sequence.
Reference : https://docs.python.org/3/glossary.html#term-list

But yes there is a difference. Sequence are to get the items from the order list.

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.

Get access with a 7-day free trial.
You Belong in the World's Smartest IT Community