You are on page 1of 2

How To WordPress PHP Laravel ReactJS Python Git jQuery Tips & Tricks

HOME
PYTHON FULL GUIDE PYTHON SEQUENCES – TYPES, OPERATIONS, AND FUNCTIONS

Full Guide Python Sequences – Types,


Operations, and Functions
PYTHON

AMAN MEHRA
SEPTEMBER 6, 2021
LEAVE A COMMENT

Tweet on Twitter
Share on Facebook

FOLLOW US
Stay updated via social channels

Get New Post By Email


Name

Your email address

SUBSCRIBE
In this tutorial, we’ll learn what python sequences are? How do they interact with
python and how we can use sequences in python?

Sequences are one of the most core topics in any programming language
RECENT POSTS
specifically in python because python allows itself to be more readable.
How to Convert Html to PDF using
Sequences play an important role in writing code that’s easily readable.
JavaScript?

So let’s take a look at what sequences are and how we can use them in python. How to Add Password Strength
Meter in WooCommerce?

So sequences in python are a collection of elements, we will learn all about this How to Change Default Product
and try to understand what can you do with those sequences? Then we will look Sorting in WooCommerce?

at what are the operations that we can perform on sequences. 100+ Free High DA PA Web 2.0
Sites List
We’ll also take a look at the types of python sequences, sequence operations,
Top Social Bookmarking Sites List
and sequence functions. What are the uses of them and how we can use them? – High DA, PA & Dofollow
We will cover all details in this tutorial.

CATEGORIES

Git

How To
let’s get started.
JavaScript

jQuery
Table of Contents
Laravel
1
What are Sequences in Python?
2
Types of Python Sequences PHP
3
Python Sequence Operations Python
4
Python Sequence Functions
ReactJS

SEO

What are Sequences in Python? Themes

WooCommerce
A sequence is a generic term used to define a collection of elements in which
order is important. WordPress

So a sequence is basically just a term that is assigned to refer to a collection of


elements in which order is important to order. Meaning if something is added MOST VIEWED POSTS
first and then something else is added after it, the order in which these two
WooCommerce Remove Update
things were added is preserved. Cart Button and Make it
Automatically Update.
So if you would be asking from that sequence, give me the first item. So it won’t
How to Change Price of Specific
randomly select any item. It will give you the item that was added right at the
Product and Quantity in Cart?
beginning.
How to Redirect to Checkout After
Now again there are other operations and functions that you can perform on it Add to Cart?

as well. Which we’ll take a look at but still you need to remember the order is Upload Multiple Featured Images
very important. in a Post OR Page

How can I Prevent SQL Injection in


It is also to be noticed that this collection of elements are not homogeneous.
PHP?
This basically means that you can have a collection of elements where each
element is of a different type.

TIPS & TRICKS


You can store a number, you can store an object, you can store a string then
you can store some other object, you can store a float number, which is a Python If Conditions Using all()
fractional number and you could store many different kinds of things there as and any() Methods

well. Loading Spinner in React JS

What is React.PureComponent?
So it’s not necessarily the case that you need to store just one particular kind of
data in all the sequences. So you can use that to your advantage and just make Laravel Model boot() Method

sure that you understand that order is important here. Conditions in React with JSX

The order in which the sequences are added and removed items are added and
removed in sequences is really important.

Types of Python Sequences

There are mainly six types of sequences in python that we need to know. Some
of the types that have mostly been used, and others are not that well used and
they have very niche and particular use cases. We will discuss more in detail
below with examples.

So the following are the main types of python sepeuences:

list

tuple

range

string

bytes

bytearray

Let’s discuss each type of sequence in detail with examples.

Python list

The list is the most versatile sequence type and you can create it using the []
brackets. The list is one of the basic data types in python they are used to store
a collection of data and they maintain the order in which each item that is
added is added at a particular index and when they are removed the indexes are
re-indexed.

So if I remove something from the middle then everything after that gets re-
indexed which will be shifted one position ahead of its original position.

The list sequence is mutable. Mutability basically means that you can
manipulate the data inside the list, you can add new items, remove old items,
you can remove items from particular indexes, you can get a slice of the index
you can do a lot of things.

1 # list with number


2 list1 = [3,6,3,84,2,58]
3 print(list1[-1])
4 # output: 58
5 ​
6 # list with string
7 list2 = ['Apple', 'Acer', 'HP', 'Dell']
8 list2[3] = 'Asus'
9 print(list2)
10 # output: ['Apple', 'Acer', 'HP', 'Asus', 'Dell']
11 ​
12 # list with number, string, and further list
13 list3 = ['Red', 3, 'Black', 5, [13,15,16,14], 4]
14 print(list3[4][1])
15 # output: 15

Python tuple

