• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • HTML
    • JavaScript
    • jQuery
    • PHP
    • Python
    • SAS
    • Ruby
  • About
You are here: Home / Python / Squaring in Python – Square a Number Using Python math.pow() Function

Squaring in Python – Square a Number Using Python math.pow() Function

January 29, 2022 Leave a Comment

To square a number in Python, the easiest way is to multiply the number by itself.

square_of_10 = 10*10

We can also use the pow() function from the math module to square a number.

import math

square_of_10 = math.pow(10,2)

Finally, we can find the square of a number in Python with the built in exponentiation operator **.

square_of_10 = 10**2

Squaring numbers is a common task to do when working with numbers in a program. In Python, we can easily square a number.

By definition, the square of a number is the number multiplied by itself. We can calculate the square of a number in Python by multiplying it by itself.

Below are some examples of how to find the squares of various numbers in Python

print(4*4)
print(9*9)
print(13*13)
print(90*90)
print(2182*2182)

#Output:
16.0
81.0
169.0
8100.0
4761124.0

Squaring a Number in Python with the math module pow() Function

The Python math module has many powerful functions which make performing certain calculations in Python very easy.

One such calculation which is very easy to perform in Python is finding the square of a number.

The pow() function from the Python math module also lets us compute the square of a number.

The pow() function takes two numbers as input, the first number is the base and the second number is the exponent.

For squaring in Python, we pass “2” to the second parameter in the pow() function.

Below are some examples of how to use the pow() function to find the squares of various numbers.

import math

print(math.pow(4, 2))
print(math.pow(9, 2))
print(math.pow(13, 2))
print(math.pow(90, 2))
print(math.pow(2182, 2))

#Output:
16.0
81.0
169.0
8100.0
4761124.0

Finding the Square of a Number with the ** Operator in Python

We can also use the built in ** to find exponents in Python. To find a square with the built in exponentiation operator ** , we just put “2” after **.

Below are some examples of how to use the Python built in ** operator to find the squares of different numbers.

import math

print(4**(2))
print(9**(2))
print(13**(2))
print(90**(2))
print(2182**(2))

#Output:
16.0
81.0
169.0
8100.0
4761124.0

Hopefully this article has been useful for you to learn how to square numbers in Python.

Other Articles You'll Also Like:

  • 1.  How to Subtract Two Numbers in Python
  • 2.  Adjusting Python Turtle Screen Size with screensize() Function
  • 3.  Floor in Python – Find Floor of Number with math.floor() Function
  • 4.  for char in string – How to Loop Over Characters of String in Python
  • 5.  Flatten List of Tuples in Python
  • 6.  How to Remove All Spaces from String in Python
  • 7.  Using Matplotlib and Seaborn to Create Pie Chart in Python
  • 8.  Python Get Operating System Information with os and platform Modules
  • 9.  pandas startswith() – Check if String Starts With Certain Characters
  • 10.  Remove Empty Lists from List in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy