Sequence in Python

Published on
5,111 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 47

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Join & Write a Comment

Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
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