Beginner’s Guide to Python

Python Built-in Methods – Python Set issubset() Method

Python Set issubset() Method Determines whether all items in the set are present in the specified set Usage The issubset() method returns True if all items in the set are present in the specified set, otherwise FALSE. In set theory, every set is a subset of itself. For example, A.issubset(A) is True. Syntax set.issubset(set) Parameter Condition Description set Required A set to …

Python Built-in Methods – Python Set intersection_update() Method

Python Set intersection_update() Method Updates the set by removing the items that are not common Usage The intersection_update() method updates the set by removing the items that are not common to all the specified sets. You can specify as many sets as you want, just separate each set with a comma. If you don’t want to update the original set, use intersection() method. …

Python Built-in Methods – Python Set discard() Method

Python Set discard() Method Removes a specified item from the set Usage The discard() method removes a specified item from the set. Syntax set.discard(item) Parameter Condition Description item Required An item you want to remove from the set Examples # Remove ‘red’ from the set S = {‘red’, ‘green’, ‘blue’} S.discard(‘red’) print(S) # Prints {‘blue’, ‘green’} If specified item doesn’t …

Python Built-in Methods – Python abs() Function

Python Set difference() Method Returns a new set with items in the set that are not in other sets Usage The difference() method returns a new set of items that are in the original set but not in any of the specified sets. You can specify as many sets as you want, just separate each set with a comma. If you …

Python Built-in Methods – Python Set copy() Method

Python Set copy() Method Copies the set shallowly Usage The copy() method returns the Shallow copy of the specified set. Syntax set.copy() Basic Example # Copy set ‘S’ to ‘x’ S = {‘red’, ‘green’, ‘blue’} x = S.copy() print(x) # Prints {‘blue’, ‘green’, ‘red’} copy() vs Assignment statement Assignment statement does not copy objects. For example, old_Set = {‘red’, ‘green’, …

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

Python Set clear() Method Removes all items from the set Usage Use clear() method to remove all items from the set. The method does not return anything; it just updates the set. Syntax set.clear() Basic Example # Clear the set S = {‘red’, ‘green’, ‘blue’} S.clear() print(S) # Prints set()   Python Example for Beginners Special 95% discount …

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

Python Tuple index() Method Searches the tuple 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 tuple. Syntax tuple.index(item,start,end) Parameter Condition Description item Required Any item you want to …

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

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

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

Python List sort() Method Sorts the items of the list Usage Use sort() method to sort the items of the list. You can optionally specify parameters for sort customization like sorting order and sorting criteria. Syntax list.sort(key,reverse) Parameter Condition Description key Optional A function to specify the sorting criteria. Default value is None. reverse Optional Settting it to …

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

Python List reverse() Method Reverses the order of the list Usage The reverse() method reverses the order of list. This method does not return anything; it modifies the list in place. Syntax list.reverse() Examples L = [‘red’, ‘green’, ‘blue’] L.reverse() print(L) # Prints [‘blue’, ‘green’, ‘red’] L = [1, 2, 3, 4, 5] L.reverse() print(L) # Prints [5, …