Today in this blog we will learn about the most useful sequences that every Python programmer should know.
Here is the agenda.
Agenda
- What is Sequence?
- Most Useful Sequences
- What is list?
- What is list comprehension?
- How to create and take input in list?
- Function provided by list class
- What is str?
- How to create and take input in str?
- Function provided by str class
- What is tuple?
- Difference between tuple and list?
- How to create and take input in tuple?
- Function provided by tuple class
- Build-in Methods for any Sequences
- Conclusion
What is Sequence?
- Sequences are a generic term for an ordered set which means that the order in which we input the items will be the same when we access them.
<span>Input</span><span>:</span> <span>[</span><span>1</span><span>,</span><span>2</span><span>,</span><span>3</span><span>,</span><span>4</span><span>,</span><span>5</span><span>]</span><span>Input</span><span>:</span> <span>[</span><span>1</span><span>,</span><span>2</span><span>,</span><span>3</span><span>,</span><span>4</span><span>,</span><span>5</span><span>]</span>Input: [1,2,3,4,5]
Enter fullscreen mode Exit fullscreen mode
So here if I traverse using loop then it will print the output in the same order as I input data.
- Sequences are iterable but an iterable may not be sequence as set and dict
- Sequence have concept of indexing.
Most Useful Sequence
There are 3 most useful sequences.
- str
- list
- tuple
List is the most useful sequence
We will discuss about them one by one. So let start’s with list
What is list?
- List is a sequence
- List stores multiples element in a single variable
<span>list</span><span>=</span><span>[</span><span>1</span><span>,</span><span>'</span><span>UG-SEP</span><span>'</span><span>,</span><span>'</span><span>dev</span><span>'</span><span>,</span><span>1.2</span><span>,(</span><span>7</span><span>,</span><span>'</span><span>Python</span><span>'</span><span>)]</span><span>list</span><span>=</span><span>[</span><span>1</span><span>,</span><span>'</span><span>UG-SEP</span><span>'</span><span>,</span><span>'</span><span>dev</span><span>'</span><span>,</span><span>1.2</span><span>,(</span><span>7</span><span>,</span><span>'</span><span>Python</span><span>'</span><span>)]</span>list=[1,'UG-SEP','dev',1.2,(7,'Python')]
Enter fullscreen mode Exit fullscreen mode
- List can contain heterogenous element.
- List is mutable.
What is list comprehension?
List Comprehension offer a syntax to compress the code it used to take input in list in just one line. It is a very awesome feature to compress code and easy to understand.
Syntax
List= [ expression(element) for element in old List if condition ]List= [ expression(element) for element in old List if condition ]List= [ expression(element) for element in old List if condition ]
Enter fullscreen mode Exit fullscreen mode
we will learn how to take input from user using List Comprehension in forward topic
How to create and assign value in list?
List are created using square brackets( [] ) in the square brackets we write elements separating by commas( , )
<span>list</span><span>=</span><span>[</span><span>'</span><span>Welcome</span><span>'</span><span>,</span><span>'</span><span>Dev</span><span>'</span><span>,</span><span>'</span><span>Community</span><span>'</span><span>]</span><span>list</span><span>=</span><span>[</span><span>'</span><span>Welcome</span><span>'</span><span>,</span><span>'</span><span>Dev</span><span>'</span><span>,</span><span>'</span><span>Community</span><span>'</span><span>]</span>list=['Welcome','Dev','Community']
Enter fullscreen mode Exit fullscreen mode
As you can see I writes elements separating by commas.
How to create empty list?
<span># if the square brackets are empty it will considered as empty list </span><span>l1</span><span>=</span><span>[]</span><span># list() function used to creates list objects </span><span>l2</span><span>=</span><span>list</span><span>()</span><span># if the square brackets are empty it will considered as empty list </span><span>l1</span><span>=</span><span>[]</span> <span># list() function used to creates list objects </span><span>l2</span><span>=</span><span>list</span><span>()</span># if the square brackets are empty it will considered as empty list l1=[] # list() function used to creates list objects l2=list()
Enter fullscreen mode Exit fullscreen mode
How to take input from user
<span># Using List comprehension </span><span>l1</span><span>=</span><span>[</span><span>eval</span><span>(</span><span>i</span><span>)</span> <span>for</span> <span>i</span> <span>in</span> <span>input</span><span>(</span><span>"</span><span>Enter data separating by commas</span><span>"</span><span>).</span><span>split</span><span>(</span><span>'</span><span>,</span><span>'</span><span>)]</span><span># So in the above line what happen is that first we will enter in # list comprehension and take input from user after that it will # be split each element using commas as the criteria the split() # function split() string on a given criteria and I pass , as the # criteria so no the each element separated by , are treated as # list element and now the for run and we evaluate the element and # assign it. </span><span># using for loop # create a empty list </span><span>l2</span><span>=</span><span>list</span><span>()</span><span># take the size of list </span><span>for</span> <span>i</span> <span>in</span> <span>range</span><span>(</span><span>0</span><span>,</span><span>int</span><span>(</span><span>input</span><span>(</span><span>"</span><span>Enter the size</span><span>"</span><span>))):</span><span># append is a function to add data at the last of the list we will # learn about it </span> <span>l2</span><span>.</span><span>append</span><span>(</span><span>eval</span><span>(</span><span>input</span><span>()))</span><span># Using List comprehension </span><span>l1</span><span>=</span><span>[</span><span>eval</span><span>(</span><span>i</span><span>)</span> <span>for</span> <span>i</span> <span>in</span> <span>input</span><span>(</span><span>"</span><span>Enter data separating by commas</span><span>"</span><span>).</span><span>split</span><span>(</span><span>'</span><span>,</span><span>'</span><span>)]</span> <span># So in the above line what happen is that first we will enter in # list comprehension and take input from user after that it will # be split each element using commas as the criteria the split() # function split() string on a given criteria and I pass , as the # criteria so no the each element separated by , are treated as # list element and now the for run and we evaluate the element and # assign it. </span> <span># using for loop # create a empty list </span><span>l2</span><span>=</span><span>list</span><span>()</span> <span># take the size of list </span><span>for</span> <span>i</span> <span>in</span> <span>range</span><span>(</span><span>0</span><span>,</span><span>int</span><span>(</span><span>input</span><span>(</span><span>"</span><span>Enter the size</span><span>"</span><span>))):</span> <span># append is a function to add data at the last of the list we will # learn about it </span> <span>l2</span><span>.</span><span>append</span><span>(</span><span>eval</span><span>(</span><span>input</span><span>()))</span># Using List comprehension l1=[eval(i) for i in input("Enter data separating by commas").split(',')] # So in the above line what happen is that first we will enter in # list comprehension and take input from user after that it will # be split each element using commas as the criteria the split() # function split() string on a given criteria and I pass , as the # criteria so no the each element separated by , are treated as # list element and now the for run and we evaluate the element and # assign it. # using for loop # create a empty list l2=list() # take the size of list for i in range(0,int(input("Enter the size"))): # append is a function to add data at the last of the list we will # learn about it l2.append(eval(input()))
Enter fullscreen mode Exit fullscreen mode
Function provided by list class
append() : used to append element at the end of the listclear() : to remove all element from the listcopy() : return shallow copy of the listcount() : to the number of occurrence of a particular elementindex() : to get the index of a particular elementinsert() : to insert element at a particular indexpop() : to pop a particular element by index no. remove thelast element if index not providedsort() : to sort the element in an orderreverse(): to reverse the listremove() : remove the particular elementappend() : used to append element at the end of the list clear() : to remove all element from the list copy() : return shallow copy of the list count() : to the number of occurrence of a particular element index() : to get the index of a particular element insert() : to insert element at a particular index pop() : to pop a particular element by index no. remove the last element if index not provided sort() : to sort the element in an order reverse(): to reverse the list remove() : remove the particular elementappend() : used to append element at the end of the list clear() : to remove all element from the list copy() : return shallow copy of the list count() : to the number of occurrence of a particular element index() : to get the index of a particular element insert() : to insert element at a particular index pop() : to pop a particular element by index no. remove the last element if index not provided sort() : to sort the element in an order reverse(): to reverse the list remove() : remove the particular element
Enter fullscreen mode Exit fullscreen mode
Syntax:
<span>l</span><span>=</span><span>[</span><span>1</span><span>,</span><span>2</span><span>,</span><span>3</span><span>]</span><span># append 4 at the end so l=[1,2,3,4] </span><span>l</span><span>.</span><span>append</span><span>(</span><span>4</span><span>)</span><span># clear all element l=[] </span><span>l</span><span>.</span><span>clear</span><span>()</span><span># shallow copy of l shlcpy=[] </span><span>shlcpy</span><span>=</span><span>l</span><span>.</span><span>copy</span><span>()</span><span># count the occurrence of 1 but before that let add some value in l </span><span>l</span><span>=</span><span>[</span><span>1</span><span>,</span><span>2</span><span>,</span><span>2</span><span>,</span><span>3</span><span>,</span><span>1</span><span>,</span><span>3</span><span>,</span><span>5</span><span>,</span><span>7</span><span>]</span><span>print</span><span>(</span><span>l</span><span>.</span><span>count</span><span>(</span><span>1</span><span>))</span><span># index of a particular element prints the first occurrent index 0 </span><span>print</span><span>(</span><span>l</span><span>.</span><span>index</span><span>(</span><span>1</span><span>))</span><span># insert at the 2 and 4th index l=[1,2,2,3,2,1,3,5,7] </span><span>l</span><span>.</span><span>insert</span><span>(</span><span>4</span><span>,</span><span>2</span><span>)</span><span># pop 0 index value l=[2,2,3,2,1,3,5,7] </span><span>l</span><span>.</span><span>pop</span><span>(</span><span>0</span><span>)</span><span># sort the list l=[1,2,2,2,3,3,5,7] </span><span>l</span><span>.</span><span>sort</span><span>()</span><span># reverse the list l=[7,5,3,3,2,2,2,1] </span><span>l</span><span>.</span><span>reverse</span><span>()</span><span># sort in descending to ascending </span><span>l</span><span>.</span><span>sort</span><span>(</span><span>reverse</span><span>=</span><span>True</span><span>)</span><span># remove particular element 7 l=[5,3,3,2,2,2,1] </span><span>l</span><span>.</span><span>remove</span><span>(</span><span>1</span><span>)</span><span>l</span><span>=</span><span>[</span><span>1</span><span>,</span><span>2</span><span>,</span><span>3</span><span>]</span> <span># append 4 at the end so l=[1,2,3,4] </span><span>l</span><span>.</span><span>append</span><span>(</span><span>4</span><span>)</span> <span># clear all element l=[] </span><span>l</span><span>.</span><span>clear</span><span>()</span> <span># shallow copy of l shlcpy=[] </span><span>shlcpy</span><span>=</span><span>l</span><span>.</span><span>copy</span><span>()</span> <span># count the occurrence of 1 but before that let add some value in l </span><span>l</span><span>=</span><span>[</span><span>1</span><span>,</span><span>2</span><span>,</span><span>2</span><span>,</span><span>3</span><span>,</span><span>1</span><span>,</span><span>3</span><span>,</span><span>5</span><span>,</span><span>7</span><span>]</span> <span>print</span><span>(</span><span>l</span><span>.</span><span>count</span><span>(</span><span>1</span><span>))</span> <span># index of a particular element prints the first occurrent index 0 </span><span>print</span><span>(</span><span>l</span><span>.</span><span>index</span><span>(</span><span>1</span><span>))</span> <span># insert at the 2 and 4th index l=[1,2,2,3,2,1,3,5,7] </span><span>l</span><span>.</span><span>insert</span><span>(</span><span>4</span><span>,</span><span>2</span><span>)</span> <span># pop 0 index value l=[2,2,3,2,1,3,5,7] </span><span>l</span><span>.</span><span>pop</span><span>(</span><span>0</span><span>)</span> <span># sort the list l=[1,2,2,2,3,3,5,7] </span><span>l</span><span>.</span><span>sort</span><span>()</span> <span># reverse the list l=[7,5,3,3,2,2,2,1] </span><span>l</span><span>.</span><span>reverse</span><span>()</span> <span># sort in descending to ascending </span><span>l</span><span>.</span><span>sort</span><span>(</span><span>reverse</span><span>=</span><span>True</span><span>)</span> <span># remove particular element 7 l=[5,3,3,2,2,2,1] </span><span>l</span><span>.</span><span>remove</span><span>(</span><span>1</span><span>)</span>l=[1,2,3] # append 4 at the end so l=[1,2,3,4] l.append(4) # clear all element l=[] l.clear() # shallow copy of l shlcpy=[] shlcpy=l.copy() # count the occurrence of 1 but before that let add some value in l l=[1,2,2,3,1,3,5,7] print(l.count(1)) # index of a particular element prints the first occurrent index 0 print(l.index(1)) # insert at the 2 and 4th index l=[1,2,2,3,2,1,3,5,7] l.insert(4,2) # pop 0 index value l=[2,2,3,2,1,3,5,7] l.pop(0) # sort the list l=[1,2,2,2,3,3,5,7] l.sort() # reverse the list l=[7,5,3,3,2,2,2,1] l.reverse() # sort in descending to ascending l.sort(reverse=True) # remove particular element 7 l=[5,3,3,2,2,2,1] l.remove(1)
Enter fullscreen mode Exit fullscreen mode
What is str?
- str is a sequence of characters which are iterable
- str is immutable
- str elements are indexed
<span>s</span><span>=</span><span>"</span><span>Welcome to my blog follow me for more post like this.</span><span>"</span><span>s</span><span>=</span><span>"</span><span>Welcome to my blog follow me for more post like this.</span><span>"</span>s="Welcome to my blog follow me for more post like this."
Enter fullscreen mode Exit fullscreen mode
How to create and take input in str?
<span># Empty str </span><span>s</span><span>=</span><span>str</span><span>()</span><span># Empty str </span><span>s</span><span>=</span><span>str</span><span>()</span># Empty str s=str()
Enter fullscreen mode Exit fullscreen mode
Ways to write str constant
<span># by using double quotes " </span><span>str1</span><span>=</span><span>"</span><span>Dev.to</span><span>"</span><span># by using single quotes ' </span><span>str2</span><span>=</span><span>'</span><span>Follow me</span><span>'</span><span># by using """ </span><span>str3</span><span>=</span><span>"""</span><span>Love the post</span><span>"""</span><span># by using double quotes " </span><span>str1</span><span>=</span><span>"</span><span>Dev.to</span><span>"</span> <span># by using single quotes ' </span><span>str2</span><span>=</span><span>'</span><span>Follow me</span><span>'</span> <span># by using """ </span><span>str3</span><span>=</span><span>"""</span><span>Love the post</span><span>"""</span># by using double quotes " str1="Dev.to" # by using single quotes ' str2='Follow me' # by using """ str3="""Love the post"""
Enter fullscreen mode Exit fullscreen mode
How to take input from user in str?
<span># input() is a function used to take input from user return str </span><span>str</span><span>=</span><span>input</span><span>(</span><span>"</span><span>Enter a string</span><span>"</span><span>)</span><span># input() is a function used to take input from user return str </span><span>str</span><span>=</span><span>input</span><span>(</span><span>"</span><span>Enter a string</span><span>"</span><span>)</span># input() is a function used to take input from user return str str=input("Enter a string")
Enter fullscreen mode Exit fullscreen mode
Function provided by str class
s.replace() : replace text by another texts.index() : get index of a particular texts.count() : count the occurrence of a given texts.split() : split str on the basis of given criteria returnslists.join() : join each sequence elements separated by the givencriterias.startswith(): check whether the string start with the given texts.endwith() : check whether the string end with the given texts.find() : find some given text in the strings.upper() : convert the string to uppercases.lower() : convert the string to lowercases.strip() : remove extra white space from the left and rightof the strings.replace() : replace text by another text s.index() : get index of a particular text s.count() : count the occurrence of a given text s.split() : split str on the basis of given criteria returns list s.join() : join each sequence elements separated by the given criteria s.startswith(): check whether the string start with the given text s.endwith() : check whether the string end with the given text s.find() : find some given text in the string s.upper() : convert the string to uppercase s.lower() : convert the string to lowercase s.strip() : remove extra white space from the left and right of the strings.replace() : replace text by another text s.index() : get index of a particular text s.count() : count the occurrence of a given text s.split() : split str on the basis of given criteria returns list s.join() : join each sequence elements separated by the given criteria s.startswith(): check whether the string start with the given text s.endwith() : check whether the string end with the given text s.find() : find some given text in the string s.upper() : convert the string to uppercase s.lower() : convert the string to lowercase s.strip() : remove extra white space from the left and right of the string
Enter fullscreen mode Exit fullscreen mode
str contain more functions
Syntax
<span># Let first create a str </span><span>s</span><span>=</span><span>'</span><span>text</span><span>'</span><span># let replace 'tex' by 'res' gives 'rest' does not changes in s # return str object which i have again stored in s so it become # rest </span><span>s</span><span>=</span><span>s</span><span>.</span><span>replace</span><span>(</span><span>'</span><span>tex</span><span>'</span><span>,</span><span>'</span><span>res</span><span>'</span><span>)</span><span># find the index of s which is 2 </span><span>s</span><span>.</span><span>find</span><span>(</span><span>'</span><span>s</span><span>'</span><span>)</span><span># count the occurrence of 'st' which is 1 </span><span>s</span><span>.</span><span>count</span><span>(</span><span>'</span><span>st</span><span>'</span><span>)</span><span># split string on the basis of '' empty space means each char will # become element of the list l=['r','e','s','t'] </span><span>l</span><span>=</span><span>s</span><span>.</span><span>split</span><span>(</span><span>''</span><span>)</span><span># join l on the separating by ',' s="r,e,s,t" </span><span>s</span><span>=</span><span>'</span><span>,</span><span>'</span><span>.</span><span>join</span><span>(</span><span>l</span><span>)</span><span># check whether s starts with 'r,e,s' or not True </span><span>s</span><span>.</span><span>startswith</span><span>(</span><span>'</span><span>r,e,s</span><span>'</span><span>)</span><span># check whether s ends with 's,t' or not True </span><span>s</span><span>.</span><span>endswith</span><span>(</span><span>'</span><span>s,t</span><span>'</span><span>)</span><span># remove extra white space of ' My ' using strip s='My' </span><span>s</span><span>=</span><span>'</span><span> My </span><span>'</span><span>.</span><span>strip</span><span>()</span><span># lowercase the string </span><span>s</span><span>.</span><span>lowercase</span><span>()</span><span># uppercase the string </span><span>s</span><span>.</span><span>uppercase</span><span>()</span><span># Let first create a str </span><span>s</span><span>=</span><span>'</span><span>text</span><span>'</span> <span># let replace 'tex' by 'res' gives 'rest' does not changes in s # return str object which i have again stored in s so it become # rest </span><span>s</span><span>=</span><span>s</span><span>.</span><span>replace</span><span>(</span><span>'</span><span>tex</span><span>'</span><span>,</span><span>'</span><span>res</span><span>'</span><span>)</span> <span># find the index of s which is 2 </span><span>s</span><span>.</span><span>find</span><span>(</span><span>'</span><span>s</span><span>'</span><span>)</span> <span># count the occurrence of 'st' which is 1 </span><span>s</span><span>.</span><span>count</span><span>(</span><span>'</span><span>st</span><span>'</span><span>)</span> <span># split string on the basis of '' empty space means each char will # become element of the list l=['r','e','s','t'] </span><span>l</span><span>=</span><span>s</span><span>.</span><span>split</span><span>(</span><span>''</span><span>)</span> <span># join l on the separating by ',' s="r,e,s,t" </span><span>s</span><span>=</span><span>'</span><span>,</span><span>'</span><span>.</span><span>join</span><span>(</span><span>l</span><span>)</span> <span># check whether s starts with 'r,e,s' or not True </span><span>s</span><span>.</span><span>startswith</span><span>(</span><span>'</span><span>r,e,s</span><span>'</span><span>)</span> <span># check whether s ends with 's,t' or not True </span><span>s</span><span>.</span><span>endswith</span><span>(</span><span>'</span><span>s,t</span><span>'</span><span>)</span> <span># remove extra white space of ' My ' using strip s='My' </span><span>s</span><span>=</span><span>'</span><span> My </span><span>'</span><span>.</span><span>strip</span><span>()</span> <span># lowercase the string </span><span>s</span><span>.</span><span>lowercase</span><span>()</span> <span># uppercase the string </span><span>s</span><span>.</span><span>uppercase</span><span>()</span># Let first create a str s='text' # let replace 'tex' by 'res' gives 'rest' does not changes in s # return str object which i have again stored in s so it become # rest s=s.replace('tex','res') # find the index of s which is 2 s.find('s') # count the occurrence of 'st' which is 1 s.count('st') # split string on the basis of '' empty space means each char will # become element of the list l=['r','e','s','t'] l=s.split('') # join l on the separating by ',' s="r,e,s,t" s=','.join(l) # check whether s starts with 'r,e,s' or not True s.startswith('r,e,s') # check whether s ends with 's,t' or not True s.endswith('s,t') # remove extra white space of ' My ' using strip s='My' s=' My '.strip() # lowercase the string s.lowercase() # uppercase the string s.uppercase()
Enter fullscreen mode Exit fullscreen mode
What is tuple?
- tuple is immutable
- tuple can store heterogenous element
- tuple elements are separated by ‘,’
<span>t</span><span>=</span><span>(</span><span>1</span><span>,</span><span>'</span><span>Comment</span><span>'</span><span>,</span><span>'</span><span>Share</span><span>'</span><span>,</span><span>1.5</span><span>)</span><span>t</span><span>=</span><span>(</span><span>1</span><span>,</span><span>'</span><span>Comment</span><span>'</span><span>,</span><span>'</span><span>Share</span><span>'</span><span>,</span><span>1.5</span><span>)</span>t=(1,'Comment','Share',1.5)
Enter fullscreen mode Exit fullscreen mode
The most common doubt
Difference between tuple and list?
How to create and take input in tuple?
Tuple is created using Parenthesis ‘()’ inside it we write element separated by ‘,’
How to create empty tuple
<span># by using tuple() function which create tuple object </span><span>t</span><span>=</span><span>tuple</span><span>()</span><span># by leaving the parenthesis empty </span><span>t</span><span>=</span><span>()</span><span># by using tuple() function which create tuple object </span><span>t</span><span>=</span><span>tuple</span><span>()</span> <span># by leaving the parenthesis empty </span><span>t</span><span>=</span><span>()</span># by using tuple() function which create tuple object t=tuple() # by leaving the parenthesis empty t=()
Enter fullscreen mode Exit fullscreen mode
How to take input in tuple from user?
<span># using List comprehension we convert the list into tuple using # tuple() function let check how </span><span>t</span><span>=</span><span>tuple</span><span>([</span><span>eval</span><span>(</span><span>i</span><span>)</span> <span>for</span> <span>i</span> <span>in</span> <span>input</span><span>(</span><span>"</span><span>Enter data separated by commas </span><span>"</span><span>).</span><span>split</span><span>(</span><span>'</span><span>,</span><span>'</span><span>)])</span><span># using List comprehension we convert the list into tuple using # tuple() function let check how </span><span>t</span><span>=</span><span>tuple</span><span>([</span><span>eval</span><span>(</span><span>i</span><span>)</span> <span>for</span> <span>i</span> <span>in</span> <span>input</span><span>(</span><span>"</span><span>Enter data separated by commas </span><span>"</span><span>).</span><span>split</span><span>(</span><span>'</span><span>,</span><span>'</span><span>)])</span># using List comprehension we convert the list into tuple using # tuple() function let check how t=tuple([eval(i) for i in input("Enter data separated by commas ").split(',')])
Enter fullscreen mode Exit fullscreen mode
You cannot add, modify and delete element in tuple this is a big difference between tuple and list
Functions provided by tuple class
tuple class only provide two functions
<span>t</span><span>.</span><span>index</span><span>():</span> <span>get</span> <span>the</span> <span>index</span> <span>of</span> <span>particular</span> <span>text</span><span>t</span><span>.</span><span>count</span><span>():</span> <span>count</span> <span>the</span> <span>occurrence</span> <span>of</span> <span>given</span> <span>text</span><span>t</span><span>.</span><span>index</span><span>():</span> <span>get</span> <span>the</span> <span>index</span> <span>of</span> <span>particular</span> <span>text</span> <span>t</span><span>.</span><span>count</span><span>():</span> <span>count</span> <span>the</span> <span>occurrence</span> <span>of</span> <span>given</span> <span>text</span>t.index(): get the index of particular text t.count(): count the occurrence of given text
Enter fullscreen mode Exit fullscreen mode
Syntax
<span># create a tuple </span><span>t</span><span>=</span><span>(</span><span>1</span><span>,</span><span>2</span><span>,</span><span>3</span><span>,</span><span>4</span><span>,</span><span>'</span><span>blogging</span><span>'</span><span>,</span><span>1</span><span>)</span><span># get the index of 3 in t which is 2 </span><span>t</span><span>.</span><span>index</span><span>(</span><span>3</span><span>)</span><span># count the occurrence of 1 in t which is 2 </span><span>t</span><span>.</span><span>count</span><span>(</span><span>1</span><span>)</span><span># create a tuple </span><span>t</span><span>=</span><span>(</span><span>1</span><span>,</span><span>2</span><span>,</span><span>3</span><span>,</span><span>4</span><span>,</span><span>'</span><span>blogging</span><span>'</span><span>,</span><span>1</span><span>)</span> <span># get the index of 3 in t which is 2 </span><span>t</span><span>.</span><span>index</span><span>(</span><span>3</span><span>)</span> <span># count the occurrence of 1 in t which is 2 </span><span>t</span><span>.</span><span>count</span><span>(</span><span>1</span><span>)</span># create a tuple t=(1,2,3,4,'blogging',1) # get the index of 3 in t which is 2 t.index(3) # count the occurrence of 1 in t which is 2 t.count(1)
Enter fullscreen mode Exit fullscreen mode
Build-in Methods for any sequences
len(): find the length of a given sequencemin(): finds the min value of the given sequencemax(): finds the min value of the given sequencesum(): return the sum of the element of given sequencesorted(): return list after sorting the given sequencelen(): find the length of a given sequence min(): finds the min value of the given sequence max(): finds the min value of the given sequence sum(): return the sum of the element of given sequence sorted(): return list after sorting the given sequencelen(): find the length of a given sequence min(): finds the min value of the given sequence max(): finds the min value of the given sequence sum(): return the sum of the element of given sequence sorted(): return list after sorting the given sequence
Enter fullscreen mode Exit fullscreen mode
Syntax
<span># create a sequence </span><span>l</span><span>=</span><span>[</span><span>2</span><span>,</span><span>5</span><span>,</span><span>1</span><span>,</span><span>4</span><span>,</span><span>3</span><span>]</span><span># find the len of l which is 5 </span><span>len</span><span>(</span><span>l</span><span>)</span><span># find the max element which is 5 </span><span>max</span><span>(</span><span>l</span><span>)</span><span># find the min element which is 1 </span><span>min</span><span>(</span><span>l</span><span>)</span><span># find the sum which is 15 </span><span>sum</span><span>(</span><span>l</span><span>)</span><span># sort l but no changes in l return list which is [1,2,3,4,5] </span><span>sorted</span><span>(</span><span>l</span><span>)</span><span># create a sequence </span><span>l</span><span>=</span><span>[</span><span>2</span><span>,</span><span>5</span><span>,</span><span>1</span><span>,</span><span>4</span><span>,</span><span>3</span><span>]</span> <span># find the len of l which is 5 </span><span>len</span><span>(</span><span>l</span><span>)</span> <span># find the max element which is 5 </span><span>max</span><span>(</span><span>l</span><span>)</span> <span># find the min element which is 1 </span><span>min</span><span>(</span><span>l</span><span>)</span> <span># find the sum which is 15 </span><span>sum</span><span>(</span><span>l</span><span>)</span> <span># sort l but no changes in l return list which is [1,2,3,4,5] </span><span>sorted</span><span>(</span><span>l</span><span>)</span># create a sequence l=[2,5,1,4,3] # find the len of l which is 5 len(l) # find the max element which is 5 max(l) # find the min element which is 1 min(l) # find the sum which is 15 sum(l) # sort l but no changes in l return list which is [1,2,3,4,5] sorted(l)
Enter fullscreen mode Exit fullscreen mode
Conclusion
This Post will help you to enhance your python skills as a beginner. Don’t Forget to Follow me and Give a Unicorn if this post is informative and useful for you.
If you have any doubt then comment below.
This post is created on the request of @meenagupta5
Hope you love it
Read more:
5 Project to Master in Python for Beginners
Ujjwal (UG THE SEP) ・ Aug 23 ’21
#python #beginners #codenewbie #tutorial
暂无评论内容