image
image
image

Squaring and Cubing in Python

image

Squaring a number-number**2

Cubing a number-number**3

Example

Start IDLE.

Navigate to the File menu and click New Window.

Type the following:

––––––––

image

Square of 3 in Python will be 3**2

Cube of 5 in Python will be 5**3

  1. Square of 3

Start IDLE.

Navigate to the File menu and click New Window.

Type the following:

number=3   #declaring  variable number and assigning value 3

square=number**2

print(square)  #Calling the print function to display what square has

  1. Cube of 5

Start IDLE.

Navigate to the File menu and click New Window.

Type the following:

number=5  #declaring  variable number and assigning value 5

cube=number**3

print(cube)  #Calling the print function to display what cube has

––––––––

image

Practice Exercise

Use python operators to write and run a python program that finds the following:

a.  Cube of 7

b.  Square of 15

c.  Cube of 6

d.  Square of 11

e.  Cube of 8

f.  Square of 13

Point to Note

We can still multiply 2 two times to get the square of 2. The reason for using the square and cube operators is to help us write compact and efficient code. Remember that the interpreter goes through each line including comments only that it ignores comments. Using the cube and square operators helps compact code and increase the efficiency of interpretation including troubleshooting as well as human readability of the code.