Found 10805 Articles for Python

How will you explain Python Operator Overloading?

Jayashree
Updated on 30-Jul-2019 22:30:22

138 Views

Every class in Python, whether built-in or user defined is inherited from object class. The object class has a number of properties whose name is preceded and followed by double underscores (__). Each of these properties is a wrapper around a method of same name. Such methods are called special or magic methods.The magic methods __lt__(), __gt__(), __eq__(), __ne__(), etc. are overridden in a class to overload == and != operators respectively.

How to perform square root without using math module in Python?

Vikram Chiluka
Updated on 28-Aug-2023 12:39:09

27K+ Views

In this article, we will show you how to perform square root without using a math module in Python. Below are the various methods to accomplish this task: Using exponential operator ** Using Mathematical Logic Using exponential operator ** Without using the math module, the simplest approach to find the square root of a number in Python is to use the built-in exponential operator ** (It is an exponent operator because it calculates the power of the first operand to the power of the second operand). Algorithm (Steps) Following are the Algorithm/steps to be followed to perform the desired ... Read More

How to calculate square root of a number in Python?

Vikram Chiluka
Updated on 27-Oct-2022 12:24:16

2K+ Views

In this article, we will show you how to calculate the square root of a number in Python. Below are the various methods to accomplish this task − Calculating square root using sqrt() Calculating the square root using the pow() function Calculating the square root using the exponent operator Calculating square root using the cmath module What is the square root of a number? The square root of a number is a value when multiplied by itself returns that same number. Example − 5 x 5 = 25, hence the square root of 25 is 5. However -5 ... Read More

How do you round up a float number in Python?

Vikram Chiluka
Updated on 26-Oct-2023 03:44:45

22K+ Views

In this article, we will show you how to round up a float number in Python. Round up float number using the round() function The round() function gives a floating point number with a specified number of decimals which is the rounded version of the specified number. The function will return the nearest integer because the default value for the number of decimals is 0. Syntax round(number, digits) Parameters number(required) − a number that should be rounded digits(optional) − up to the number of decimals to be rounded. 0 is the default. Python has an in-built function round() for ... Read More

How to round down to 2 decimals a float using Python?

Vikram Chiluka
Updated on 29-Aug-2023 07:46:09

148K+ Views

In this article, we will show you how to round off a floating number upto 2 decimals in python. Below are the various methods to accomplish this task: Using round() function Using format() function Using Decimal Module Using ceil() function Using round() function The round() function gives a floating point number with a specified number of decimals which is the rounded version of the specified number. The function will return the nearest integer because the default value for the number of decimals is 0. Syntax round(number, digits) Parameters number(required)- a number that should be rounded digits(optional)- ... Read More

How to Find the Power of a Number Using Recursion in Python?

Jayashree
Updated on 02-Mar-2020 10:16:14

746 Views

Following program accepts a number and index from user. The recursive funcion rpower() uses these two as arguments. The function multiplies the number repeatedly and recursively to return power.Exampledef rpower(num,idx):     if(idx==1):        return(num)     else:        return(num*rpower(num,idx-1)) base=int(input("Enter number: ")) exp=int(input("Enter index: ")) rpow=rpower(base,exp) print("{} raised to {}: {}".format(base,exp,rpow))OutputHere is a sample run −Enter number: 10 Enter index: 3 10 raised to 3: 1000

How to find power of a number in Python?

Vikram Chiluka
Updated on 28-Oct-2022 11:33:35

13K+ Views

In this article, we will show you how to find the power of a number in python. Below are the various methods to accomplish this task − Using for loop Using Recursion Using pow() function Using ** operator Using for loop Algorithm (Steps) Following are the Algorithm/steps to be followed to perform the desired task –. Create a function findPower() that returns the power of a Number. The function accepts number, and exponent value as parameters. Take a variable that stores the result and initialize its value with 1. To obtain the power, multiply the given ... Read More

How to find the fractional and integer parts of x in a two-item tuple in Python?

Jayashree
Updated on 02-Mar-2020 10:16:43

90 Views

The method modf() returns the fractional and integer parts of x in a two-item tuple. Both parts have the same sign as x. The integer part is returned as a float. First item in tuple is fractional part>>> import math >>> math.modf(100.73) (0.730000000000004, 100.0) >>> math.modf(-5.55) (-0.5499999999999998, -5.0)

How to find the value closest to negative infinity in Python?

Jayashree
Updated on 02-Mar-2020 10:18:38

124 Views

Although infinity doesn't have a concrete representation, the closest number to negative infinity is represented as return value of float() function with '-inf' as argument>>> a=float('-inf') >>> -inf

How to find the value closest to positive infinity in Python?

Jayashree
Updated on 02-Mar-2020 10:18:04

127 Views

Although infinity doesn't have a concrete representation, the closest number to infinity is represented as return value of float() function with 'inf' as argument>>> a=float('inf') >>> a inf

Advertisements