Sequences in Python

Posted by Infocampus HR on January 3rd, 2019

Sequences are the general term for ordered sets. There are seven types of sequences in Python. These are:

  •         Unicode string
  •         Strings
  •         Lists
  •         Tuples
  •         Byte arrays
  •         Buffers
  •         Xrange objects

Out of those seven, 3 are the foremost popular. These 3 are:

  •         Lists
  •         Tuples
  •         Strings

In this Sequences in Python article, we shall mention every of those sequence types in details, show however these Python Training in Bangalore  are utilized in python programming and provide relevant examples. Sequences are the essential building block of python programming and are used on a daily basis by python developers.

For new python developers and learners, this text should produce essential learning objective, for established programmers, this might be a revision module.

Main concept Of Sequences in Python

Among all sequence types, Lists are the foremost versatile. a list component can be any object. Lists are changeable which implies they will be modified. Its components will be updated, removed, and also components can be inserted into it.

Tuples are also like lists, however there's one distinction that they're immutable which means they cannot be modified when outlined.

Strings are very little completely different than list and tuples, a string will solely store characters. Strings have a special notation.

Following are the operations that may be performed on a sequence:

•+ operator combines two sequences in a method. it's also called concatenation. as an example, [1,2,3,4,5] + [6,7] can evaluate to [1,2,3,4,5,6,7].

•* operator repeats a sequence a defined range of times. as an example, [1,22]*3 can evaluate to [1,22,1,22,1,22].

•NewSeq[i] returns the i’th character of NewSeq. Sequences in Python are indexed from zero, therefore the initial element’s index is zero, the second’s index is one, and so on.

•NewSeq[-i] returns the i’th component from the top of NewSeq, thus NewSeq [-1] can the last component of NewSeq, NewSeq [-2] are the second -last component.

•All sequences in python will be sliced.

Useful Functions on a sequence:

•len(NewSeq): This returns the quantity of components within the sequence NewSeq. Len stands for length.

Searching on sequences in Python:

•index(x): can return the index of x’s initial incidence. If there's no x in NewSeq index, it'll throw an error. This error will be handled by a if statement. it will be used to skip this.

•min(NewSeq) and max(NewSeq): can return the smallest and largest components severally of NewSeq. For string, this order are in Best Institute For Python Training in Marathahalli  an exceedingly wordbook order. If any 2 components in NewSeq are matchless as an example one a string and another variety, then min and max can throw errors.

•count(x): can come back the quantity of occurrences of x in NewSeq.

A string is painted in single or double quotes: ‘xyz’, “foo-bar”.

Unicode strings are kind of like strings however are specified employing a preceding “u” character within the syntax: u’abcd’, u”defg”.

Lists are represented/created with square brackets with every item separated victimization commas. Example: -[a, b, c, d].

Tuples are created by the comma operator, however they're not inside square brackets. introduction parentheses are optional in tuples. However, an empty tuple should use an introduction parenthesis. Example: – a, b, c or ().Ex: – (d,).

Buffer objects too haven't any inbuilt Python syntax, and frequently, it's created victimization the inbuilt perform buffer (). Buffers don’t support operations like concatenation or repetition.

Xrange objects are once more like buffers. there's no specific syntax for Xrange additionally. they will be created victimization the xrange() perform. They too, don't support operations like slicing, concatenation or repetition. Use of in, not in, min() or max() on Xrange is additionally inefficient.

Among operations that are supported by most sequence varieties, “in” and “not in” operations have equal priority because the comparison operations, and “+” and “*” operations have the equal priority because the corresponding numeric operations.

Sequences in Python

In this section, we have a tendency to shall demonstrate samples of sequences in python: –

•String:

Slicing and dicing and indexing a string.

•List:

Defining a list and indexing and appending it.

•Tuples:

Various operations on a tuple.

Apart from these, there are several different strategies and functions ar offered that may be enforced on strings, lists, and tuple etc. Some such strategies for strings are given below: –

• Capitalize ()

• Center(width[, fillchar])

• count(sub[, start[, end]])

• decode([encoding[, errors]])

• encode( [encoding[,errors]])

• endswith( suffix[, start[, end]])

• expandtabs( [tabsize])

• find( sub[, start[, end]])

• index( sub[, start[, end]])

• isalnum( )

• islower( )

• isupper( )

• join( seq)

• replace(old, new[, count])

• startswith( prefix[, start[, end]])

• swapcase( )

Details regarding these functions are provided in succeeding articles.

Conclusion – Sequences in Python

it's  expected that students perceive the foundations of sequences and should follow given examples on a python IDE or console. From here, students will get ahead with their journey of python programming and if needed rummage around for extra follow material on an online or in python follow books. Python language is extremely abundant in demand today and having smart foundational understanding will profit students a lot in their future endeavors.

Like it? Share it!


Infocampus HR

About the Author

Infocampus HR
Joined: December 10th, 2016
Articles Posted: 792

More by this author