Python Tutorials

Machine Learning for Beginners in Python: Apply Operations To Elements

Apply Operations To Elements Preliminaries import numpy as np Create Matrix matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) Create Vectorized Function add_100 = lambda i: i + 100 vectorized_add_100 = np.vectorize(add_100) Apply Function To Elements vectorized_add_100(matrix) array([[101, 102, 103], [104, 105, 106], [107, 108, 109]])   Python Example for Beginners Special …

Machine Learning for Beginners in Python: How to Create A Matrix

Create A Matrix Preliminaries import numpy as np Create Matrix matrix = np.array([[1, 4], [2, 5]]) Note NumPy’s mat data structure is less flexible for this purposes and should be avoided.   Python Example for Beginners Special 95% discount 2000+ Applied Machine Learning & Data Science Recipes Portfolio Projects for Aspiring Data Scientists: Tabular Text & …

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. …