Python Dictionary get() Method

Python dictionary key() method “retrieves the value associated with a given key”. If the key is not found in the dictionary, it returns a default value that can be specified as an optional second argument. If no default value is provided, it returns None. Syntax dictionary.get(keyname, value) Parameters The keyname name parameter and the keyname … Read more

Python enumerate() Function

Python enumerate() is a built-in method that “adds a counter to an iterable and returns the enumerated object”. The enumerate() function takes a collection (e.g., a tuple) and returns it as an enumerate object. The enumerate() function adds a counter as the key of the enumerate object. Syntax enumerate(iterable, start) Parameters An iterable parameter is an iterable object. The … Read more

Python Requests Module

Python’s “requests” module makes HTTP requests to web services or APIs. It provides a simple and convenient way to send HTTP requests and handle responses, making it easier to interact with web services than using the built-in urllib library. To get started with the requests library, you must install it first, as it is not … Read more

Python eval() Method

Python eval() is a built-in function that “evaluates a string as a Python expression and returns the result of the expression”. It can be helpful for dynamically executing code or evaluating user-supplied expressions in a controlled manner. Syntax eval(expression, globals=None, locals=None) Parameters expression – this string is parsed and evaluated as a Python expression. globals (optional) – … Read more

Python Generators

Python generators are simple “functions that return an iterable set of items, one at a time, in a unique way”. It “creates the iterators but with a different approach”. If the function contains at least one yield statement (it may include other yield or return statements), it becomes a Generator function. The main difference between normal functions and Generators is that … Read more

Python String count() Method

Python string count() is a built-in function that “returns the number of occurrences of the substring in the given string”. The method searches the substring in the given string and returns how many times it is present. It takes optional parameters to start and end to specify the starting and ending positions in the string, respectively. Syntax … Read more

How to Generate a Random Number in Python

To Generate a Random Number in Python, you can use the “random” module. Generate a random floating-point number between 0 (inclusive) and 1 (exclusive) using the random() function import random random_float = random.random() print(random_float) Output 0.34926230545840775 Generate a random integer between a given range (inclusive) using the randint() function import random random_integer = random.randint(1, 10) … Read more

What is the Boolean Indexing in Pandas

Boolean indexing in Pandas is a method used to filter data in a DataFrame or Series by specifying a condition that returns a boolean array. This boolean array is then used to index the original DataFrame or Series. Only the rows (or elements) corresponding to True values in the boolean array are retained in the … Read more

How to Sort by Column Values in Pandas DataFrame

To sort a Pandas DataFrame by column values, you can use the “sort_values()” method. The “sort_values()” function sorts the data frame in ascending or descending order of the provided column. It differs from the “sorted()” function since it cannot sort a data frame, and a specific column cannot be selected. Syntax Series.sort_values(axis=0, ascending=True, inplace=False, kind=’quicksort’, … Read more

Pandas value_counts() Method

The value_counts() method in Pandas is used to compute the frequency distribution of unique values in a Pandas Series (a one-dimensional labeled array). It returns a new Series object where the index contains the unique values, and the data contains the counts of each unique value, sorted by counts in descending order by default. Syntax … Read more

Python Stack

Python Stack is an Abstract data type that stores an Item in the order in which they were added. Basic Stack Operations are the following. Add item to the Stack. Remove the item from the Stack. How many items are there in the Stack? Example class Stack: def __init__(self): self.items = [] def push(self, item): … Read more