Python Tutorials

learn Python By Example – Dictionary Basics

Hits: 16 Dictionary Basics Basics Not sequences, but mappings. That is, stored by key, not relative position. Dictionaries are mutable.   Build a dictionary via brackets unef_org = {‘name’ : ‘UNEF’, ‘staff’ : 32, ‘url’ : ‘http://unef.org’} View the variable unef_org {‘name’: ‘UNEF’, ‘staff’: 32, ‘url’: ‘http://unef.org’} Build a dict via keys who_org = {} …

Learn Python By Example – Data Structure Basics

Hits: 4 Data Structure Basics Lists “A list is a data structure that holds an ordered collection of items i.e. you can store a sequence of items in a list.” – A Byte Of Python Lists are mutable. /* Create a list of countries, then print the results */ allies = [‘USA’,’UK’,’France’,’New Zealand’, ‘Australia’,’Canada’,’Poland’]; allies …

Machine Learning for Beginners in Python: Support Vector Classifier

Hits: 8 Support Vector Classifier There is a balance between SVC maximizing the margin of the hyperplane and minimizing the misclassification. In SVC, the later is controlled with the hyperparameter C, the penalty imposed on errors. C is a parameter of the SVC learner and is the penalty for misclassifying a data point. When C is …

Machine Learning for Beginners in Python: How to Handle Imbalanced Classes In Logistic Regression

Hits: 6 Handling Imbalanced Classes In Logistic Regression Preliminaries /* Load libraries */ from sklearn.linear_model import LogisticRegression from sklearn import datasets from sklearn.preprocessing import StandardScaler import numpy as np Load Iris Flower Dataset /* Load data */ iris = datasets.load_iris() X = iris.data y = iris.target Make Classes Imbalanced /* Make class highly imbalanced by …

Machine Learning for Beginners in Python: Apply Operations To Elements

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

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

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

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

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

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

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

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

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

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

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

Python Built-in Methods – Python zip() Function

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

Python Built-in Methods – Python tuple() Function

Hits: 5 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() …

Python Built-in Methods – Python sorted() Function

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

Python Built-in Methods – Python slice() Function

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

Python Built-in Methods – Python range() Function

Hits: 3 Python range() Function Generates a sequence of numbers within a given range Usage The range() function generates a sequence of numbers from start up to stop, and increments by step. When used in a for loop, range() provides a simple way to repeat an action a specific number of times. for x in range(3): print(‘Hello!’) # Prints Hello! Hello! Hello! Syntax range(start,stop,step) …