Python

Python Built-in Methods – Python List pop() Method

Python List pop() Method Removes an item at specified index Usage The pop() method removes a single list item at specified index and returns it. If no index is specified, pop() method removes and returns the last item in the list. Syntax list.pop(index) Parameter Condition Description index Optional An index of item you want to remove. Default value is -1 Return Value The pop() method …

Python Built-in Methods – Python List index() Method

Python List index() Method Searches the list for a given item Usage The index() method searches for the first occurrence of the given item and returns its index. If specified item is not found, it raises ‘ValueError’ exception. The optional arguments start and end limit the search to a particular subsequence of the list. Syntax list.index(item,start,end) Parameter Condition Description item Required Any item (of type string, …

Python Built-in Methods – Python List count() Method

Python List count() Method Counts the number of occurrences of an item Usage Use count() method to find the number of times the specified item appears in the list. Syntax list.count(item) Parameter Condition Description item Required Any item (of type string, list, set, etc.) you want to search for. Examples # Count number of occurrences of ‘red’ L = [‘red’, …

Python Built-in Methods – Python List clear() Method

Python List clear() Method Removes all items from the list Usage Use clear() method to remove all items from the list. This method does not return anything; it modifies the list in place. Syntax list.clear() Basic Example L = [‘red’, ‘green’, ‘blue’] L.clear() print(L) # Prints [] Please note that clear() is not same as assigning an empty list L = …

Python Built-in Methods – Python List append() Method

Python List append() Method Appends an item to a list Usage The append() method adds a single item to the end of the list. This method does not return anything; it modifies the list in place. Syntax list.append(item) Parameter Condition Description item Required An item you want to append to the list Examples # Append ‘yellow’ L = [‘red’, ‘green’, …

Python Built-in Methods – Python tuple() Function

Python tuple() Function Creates a tuple from an iterable Usage The tuple() function creates a tuple from an iterable. The iterable may be a sequence (such as a string, list or range) or a collection (such as a dictionary, set or frozen set) Syntax tuple(iterable) Parameter Condition Description iterable Required A sequence or a collection Examples tuple() with no arguments creates an empty tuple. T = tuple() print(T) # …

Python Built-in Methods – Python sorted() Function

Python sorted() Function Sorts the items of an iterable Usage The sorted() method sorts the items of any iterable You can optionally specify parameters for sort customization like sorting order and sorting criteria. Syntax sorted(iterable,key,reverse) The method has two optional arguments, which must be specified as keyword arguments. Parameter Condition Description iterable Required Any iterable (list, tuple, dictionary, set …

Python Built-in Methods – Python pow() Function

Python pow() Function Raises a number to a power Usage The pow(x, y) function calculates the value of x to the power of y ( x y ). If a third argument z is specified, pow(x, y, z) function returns x to the power of y, modulus z  ( x y % z ). Syntax pow(x,y,z) Parameter Condition Description x Required The base y Required The exponent z Optional The …

Python Built-in Methods – Python min() Function

Python min() Function Returns the smallest item Usage The min() function can find the smallest of two or more values (such as numbers, strings etc.) the smallest item in an iterable (such as list, tuple etc.) With optional key parameter, you can specify custom comparison criteria to find minimum value. Syntax min(val1,val2,val3… ,key) Parameter Condition Description val1,val2,val3… Required Two or more values to compare key …

Python Built-in Methods – Python max() Function

Python max() Function Returns the largest item Usage The max() function can find the largest of two or more values (such as numbers, strings etc.) the largest item in an iterable (such as list, tuple etc.) With optional key parameter, you can specify custom comparison criteria to find maximum value. Syntax max(val1,val2,val3… ,key) Parameter Condition Description val1,val2,val3… Required Two or more values to compare key …