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 by Pradeep Elance
317 articles
classmethod() in Python
A class method receives the class itself as its first argument (conventionally named cls). This allows us to call the method on the class without first creating an instance. We use the @classmethod decorator before the method declaration to define a class method. Key Features of Class Methods A classmethod is bound to a class and does not depend on the instantiation of a class to be used. A classmethod can modify class attributes which propagate to all instances of the class. The first parameter is always the ...
Read MorePython eval()
The eval() method parses the expression passed to it and runs the expression within the program. In other words, it interprets a string as code inside a Python program. Syntax The syntax for eval() is as below − eval(expression, globals=None, locals=None) Where expression − The Python expression passed to the method as a string. globals − A dictionary of available global methods and variables (optional). locals − A dictionary of available local methods and variables (optional). Basic Example Here's a simple example showing ...
Read MoreHelp function in Python
Python provides a built-in help() function that gives you access to the interactive help system. This function displays documentation for modules, functions, classes, and other objects directly in your Python environment. Syntax help(object) help('string') help() Where: object − Any Python object (function, module, class, etc.) 'string' − String name of a module or keyword help() − Starts interactive help mode Getting Help on Built-in Functions You can get help on any built-in function by passing it directly to help() ? help(len) Help on built-in function len ...
Read Moreexec() in Python
The exec() function in Python allows you to dynamically execute Python code at runtime. It can execute code passed as a string or compiled code object, making it useful for scenarios where code needs to be generated and executed programmatically. Syntax exec(object, globals, locals) Where: object − A string containing Python code or a compiled code object globals − Optional dictionary defining the global namespace (default: current globals) locals − Optional dictionary defining the local namespace (default: current locals) Basic String Execution The simplest use case is executing a string ...
Read Moredegrees() 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 More