Tuples are basically the same as lists but the difference that they have from
lists is that tuples are immutable and you need to use () parenthesis to
create them. You cannot do anything with a tuple after defining it. You cannot
modify it and add data to it neither can you remove data from it.

The benefit of a tuple is that if you want to provide some constant collection of
data that you know is not going to change and also is not supposed to change
so you just declare a tuple add the values in it. After that, you cannot modify it, if
you will try to modify it will throw an error.

1 t1 = (14,56,3,9,57)
2 print(t1)
3 # output: (14,56,3,9,57)
4 ​
5 t2 = ( "love blogging", 4,6.5, 2 )
6 print(t2)
7 # output: ( "love blogging", 4,6.5, 2 )

Python range()

The range() object is a built-in python function and they are very often used
when we want to loop over certain things. A range is basically a sequence of
numbers that we defined. So we tell python that I want a range of numbers
ranging from 0 to 100.

The range() function gives you numbers in a particular sequence. So they hold a
collection and give you the numbers. It will generate the sequence of integers
with the start and end range is given.

You can also create a list or tuple or any other kind of collection using the
range() function object. Check full article about range() function here.

1 x = range(5)
2 for i in x:
3 print(i)

Python string

Strings are just a collection of characters that are added one after the other and
you can write in the single-quotes (”) or double-quotes (“”).

Python strings are also immutable, you can only reassign the new string to the
variable but can’t make any changes in the string.

Let’s say if you have a particular word ‘c a t’ cat then you can’t jumble up the
words and still have it mean the same thing that’s not going to be possible
because there could be multiple mutations and permutations and combinations
possible for that. So each particular sequence has a particular meaning.

1 blog = "Your Blog Coach"


2 print(blog[2])
3 # output: u

Python bytes()

The bytes() function is the core built-in type for manipulating the binary
data. Bytes objects are immutable sequences that are mean you cannot modify
them.

The syntax of bytes is the same as string type but it will return the bytes
sequence with a prefix of b

1 number = 10
2 b = bytes(number)
3 print( b )
4 # output: b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'

Python bytearray()

The bytearray() function is also a core built-in type. bytearray objects are
mutable and you can modify them. You can add a new element, you can remove
the old element from the byte array objects.

1 ba = [1, 2, 3, 4]
2 print( bytearray(ba) )
3 # output: bytearray(b’\x01\x02\x03\x04′)

Python Sequence Operations

The python operations that you can execute on a sequence. A sequence as we


have already discussed is just a term that is defined to describe things that have
a collection of elements.

There are four main operations for any sequence and these operations you can
implement on the sequence to check the specific element is exist or not, you can
slice the element, you can concatenate the element, etc.

The following are the main operations:

Membership

Concatenation

Repeat

Slicing

Membership

Membership operation implements when we want to check that the specific


element exists or is not in the sequence. For this, we used the (in) and (not in)
operators.

1 'ho' in "Python"
2 # true
3 ​
4 'yt' not in "Python"
5 #false

Concatenation

Concatenation operation means when you want to add two things and make
something new. This is what actually concatenation means is that you have one
sequence and you have another sequence and you want to concatenate one
sequence after the other sequence.

So, we will use the (+) operator to concatenate two sequences. It will add the
second sequence’s elements to the first sequence.

1 s1 = [2,5,7,3]
2 s2 = [6,7,3,9]
3 ​
4 s3 = s1+s2
5 print(s3)
6 ​
7 #[2,5,7,3,6,7,3,9]

Repeat

This Repeat operation we used when we need to print the same sequence of n
times. So it will loop through the same sequence’s elements for printing for the n
number of times that we will be given.

So, we will use the (*) operator to repeat sequences over and over again. It will
print the same sequence’s elements for the n times.

1 s1 = [3,5,6,6]*2
2 ​
3 print(s1)
4 # [3,5,6,6,3,5,6,6]

Slicing

The slicing operator allows us to just slice off a particular element or a


particular group of elements. It will cut off the elements from the sequence
based on your given order to start and end. The order always starts from 0,
remember this.

1 print( "I am learning python"[5:10] )


2 # learn
3 ​
4 print( (43,7,35,8,4,3,7)[3] )
5 # 35

Python Sequence Functions

The python sequence functions are used to perform any action on sequence to
modify or get some details. Like, if you want to count the elements of the
sequence, want to get min, max from the sequence, and insert, delete, clear, etc,
and more.

Let’s have a look at some sequence functions.

append() function

The append() function is used to add an item to the end of the sequence.

1 list = [54,6,24,82,34]
2 list.append(73)
3 print(list)
4 # output: [54,6,24,82,34, 73]

len() function

The len() function is used to get the total length of the sequence, which
means how many elements are in the sequence. It is also the same if you use
count() to get the total element of the sequence.

