Beginner’s Guide to 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 copy() Method

Python List copy() Method Copies the list shallowly Usage The copy() method returns the Shallow copy of the specified list. Syntax list.copy() Basic Example # Create a copy of list ‘L’ L = [‘red’, ‘green’, ‘blue’] X = L.copy() print(X) # Prints [‘red’, ‘green’, ‘blue’] copy() vs Assignment statement Assignment statement does not copy objects. For example, old_List = [‘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 zip() Function

Python zip() Function Combines multiple iterables together Usage The zip() function combines items from each of the specified iterables. The return value is a list of tuples where the items of each passed iterable at same index are paired together. Syntax zip(iterables) Parameter Condition Description iterables Optional One or more iterables (list, tuple, dictionary etc.) to be joined together Basic Example …

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 slice() Function

Python slice() Function Returns a slice object to slice a sequence Usage The slice() function returns a slice object. A slice object is used to specify how to slice a sequence (list, tuple, string or range etc.) . You can specify where to start the slicing, where to stop and specify the step. Syntax slice(start,stop,step) Parameter Condition Description start Optional A number to specify start of the slicing. …