Sequence Data Types in Python programming language
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 Sequential data-types in Python
List 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
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.
Operations on List
# initialize an empty list arr = [1, 4, 2, 5, 3] # 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])
[1, 2, 3, 4, 5] 1 5 [5, 4, 3, 2, 1]
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:
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 = 'PrepInsta' # 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)
1 PREPINSTA prepinsta atsniperp p-r-e-p-i-n-s-t-a ['p', 'r', 'e', 'p', 'i', 'n', 's', 't', 'a']
Tuple 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:
etc. Let’s have a look at some operations on tuples.
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)
(1, 'String', 1.25, 4, 6) 1 2 <class 'tuple'> 5