Hướng dẫn power of two python
In this example, you will learn to compute the power of a number. Show
Nội dung chính
Nội dung chính
To understand this example, you should have the knowledge of the following Python programming topics:
Example 1: Calculate power of a number using a while loop
Output Answer = 81 In this program, Using the while loop, we keep on multiplying the In this case, we multiply Example 2: Calculate power of a number using a for loop
Output Answer = 81 Here, instead of using a while loop, we've used a for loop. After each iteration, the exponent is decremented by 1, and the result is multiplied by the base exponent number of times. Both programs above do not work if you have a negative exponent. For that, you need to use the Example 3: Calculate the power of a number using pow() function
Output Answer = 0.012345679012345678
Exponentiation is a mathematical operation where a value is multiplied a certain number of times with itself. Let’s see how we perform that task in Python. IN THIS ARTICLE:
# Calculate exponents in the Python programming languageIn mathematics, an exponent of a number says how many times that number is repeatedly multiplied with itself (Wikipedia, 2019). We usually express that operation as bn, where b is the base and n is the exponent or power. We often call that type of operation “b raised to the n-th power”, “b raised to the power of n”, or most briefly as “b to the n” (Wikipedia, 2019). Python has three ways to exponentiate values:
Since each approach gives the right answer, feel free to pick any. If you aren’t sure, use Let’s take a closer look at each option Python offers. # Calculate Python exponents with the ** operatorThe first way to raise a number to a power is with Python’s The The # Example: do exponentiation in Python with **Let’s see how
we can use the
Here we first make five different variables. We name them Then we raise each variable to a certain exponent with the The last
bit of code outputs the original and exponentiated value with Python’s
# Calculate Python exponents with the pow() functionAnother way to exponentiate values is with the built-in So to calculate 32, we use the
By the way, the # Example: raise numbers to a power with pow()Let’s look at a Python program that uses the
First we make five different variables. They are positive, negative, and there’s a
floating-point value. We name those variables Then we raise each variable to a particular power. For that we call the Next several
# Raise numbers to a power with Python’s math.pow()Python’s So why another way to exponentiate values? What makes A quick example of
Here’s how Also, # Example: raise numbers to a power with math.pow()To see how the
Before we can use the Next we raise each variable to a certain power. For that we call Then we output the original and exponentiated value with Python’s
# Process a list or array: calculate the exponent for each valueIn the examples above we each time raised a single value to a certain exponent. But what if we want to exponentiate a list or array of values? Let’s find out. # Exponentiate list values with Python’s list comprehensionOne way to raise each list value to a particular power is with a list comprehension. This requires just a little bit of code and runs efficiently. Here’s how we do that:
This code first makes a list of integer and floating-point numbers (named Then we make a list comprehension. Here the That’s how our list comprehension processes the entire list, executing The last bit of code has the
By the way, making a second list isn’t always needed. When you don’t have to keep the original values, you can overwrite the list with its exponentiated values. For example:
# Raise values to a power with Python’s for loopThe Here’s how we raise values to a particular value with a regular
This mini-program makes three lists. The first, Then we make a Inside the loop we add a new value to the After the loop ends we populated the
If you don’t need to keep the original list, you can also overwrite its values with the exponentiated result. For that we can also use the
LEARN MORE
# SummaryExponentiation (bn) is the mathematical operation that multiples a number (b) a certain number of times (n) with itself. There are three ways to program that behaviour in Python. The power operator ( The built-in The ReferencesMatthes, E. (2016). Python Crash Course: A Hands-On, Project-Based Introduction to Programming. San Francisco, CA: No Starch Press. Python.org (n.d. a). Built-in Functions. Retrieved on October 22, 2019, from https://docs.python.org/3.8/library/functions.html Python.org (n.d. b). math — Mathematical functions. Retrieved on October 22, 2019, from https://docs.python.org/3.8/library/math.html Python.org (n.d. c). Expressions. Retrieved on October 30, 2019, from https://docs.python.org/3.8/reference/expressions.html Sweigart, A. (2015). Automate The Boring Stuff With Python: Practical Programming for Total Beginners. San Francisco, CA: No Starch Press. Wikipedia (2019, October 26). Exponentiation. Retrieved on October 30, 2019, from https://en.wikipedia.org/wiki/Exponentiation Published December 20, 2019. « All Python math articles How do you find the power of two numbers in Python?Power Python: pow() Method. Python includes a built-in function that can be used to calculate powers: pow(). pow() accepts three parameters: a base number, an exponent to which the base is raised, and a modulo operator. The pow() method calculates a certain number to the power of another number. How do you write 2 power N in Python?Python pow() function returns the result of the first parameter raised to the power of the second parameter. How do you find the power of a number in Python?How to find the power of a number in Python. import math. print(math. pow(4,2)) Run. Importing math module in Python.. def power(n,e): res=0. for i in range(e): res *= n. return res. print(pow(4,2)) Run. ... . def power(n, e): if e == 0: return 1. elif e == 1: return n. else: return (n*power(n, e-1)). What does POW () do in Python?The pow() function returns the value of x to the power of y (xy). If a third parameter is present, it returns x to the power of y, modulus z. |