In this post, we will see how to calculate the square and cube of every number of the given list of numbers. We will use map[] along with lambda expression for calculation.
Algorithm
Step 1: Declare a list of numbers
Step 2: Find the square of number by multiplying the number itself two times.
Step 3: Print the numbers with square value
Step 4: Find the cube of number by multiplying
the number itself three times.
Step 5: Print the numbers with cube value
Step 6: End
Example
Input [1, 2, 3, 4, 5] Output Square of every number of the provided list: [1, 4, 9, 16, 25] Cube of every number of the provided list: [1, 8, 27, 64, 125]
Program
numbers = [1, 2, 3, 4, 5] print["List of numbers:"] print[numbers] print["\nSquare of every number of the provided list:"] square_numbers = list[map[lambda x: x ** 2, numbers]] print[square_numbers] print["\nCube of every number of the provided list:"] cube_numbers = list[map[lambda x: x ** 3, numbers]] print[cube_numbers]
Output
List of numbers: [1, 2, 3, 4, 5] Square of every number of the provided list: [1, 4, 9, 16, 25] Cube of every number of the provided list: [1, 8, 27, 64, 125]
HomePython ProgrammingPrint Square and Cube of Given number in Python
Write a program in python to enter a number and print its square and cube?
num=int[input["Enter any integer number: "]]
sq=num**2
print["Square of Given number: ",sq]
cube=num**3
print["Cube of Given number: ",cube]
Output:-
Enter any integer number: 5
Square of Given number: 25
Cube of Given number: 125
Post a Comment
0 Comments
View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
Given a number, the task is to write a Python program to calculate square of the given number.
Examples:
Input: 4 Output: 16 Input: 3 Output: 9 Input: 10 Output: 100
We will provide the number, and we will get the square of that number as output. We have three ways to do so:
- Multiplying the number to get the square [N * N]
- Using Exponent Operator
- Using math.pow[] Method
Method 1: Multiplication
In this approach, we will multiply the number with one another to get the square of the number.
Example:
Python3
n
=
4
square
=
n
*
n
print
[square]
Output:
16
Method 2: Using Exponent Operator
In this approach, we use the exponent operator to find the square of the number.
Exponent Operator: **
Return: a ** b will return a raised to power b as output.
Example:
Python3
n
=
4
square
=
n
*
*
2
print
[square]
Output:
16
Method 3: Using pow[] Method
In this approach, we will use the pow[] method to find the square of the number. This function computes x**y and returns a float value as output.
Syntax: float pow[x,y]
Parameters :
x : Number whose power has to be calculated.
y : Value raised to compute power.
Return Value :
Returns the value x**y in float.
Example:
Python3
n
=
4
square
=
pow
[n,
2
]
print
[square]
Output:
16.0