Beginner’s Guide to Python

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

Hits: 10 Python Set issuperset() Method Determines whether all items in the specified set are present in the original set Usage The issuperset() method returns True if all items in the specified set are present in the original set, otherwise FALSE. Syntax set.issuperset(set) Parameter Condition Description set Required A set to search for common items in Basic Example # Check …

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

Hits: 3 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 …

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

Hits: 8 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 …

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

Hits: 4 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 …

Python Built-in Methods – Python abs() Function

Hits: 8 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. …

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

Hits: 7 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 = …

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

Hits: 8 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 …

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

Hits: 6 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 …

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

Hits: 14 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’ …

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

Hits: 17 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 …

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

Hits: 15 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) # …

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

Hits: 4 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 …

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

Hits: 4 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 …

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

Hits: 34 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 …