Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles on Trending Technologies
Technical articles with clear explanations and examples
degrees() and radians() in Python
The measurements of angles in mathematics are done using two units: degrees and radians. They are frequently used in math calculations involving angles and need conversion from one value to another. In Python, we can achieve these conversions using built-in functions from the math module. degrees() Function This function takes a radian value as parameter and returns the equivalent value in degrees. The return type is a float value. Syntax math.degrees(x) Where x is the angle in radians. Example import math # Converting radians to degrees print("1 radian in ...
Read Morechr () in Python
The chr() function in Python returns a string representing a character whose Unicode code point is the integer supplied as parameter. For example, chr(65) returns the string 'A', while chr(126) returns the string '~'. Syntax The syntax of the chr() function is as follows ? chr(n) Where n is an integer value representing the Unicode code point. Basic Example The below program shows how chr() is used. We supply various integer values as parameters and get back the respective characters ? # Printing the strings from chr() function print(chr(84), chr(85), ...
Read MoreGenerating random number list in Python
There is a need to generate random numbers when studying a model or behavior of a program for different range of values. Python can generate such random numbers by using the random module. In the below examples we will first see how to generate a single random number and then extend it to generate a list of random numbers. Generating a Single Random Number The random() method in random module generates a float number between 0 and 1. Example import random n = random.random() print(n) Output Running the above code gives ...
Read Moregcd() function Python
The Greatest Common Divisor (GCD) is the largest positive integer that divides both numbers without leaving a remainder. Python provides the gcd() function in the math module to calculate this efficiently. Syntax math.gcd(x, y) Parameters: x − First integer y − Second integer Return Value: Returns the greatest common divisor as an integer. Example Let's find the GCD of different pairs of numbers ? import math print("GCD of 75 and 30 is", math.gcd(75, 30)) print("GCD of 48 and 18 is", math.gcd(48, 18)) print("GCD of 17 and 13 ...
Read Morefrozenset() in Python
The frozenset() function in Python creates an immutable set from an iterable. Unlike regular sets, frozensets cannot be modified after creation, making them useful when you need a collection that should remain unchanged. Syntax frozenset(iterable) Parameters: iterable (optional): Any iterable object like list, tuple, string, or set. If no argument is provided, returns an empty frozenset. Creating a Frozenset from a List Here's how to convert a mutable list to an immutable frozenset ? # Create a list days = ["Mon", "Tue", "Wed", "Thu"] print("Original list:", days) # ...
Read Morefloor() and ceil() function Python
Python's math module provides two useful functions for rounding decimal numbers to integers: floor() and ceil(). These functions help convert fractional numbers to their nearest integer values in different directions. floor() Function The floor() function returns the largest integer that is less than or equal to the given number. For positive numbers, it rounds down; for negative numbers, it rounds away from zero. Syntax math.floor(x) Where x is a numeric value Example of floor() Let's see how floor() works with different types of numeric values ? import math x, ...
Read MoreFinally keyword in Python
The finally keyword in Python defines a block of code that executes regardless of whether an exception occurs or not. This makes it useful for cleanup operations like closing files, database connections, or releasing resources. Syntax try: # Main Python code except ExceptionType: # Optional block to handle specific exceptions finally: # Code that always executes Example with Exception Handling Here's an example where a NameError occurs when trying to print an undefined variable ? try: ...
Read Moredivmod() in Python and its application
The divmod() function is part of Python's standard library which takes two numbers as parameters and returns the quotient and remainder of their division as a tuple. It is useful in many mathematical applications like checking for divisibility of numbers and establishing if a number is prime or not. Syntax divmod(a, b) Parameters: a − The dividend (number to be divided) b − The divisor (number that divides a) Both a and b can be integers or floats Return Value: Returns a tuple (quotient, remainder) Examples with Integers and Floats ...
Read Morecasefold() string in Python
The casefold() method in Python converts a string to lowercase, making it ideal for case-insensitive string comparisons. Unlike lower(), casefold() handles special Unicode characters more aggressively, making it the preferred choice for string matching. Syntax string.casefold() This method takes no parameters and returns a new string with all characters converted to lowercase. Basic Usage Here's how to convert a string to lowercase using casefold() − string = "BestTutorials" # print lowercase string print("Lowercase string:", string.casefold()) Lowercase string: besttutorials Case-Insensitive String Comparison You can compare two ...
Read MoreCapitalize first letter of a column in Pandas dataframe
A Pandas DataFrame is similar to a table with rows and columns. Sometimes we need to capitalize the first letter of strings in a specific column, which can be achieved using the str.capitalize() method. Creating a DataFrame Let's start by creating a DataFrame with columns for days and subjects ? import pandas as pd # Create a DataFrame df = pd.DataFrame({ 'Day': ['mon', 'tue', 'wed', 'thu', 'fri'], 'Subject': ['math', 'english', 'science', 'music', 'games'] }) print(df) Day ...
Read More