Sequences in Python

Sequences are those types that support Indexing and Slicing. In that respect, strings, unicodes, lists, tuples, buffers and xrange are all sequence types

Indexing allows you to fetch a particular item in the sequence directly

Slicing operation which allows us to retrieve a slice of the sequence i.e. a part of the sequence

For example:

t = 12345, 54321, ‘hello!’

>>> t

(12345, 54321, ‘hello!’)

>>> t[0]


>>> t[:-1]

(12345, 54321)

Dictionary in Python

Values are unordered pair of key-value pairs

Each value has a corresponding key. Thus there exists a one-to-one relation between them

You cannot get a key by value, as otherwise

Keys are immutable but values can either be immutable or mutable

Dictionary cannot have a duplicate key thus a one-to-one relation must always exist between key and value. Assigning a value to an existing key will wipe out the old value

>>> d = {“Hello”:10, “world”:20}

>>> d

{‘world’: 20, ‘Hello’: 10}

>>> d[“Hello”]


>>> d[“Hello”] = 30

>>> d

{‘world’: 20, ‘Hello’: 30}


  • Dictionaries support the following in-built methods, but are not limited to these:

d[k] retrieves a value from a dictionary

d[k]=x sets a value in the dictionary

d.has_key(k) tests for the presence of a keyword

d.get(k, d) returns a default if a key is not found

d.keys() returns a list of keys from a dictionary

d.values() returns a list of values

Tuple Concept in Python (Similar to List)

It is like a list with all comma separated elements enclosed in a pair of parenthesis.

It is an immutable list. A Tuple can not be changed in any way once it is created.

>>> t = (“Hello”, 10, “world”, 20) >>> t (‘Hello’, 10, ‘world’, 20)

Like a list, all its elements are ordered. First element starts at index 0.

>>> t[0]



Negative indices count from the end of the tuple, just like a list

>>> t[-1]


Slicing also works for tuple same as it works for list.

>>> t[1:3]

(10, ‘world’) # creates a new tuple


Unlike list, they have no in-built methods. So you cannot add, remove, index elements in a tuple.


Thus, they are truly immutable in a way that lets you write create write-protect data.

List concept in Python

It is a list of comma separated values in square brackets. >>> x = [‘Hello’, 10, ‘World’, ’20’] >>> print x [‘Hello’, 10, ‘World’, ’20’]

First element in the list starts at 0 index. >>> x[0] ‘Hello’

>>> type(x) # function type() specifies type of an object.

<type ‘list’> # x is of type list


>>> x[-1]


>>> x[-2]


List can have items of different types unlike in C/C++ where we can have array of only single type.

Lists can be sliced, concatenated and so on: >>> x[:2] #list sliced to display first two elements

[‘Hello’, 10]


>>> x[:-1]

[‘Hello’, 10, ‘World’] # list sliced to drop 2 elements

>>> x[:1] + [‘Good Morning’] # Concatenate 2 lists

[‘Hello’, ‘Good Morning’]

Apart, from this you have several other in-built methods like append, count, extend, reverse, pop, etc to add, delete, count items, etc.

>>> x.append(40)

>>> x

[‘Hello’, 10, ‘World’, ’20’, 40]

• Unlike strings, which are immutable, it is possible to change individual elements of a list:

>>> a

[‘Hello’, 10, ‘World’, ’20’]

>>> x[1] = x[1] + 23

>>> print x [‘Hello’, 33, ‘World’, ’20]

Strings in Python

Strings are immutable. They can be expressed in several ways in single quotes or double quotes:

>>> ’Hello World’ ’Hello World’ # using single quote

>>> “doesn’t” “doesn’t” # using double quotes

>>> ’”Yes,” he said.’ # A mix of both ’”Yes,” he said.’

>>> “\”Yes,\” he said.” ’”Yes,” he said.’

>>> u’Hello World’ # Creates a unicode string u’Hello World’

>>> “””Hello World””” # Using triple quote ‘Hello World’

>>> ”’Hello World”’ ‘Hello World’

The small ‘u’ in front of the quote indicates that a Unicode string is to be created


>>> r’Hello \n World’             # Creates a raw string that do

# not give special attention to

# escape sequences.

‘Hello \\n World’

Python Data Types

Python supports two basic data types: Numbers and Strings:





Python supports four numeric types that all are immutable:


>>> a = 10 >>> b = 20 >>> a * b 200

Long Integer


>>> a = 2.5 >>> b = 1.5 >>> a / b 1.6666666666666667


>>> x = 1 + 2j >>> x.real 1.0 >>> x.imag 2.0

Some Basics of Python

Python binds the name to the expression’s value

Python is loosely typed in a way that names are not required to be declared for their types. You can simply assign any type of data to a single variable

For example:

>>> x = 1 # x is a name bound to value 1

>>> x = ‘Hello’ # same name now stores a string

>>> print x



Python is a strong object oriented in a way that everything is an object; be it is a string, number, function, exception, etc. everything is just an object.

Choice of Editors / IDEs

There are numerous editors or IDEs that one can use for developing python programs.

On install, Python can be started easily by typing ‘python’ on the shell. On successful invocation of python a prompt >>> is displayed which can then be used to submit python commands/programs to python for running.

To exit this python shell, you can press Ctrl + z button and press Enter.

Active State’s python has a nice Python Editor that you can use to edit python programs. This editor also has a python shell where these programs are run. Moreover, this editor has a nice word auto-completion facility that can help you determine the possible attributes a module is exporting.

There are similar other python editors like UliPad (also provides a python shell), Crimson Editor, etc. Crimson editor is just a generic text editor much like TextPad and has no way to launch python shell for running programs.

The one IDE I like the most is Eclipse Ganymede. This is a generic SDK that can be complemented with PyDev Plug-in to enable Python and Jython editor. It comes with many goodies such as code completion, syntax highlighting, syntax analysis, refactor, debug and many others.

I use PythonWin or python shell alone for short test programs. For my run time environment, I prefer Eclipse

Python list of editors or IDEs is very long. Most of them are freeware / shareware.

Installable / Source Code

The Installable or the source code of Python for various platforms can be sought either from:


Both the sites have vast documentation of library modules.