Write a program using a function that takes a number
as argument and calculates cube for it. The function
does not return a value. If there is no value passed to
the function in function call, the function should
calculate cube of 2. Python
Answers
PYTHON PROGRAM FOR CUBE CALCULATION OF A NUMBER.
Language used : Python Programming
Program :
def cubecalculation(num=2): #default value = 2
cube=num**3 #else, just write print(num**3)
print(cube)
num=int(input())
cubecalculation(num) #calling with parameter
cubecalculation() #calling without parameter
Input :
30
Output :
27000 #Output for the function call with 30 as its parameter
8 #Output for the function call wih no parameter
Explanation :
- ** is an power operation. the left side operand will be the number on which the power operation is performed, while the right operand will be the power value.
- If you use var=value in function definition's parameter passage, it will be act as a default value. Means, if the function is called without any parameter, the default value will be considered as a parameter. Always remember that default values should always be placed on the last arguments of the function.
- Now, after writing a function definition, take an integer input from the user and store it in a value.
- If you pass that value as a parameter during function calling, the function definition takes it and performs cube operation on it. Here, as we take 30 as input, passed it; the operation goes as :
30**3 = 27000
- If you send no parameter during the call, 2 will be automatically taken as a parameter, as we arranged it as a default value, and the function performs cube operation on it, as :
2**3 = 8
Learn more :
1. Write a simple python function to increase the value of any number by a constant "C=5" and write another simple function to update the value of "C" to any other value like 6,7,8, etc.
https://brainly.in/question/16334005
2. Indentation is must in python. Know more about it at :
brainly.in/question/17731168
Answer:
(i)
#definition of the program
def cube(n=2):
cube=n^3
print("Cube of",n,"is=",cube)
#main program
num=input("Enter any number (Press enter to input nothing):")
if num=="":
cube()
else:
cube(int(num))
(ii)
#definition of the function
def compare(val1,val2):
if val1==val2:
return True
else:
return False
#main program
v1=input("Enter any character:")
v2=input("Enter another character:")
print("Both are equal:",compare(v1,v2))
Explanation: