TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to PYTHON

Python pow Example, Power Operator

Use pow along with the power operator that has 2 stars. Pow can be used with 3 arguments to do modulo division.
Pow. In exponentiation, one number is multiplied by itself. This is performed with the pow method, a built-in. With two stars, we can also take exponents.NumbersBuilt-ins
In Python, two stars and pow are equivalent. So we can use whichever one seems clearest. I feel pow is clearer, but I rarely need exponents in programs.
First example. Here we use pow and the exponent operator. We see that 2 squared is 4 (because 2 times 2 is four). You should know this from math class.

Here: We use both the two-star operator and the pow method. We see the results of both syntax forms are equal.

Python program that uses pow, exponents # Two squared is four. # ... These syntax forms are identical. print(2 ** 2) print(pow(2, 2)) # Two cubed is eight. print(2 ** 3) print(pow(2, 3)) Output 4 4 8 8
3 arguments. Pow can be used with 3 arguments. The third argument is for modulo division. The result of the exponentiation operation is divided by that number. The remainder is returned.

Here: We take the square of 3 to get 9. And then we divide nine by five, so we have a remainder of 4.

Note: This version of pow may improve performance. But I have never needed this in a real Python program.

Python program that uses pow with 3 arguments # Compute 3 squared. # ... This is 9. # ... Perform 9 modulo 5 to get result of 4. result = pow(3, 2, 5) print(result) Output 4
Python documentation. The Python documentation is pretty good. Let's see what it says about pow. It states that pow and the two-star operator are equivalent.

Quote: The two-argument form pow(x, y) is equivalent to using the power operator.

Built-in Functions: python.org
A main goal of Python is syntax clarity. Python programs are easy to read and to write. With pow, we have a choice of syntax to use.
For developers who use many exponentiation operations, two stars might be clearer. But pow is often a less-common operation. For this reason it is probably a better choice overall.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf