Trusted answers to developer questions

Gutha Vamsi Krishna

We'll learn how to square a number in Python. We can achieve this using a multiplication operator, an inbuilt function, or using an exponent operator.

We can get the square of a number by multiplying the number by itself.

#given number num = 10 #square of given number print(num*num)

Square of a number using multiplication operator

- Line 2: We declare and initialize the number.
- Line 4: We get the square of the given number and display it.

`math.pow()`

methodWe can get the square of a number using the `pow()`

method provided by the `math`

module. We'll pass the number to the first parameter and `2`

to the second parameter to get the square of the number.

import math #given number num = 10 #get square of a number print(math.pow(10,2))

Square of a number using math.pow() method

- Line 1: We import the module
`math`

, which provides the`pow()`

method. - Line 3: We declare the number and initialize it.
- Line 5: We use the
`pow()`

method to get the square of the given number,`num`

, by passing the number`num`

as the first parameter and`2`

as the second parameter, and then we'll print the returned result.

We can get the square of a number in python using the exponent operator(**) if we provide the exponent value as `2`

.

#given number num = 10 #square of given number using exponent operator print(num**2)

Square of a number using exponent operator

- Line 2: We declare and initialize the number.
- Line 4: We get the square of the given number using the exponent operator where the exponent value is
`2`

and display it.

RELATED TAGS

python

CONTRIBUTOR

Gutha Vamsi Krishna

RELATED COURSES

View all Courses

Keep Exploring

Related Courses