[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More


Sequence in Python

Published on
5,285 Points
1 Endorsement
Last Modified:
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.

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[2]

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>
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]
>>> COURSE[-2]
>>> COURSE[-3]
>>> COURSE[-4]
>>> COURSE[-5]

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]

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]

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/
Author:Swadhin Ray
LVL 48

Expert Comment

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".
LVL 17

Author Comment

by:Swadhin Ray
>> 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.

Featured Post

Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

Join & Write a Comment

Suggested Articles

Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month