1 list = [54,6,24,82,34]
2 print(len(list))
3 # output: 5

insert() function

The insert() function is used to insert an item in the sequence at a


specified position. The first parameter of this function is an index of an element
and the second parameter is an element that you want to insert.

1 list = [54,6,24,82,34]
2 list.insert(3, 77)
3 print(list)
4 # output: [54,6,24,77,82,34]

remove() function

The remove() function is used to remove a specific item from the sequence.
It will throw an error if that value does not exist in the sequence.

1 list = [54,6,24,82,34]
2 list.remove(24)
3 print(list)
4 # output: [54,6,82,34]

pop() function

The pop() function is used to remove the item from the specific position of
the sequence. If the position index is not defined then it will remove the last item
of the sequence.

1 list1 = [54,6,24,82,34]
2 list1.pop(1)
3 print(list)
4 # output: [54,82,34]
5 ​
6 list2 = [3,64,38,35,2]
7 list1.pop()
8 print(list)
9 # output: [3,64,38,35]

index() function

The index() function is used to get the index of an item of the sequence. It
will return the index of the first occurrence.

It allows three-parameter, first is the item to get index, second and third is the
start and end point as slice notation, so it will search on that particular
subsequence. These start and end parameters are optional.

1 list = [54,6,24,82,34]
2 list.index(24)
3 print(list)
4 # output: 2

reverse() function

The reverse() function will reverse the place of elements of the sequence.

1 list = [54,6,24,82,34]
2 list.reverse()
3 print(list)
4 # output: [34,82,24,6,54]

copy() function

The copy() function is used to make another copy of the sequence. When
you want to make another sequence with the same elements then use the copy()
function to create it.

1 list = [54,6,24,82,34]
2 list.copy()

max() and min() function

The max() and min() are used to get the higher and lower value from the
sequence. When you want to get the maximum value from the sequence then
use the max() function and if you want to get the minimum value then use the
min() function.

1 list = [54,6,24,82,34]
2 print(max(list))
3 # output: 82
4 ​
5 print(min(list))
6 # output: 6

Hope you understand the all guides on python sequences, types, operations, and
functions. If you still have any queries please let me know in the comment
section I’ll respond to you ASAP.

More python tutorial:

How to Test Internet Speed Using Python?

How to Create Nested Functions in Python?

PYTHON APPEND() PYTHON BYTEARRAY() PYTHON BYTES() PYTHON INDEX()


PYTHON LEN() PYTHON LIST PYTHON MIN() MAX() PYTHON POP()
PYTHON RANGE() PYTHON REVERSE() PYTHON SEQUENCE
PYTHON SEQUENCE ALIGNMENT PYTHON SEQUENCE BY STEP PYTHON SEQUENCE EXAMPLE
PYTHON SEQUENCE FUNCTIONS PYTHON SEQUENCE GENERATOR
PYTHON SEQUENCE OF NUMBERS PYTHON SEQUENCE OF NUMBERS WITH STEP
PYTHON SEQUENCE OPERATIONS PYTHON SEQUENCE TYPING
PYTHON SEQUENCE VS ITERABLE PYTHON SEQUENCE VS LIST
PYTHON SEQUENCE W3SCHOOLS PYTHON SEQUENCES TYPE PYTHON TUPLE
SEQUENCE FUNCTIONS IN PYTHON SEQUENCE IN PYTHON 3 SEQUENCES IN PYTHON
SEQUENCES IN PYTHON WITH EXAMPLES TYPES OF PYTHON SEQUENCES

Tweet on Twitter
Share on Facebook

YOU MAY ALSO LIKE

Generate Random Numbers in Python Methods – Instance, Class


Python Using Random Module and Static Methods

Python range() Function – reverse, How to Extract Data from JSON File
increment, decrement in Python?

How to Test Internet Speed Using


Python?

ABOUT THE AUTHOR: AMAN MEHRA


Hey! I'm Aman Mehra and I'm a full-stack developer and have 5+
years of experience. I love coding and help to people with this blog.

LEAVE A REPLY

Your email address will not be published. Required fields are marked *

Comment

Name *

Email *

Save my name, email, and website in this browser for the next time I comment.

POST COMMENT

ABOUT QUICK LINKS RECENT POSTS GET IN TOUCH

Your Blog Coach is the best site Blog How to Convert Html to PDF using
Name
for finding the solution to any JavaScript?
Categories
issue related to coding and learn
How to Add Password Strength Your email address
more cool stuff and tricks. Contact
Meter in WooCommerce?
About
How to Change Default Product SUBSCRIBE
Sorting in WooCommerce?
© 2021 Your Blog Coach Privacy Policy
Terms and Conditions
Sitemap

You might also like