Sequence in Python

This post is been taken from my article which is published by Experts-Exchange.
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 = []
  

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']
  

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'
  

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

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

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'
>>> 
  

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'

  

 

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'

  

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/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s