Which of the following function will always returns a list?
Option c : partition[] function always return a tuple of 3 elements.
More about functions :
a] find[] : The find[] function returns the index of the first occurrence of the substring is found. If not found, then it returns -1.
Example :
str = “I love car.”
index = str.find[‘love’]
print[“Substring ‘love’ : “, index]
b] index[] : The index[] function returns the index of the given element in the list.
Example :
L = [10 , 12 , 23 , 34 , 45]
index = L.index[12]
print[“Index of 12 is “,index]
c] partition[] : The partition[] function splits the string at the first occurrence of the given string and always return a tuple containing three parts :
- String before separator
- Given string
- String after separator.
str = “I live in Lucknow.”
print[str.partition[‘in’]
d] split[] : The split[] function returns a list of the words in the string/line, separated by a delimiter string.
Example :
str = “I love to play football.”
print[str.split[” “,3]]
B]Index
Hope it will help you
9. Lists¶
A list is an ordered set of values, where each value is identified by an index. The values that make up a list are called its elements. Lists are similar to strings, which are ordered sets of characters, except that the elements of a list can have any type. Lists and strings — and other things that behave like ordered sets — are called sequences.
Introduction
In Python, you’ll be able to use a list function that creates a group that will be manipulated for your analysis. This collection of data is named a list object.
While all methods are functions in Python, not all functions are methods. There’s a key difference between functions and methods in Python. Functions take objects as inputs while Methods in contrast act on objects.
Image Source: Google Images
Python offers the subsequent list functions:
- sort[]: Sorts the list in ascending order.
- type[list]: It returns the class type of an object.
- append[]: Adds one element to a list.
- extend[]: Adds multiple elements to a list.
- index[]:Returns the first appearance of a particularvalue.
- max[list]: It returns an item from the list with a max value.
- min[list]: It returns an item from the list with a min value.
- len[list]: It gives the overall length of the list.
- clear[]: Removes all the elements from the list.
- insert[]:Adds a component at the required position.
- count[]:Returns the numberof elements with the required value.
- pop[]:Removes the element at the required position.
- remove[]:Removes the primary item with the desired value.
- reverse[]:Reverses the order of the list.
- copy[]: Returns a duplicate of the list.