what are python sequences and its operations.

what are python sequences and its operations.
A sequence is a group of individual items. We can insert values in an order and we get the result in sequence.
python consists of six types of sequences
1. Python strings
2. python lists
3. Python Tuples
4. Byte Sequences
5. Bytes Arrays
6. Range() Objects
1. Python Strings
Generally a string is nothing but a group of characters. In Python for creating an array of characters we use strings. For creating a string in python we use single or double quotes.
for example
name=”john”
For creating an empty string in python
name=str()
We can also create string like
name=str(‘india’)
In order to know the type of variable we can use ‘type’
for example
type(name)
it displays
<class ‘str’> # it tells the name variable is of ‘string’ type
2. Python Lists
List is a collection of ordered group of items. For declaring a list in python we have to use Square brackets [ ]. Python lists can hold any kind of data.
example:
list1=[‘rice’,’sugar’,’wheat’,’curd’]
For accessing list items we can use index
example
list1[0]=’milk’
In order to access part of list elements we can use slice
example:
list1[2:4]
output will be
[‘sugar’,’wheat’]
3. Python Tuples
Tuples are also similar like lists but they are immutables, which means tuples cannot be modified. The values inside the tuples cannot be changed once declared. For declaring python tuples we have to use parenthesis (). Tuples can also hold different kinds of data.
example:
tuple1=(‘rice’,’sugar’,’wheat’,’curd’)
Tuple cannot be modified so if we try to modify the element in python it shows error
tuple1[0]=’milk’ //it shows error
For accessing elements in the tuple
tuple1[0] //output will be (‘rice’)
tuple1[2:4]
output will be
(‘sugar’,’wheat’)
4. Bytes Sequence
bytes() returns an immutable bytes object. In order to display we use bytes(5) it displays b’\x01\x02\x03\x04\x05′.
Bytes() is an immutable so if we try the change an item it will display an error. so we can’t modify a bytes() object.
Bytes Arrays
It is like a bytes obect but it can be modified since it is mutable.
example:
i=bytearray(4)
it displays bytearray(b’\x00\x00\x00\x00′)
a=bytearray([1,2,3,4,5])
it displays bytearray(b’\x01\x02\x03\x04\x05′)
5. Python range() objects
A range() object gives a list of numbers.
example:
for i in range(0,5)
1
2
3
4
Operations with Python Sequences
Concatenation
Integer Multiplication
Python Slice
Membership
1. Concatenation
It is used to add the second one to first one
ex:
‘india’+’china’
Output will be
indiachina
2. Integer Multiplication
We can replicate a string for several times
example:
india * 5
output:
indiaindiaindiaindiaindia
3. Python Slice
If we want a part of a sequence we can used slice
example:
‘america’[1:4]
output will be
‘ ame’
1. Python Membership
To check whether the value is present in a sequence or not. We used ‘in’ Operator
example:
‘ind’ in ‘india today’
output will be
True
To learn more about python click here


Contact us
Whether you have a request, a query, or want to work with us, use the form below to get in touch with our team.