- 0
Notifications Mark All Read
- Login
- Get Prime
Sequence Data Types in Python Programming Language
Sequence Data-Types
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 Sequence Data-Types in Python
As we discussed above, Sequence Data Type in Python is used as a container. There are different types of Sequence Data Type. For better understanding of these data types we can prefer below image:
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
- append
- remove
- reverse
- sorted
- extend
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])
Output
[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:
- 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 = '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)
Output
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:
- count
- index
- len
- type
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)
Output
(1, 'String', 1.25, 4, 6) 1 2 <class 'tuple'> 5
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment