Please login

Prime

Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime

Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

Sequence Data Types in Python programming language

Sequence Data-Type

Sequence data-type

Sequence Data Types in Python programming language is used to store data in containers. List, Tuple and String are the different types of containers used to store the data. Lists can store the data of any data type and are mutable, Strings can store data of the str type and are immutable. Tuples can also store any type of value and are immutable data-types.

Different data-types in Python

Sequence Data Types in Python programmig language

List data-type in Python

List data-type belongs to the sequential data-type class. The list is the only data-type under sequential which are mutable. It can store values or elements of any data-type. We can change and perform many operations on the list and like

  • append
  • remove
  • reverse
  • sorted
  • extend

etc. We have many more built-in functions available for manipulation in lists. Let’s have a look at some important operations on the list.

list in python

Operations on List

#initialize an empty list
arr = []
#user defined list of length 5
#append elements in list using for loop
print('Enter elements for list :')
for _ in range(5):
    ele = input('')
    arr.append(ele)
print(arr)

#sorting the list
arr = sorted(arr)
print(arr)

#minimum element of the list
minimum = min(arr)
maximum = max(arr)
print(minimum)
print(maximum)

#reverse the list
print(arr[::-1])
Output:
Enter elements for list :
1
4
2
5
3
['1', '4', '2', '5', '3']
['1', '2', '3', '4', '5']
1
5
['5', '4', '3', '2', '1']

String data-type in Python

string in python

String data-types are used to store the string values. Strings are immutable and so we cannot manipulate the elements in the string. Strings although have many built-in functions and we a perform many operations on the string. Some built-in functions available for strings are:

  • count
  • isupper
  • islower
  • join
  • split

etc. We have many other functions for string data-type. Let’s have a look at some operations on strings.

Operations on String

#initialize a string
String = 'Mondays sucks'

#counting number of times a is available in String
val = String.count('a')
print(val)

#converting string to uppercase
String = String.upper()
print(String)

#converting string to lowercase
String = String.lower()
print(String)

#reverse the String
print(String[::-1])

#join in String
String = ('-').join(String)
print(String)

#split in string
String = String.split('-')
print(String)
Output:

1
MONDAYS SUCKS
mondays sucks
skcus syadnom
m-o-n-d-a-y-s- -s-u-c-k-s
['m', 'o', 'n', 'd', 'a', 'y', 's', ' ', 's', 'u', 'c', 'k', 's']

Tuple data-type in Python

Tuples also belong to the class of sequence data-type. There are almost the same as the list in python but have the property of immutability. We cannot manipulate the elements of the tuple but we can perform many operations on tuples:

  • count
  • index
  • len
  • type

etc. Let’s have a look at some operations on tuples.

tuple data type in python

Operations on Tuple

#initialize a tuple
Tu = tuple([1, 'String', 1.25, 4, 6])
print(Tu)

#counting elements in tuple
val = Tu.count(1)
print(val)

#finding index of an element in tuple
ele = Tu.index(1.25)
print(ele)

#finding type of sequence
print(type(Tu))

#finding length of tuple
length = len(Tu)
print(length)
Output:
(1, 'String', 1.25, 4, 6)
1
2
<class 'tuple'>
5