2 min read

Sequences

In last some posts, we talk about strings. So basically a string is a sequence of characters.
Basically, a sequence is an object that contains multiple items of data. Since the string is a sequence there are various things we had talked about like positions(index) of the values in the sequence.
Similarly, a sub-sequence is a part of the sequence we got by slicing it from index i to index j.

python-sequence
SEQUENCES IN PYTHON

There are some other types of sequences too in the python and one of them is Lists.

List in Python

List is a collection of values or an ordered sequence of values. But, unlike strings, list need not have a uniform type.
Elements of a list are enclosed in square brackets [ ].

List is basically a collection of values(comma-separated)/items within square brackets.

Creating/Declaring List

>>>Rainbow=['Violet','Indigo','Blue','Green','Yellow','Orange','Red']
>>>#Here a list is created with seven items.
>>>print(Rainbow)
['Violet','Indigo','Blue','Green','Yellow','Orange','Red']
>>>numbers=[11,22,33,4,6]  # A list of integers
>>>vowel=['a','e','i','o','u'] #List of Characters
>>>mixed=[4,'Hello',True,6.5] # A list of different data types
>>>list=[]  #An empty list(a list initialization)
>>>nest=[1,2,[3,4,5],6,7]  # List containing another list
>>>type(list)
<class 'list'>

Creating List From Existing Sequence

Using list() method, we can convert other sequence types into a list.

>>>#Python lists can be created from other sequences like
>>>list1=list('string') # Creating list from String
>>>list1
['s','t','r','i','n','g']
>>>list2=list() #Creating an empty list
>>>print(list2)
[]

Creating a list through user input

>>>list3=list(input('Enter something:'))
Enter something:Python
>>>print(list3)
['P','y','t','h','o','n']

Kapil Bansal

A student of B.Tech CSE working on competitive programming, a cybersecurity devotee working towards strengthing the concepts. I am also working on Python modules, Data Structures and Algorithms etc.

0 Comments

Leave a Reply