The square of a number is that number multiplied by itself. But what are the ways to do that in the Python programming language? Let’s find out. Show
IN THIS ARTICLE:
# Three ways to square numbers in PythonIn mathematics, a square is the result of multiplying a number by itself (Wikipedia, 2019). That multiplication is done just once, like so: n x n. That makes squaring the same as raising a number to the power of 2. For example, 9 x9 is 81 just as 9 squared (92) is 81 as well. One feature of squares is that they’re always positive. This is because a negative times another negative always gives a positive. For instance, -9 squared, or (-9)2, is also 81. There are several ways to square a number in Python:
Each approach gives the correct answer, and one isn’t better than the others. Just pick the one you like the most. Let’s see how each option works in Python. # Square a Python number with the exponent operator: **The first way to square a number is with Python’s exponent ( So we type the number to square, then # Example: square Python numbers with exponent operatorLet’s see how squaring with
This mini-program first makes five variables, named We square the value of each variable next. For that we use the variable’s
name, the Then we call Python’s
# Square a Python number with the pow() functionAnother way to square numbers is with the built-in For example, to square 3 we type: We can also square a number with the # Example: square values with Python’s pow() functionHere’s how we use the
Here we first make 5 different
variables ( Then we square each of those variables. For that we call The last bit of code outputs the original and squared value. To do that we call the
# Square a number in Python with multiplicationA square is just a number multiplied by itself. And so another option is to do that multiplication with the So to square # Example: square Python values by multiplyingSo to square values with the
We first make five variables, That squaring is what we do next. We multiply each variable with itself (for instance, Then we output both the original and its squared value with several
# Square all values in a Python list or arrayThe previous examples all squared a single value at a time. But sometimes we have a list or array whose values we need to square. Let’s look at two possible approaches for that. # Square multiple values with a list comprehensionOne option that squares a sequence of values is a list comprehension. Those runs efficient and requires just a bit of code. Here’s how a list comprehension can square each value in a list:
This example first makes a list
named Then we generate a new list with a list comprehension. The code between square brackets ( Those After that list comprehension the
Of course, we could also squared the values in a different way. For instance, with simple multiplication:
Don’t need to keep the original values? Then a list comprehension can also overwrite the existing list with squared values. For that assign the list the outcome of the list comprehension. For instance:
# Square values with Python’s for loopAnother option that squares values is Python’s Here’s how a
Here we first make the Next a Inside the loop we call the Finally we output both the original and square list with the
Above we stored the squared values in a new list. If you don’t have to keep the original list, you can also overwrite it with squared values. When we do that with the
LEARN MORE
# SummaryA square is a number multiplied by itself. Python has three ways to square numbers. The first is the exponent or power ( To square a sequence of numbers we can use a list comprehension or regular ReferencesLutz, M. (2013). Learning Python (5th Edition). Sebastopol, CA: O’Reilly Media. Matthes, E. (2016). Python Crash Course: A Hands-On, Project-Based Introduction to Programming. San Francisco, CA: No Starch Press. Wikipedia (2019, September 6). Square (algebra). Retrieved on September 18, 2019, from https://en.wikipedia.org/wiki/Square_(algebra) Published December 20, 2019. « All Python math articles How do you square each element in Python?square() in Python. numpy. square(arr, out = None, ufunc 'square') : This mathematical function helps user to calculate square value of each element in the array.
How do you put a squared on Python?Squaring numbers in Python:
Using the Exponent Operator. Multiplying the number by itself (n*n) Using pow()
|