Skip to main content


In Python, sequence is the generic term for an ordered set. There are several types of sequences in Python, the following three are the most important.
Lists are the most versatile sequence type. The elements of a list can be any object, and lists are mutable - they can be changed. Elements can be reassigned or removed, and new elements can be inserted.
Tuples are like lists, but they are immutable - they can't be changed.
Strings are a special type of sequence that can only store characters, and they have a special notation. However, all of the sequence operations described below can also be used on strings.
Sequence Operations
+ combines two sequences in a process called concatenation.
For example, [1,2,3]+[4,5] will evaluate to [1,2,3,4,5].
* repeats a sequence a (positive integral) number of times.
For example, [1,11]*3 will evaluate to [1,11,1,11,1,11].
x in mySeq will return True if x is an element of mySeq, and False otherwise. You can negate this statement with either not (x in mySeq) or x not in mySeq.
mySeq[i] will return the i'th character of mySeq. Sequences in Python are zero-indexed, so the first element has index 0, the second has index 1, and so on.
mySeq[-i] will return the i'th element from the end of mySeq, so mySeq[-1] is the last element of mySeq, mySeq[-2] is the second-to-last element, etc.
All sequences can be sliced. mySeq[1:5] will return 1,2,3 and 4 element of mySeq.
Useful Functions
len(mySeq) returns the number of elements in the sequence mySeq.
mySeq.index(x) returns the index of the first occurrence of x in mySeq. Note that if x isn't in mySeq index will return an error. (Use in with an if statement first to avoid this.)
min(mySeq) and max(mySeq) return the smallest and largest elements of mySeq, respectively. If the elements are strings this would be the first and last elements in lexicographic order (the order of words in a dictionary). Note that if any two elements in mySeq are incomparable (a string and a number, for example), min and max will return errors.
mySeq.count(x) returns the number of occurrences of x in mySeq (that is, the number of elements in mySeq that are equal to x).


Popular posts from this blog

Strings in Python

Strings are compound data type made of sequence of characters. Hence from the string, the individual charters can also be directly accessed. Strings are created by using single quotes or double quotes or triple quotes.
>>>s1="Python Programming"
>>>s2='Python Programs'
>>>s3="""Python is a powerful
Programming language"""

This will create a string object s1, s2 and s3 using different forms.
Individual characters can be accessed using different subscripts( +ve or –ve) with the string object.
We can also slice a string. Segment of a string is called a slice.
If the first index is not mentioned, the slicing will start from 0.
If the last index is not mentioned, the …

Classes and Objects in Python

Python is an object-oriented programming language, which means that it provides features that support object-oriented programming. The basic components of object oriented programming are classes and objects. A Class is a blue print to create an object. It provides the definition of basic attributes and functions of objects. Object is a running instance of the class having the identity(name), properties( values) and behaviors(functions). The Object oriented program thus consist of object definitions (classes) and most of the computations and functions are mentioned as operations on the object. Each object definition corresponds to some object or concept in the real world, and the functions that operate on these object correspond to the ways real-world objects interact. We have learned objects of string, list, tuple etc…and used the properties and functionalities of these objects which are built into the Python. Now we are going to create our own(user defined) objects. The class provides basic…

Files in Python , Exception handling

While a program is running, its data is in main memory. When the program ends, or the computer shuts down, data in memory disappears. To store data permanently, you have to put it in a file. Files are usually stored on a secondary storage device(hard disk, pen drive, DVD,CD etc). 
When there are a large number of files, they are often organized into directories (also called “folders”). Each file is identified by a unique name, or a combination of a file name and a directory name. 
By reading and writing files, programs can exchange information with each other and generate printable formats like PDF. Working with files is a lot like working with books. To use a book, you have to open it. When you’re done, you have to close it. While the book is open, you can either write in it or read from it. In either case, you know where you are in the book. Most of the time, you read the whole book in its natural order, but you can also skip around. All of this applies to files as well. 
To open a…