Found 34490 Articles for Programming

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

23K+ 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

155K+ 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

784 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

100 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

137 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

132 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

How to find exponential value in Python?

Vikram Chiluka
Updated on 09-Nov-2022 06:38:07

8K+ Views

In this article, we will show you how to find the exponential value in python. Below are the various methods to accomplish this task − Using exponential operator(**) Using exp() function Using exp() values with inbuilt numbers Using pow() function Using exponential operator(**) The exponential operator(**) can be used to calculate the power of a number. Since importing a module or calling a function is not necessary, this is the most convenient to use. The exponential value of an x is xth power of e, Euler's constant which is an irrational number called Euler's number and is equal ... Read More

How to Find Sum of Natural Numbers Using Recursion in Python?

Jayashree
Updated on 02-Mar-2020 10:01:42

3K+ Views

If a function calls itself, it is called a recursive function. In order to prevent it from falling in infinite loop, recursive call is place in a conditional statement.Following program accepts a number as input from user and sends it as argument to rsum() function. It recursively calls itself by decrementing the argument each time till it reaches 1.def rsum(n):     if n

Advertisements