def multiply_by_two(number):
"""This function takes a number and prints its double."""
result = number * 2
print("Double of", number, "is", result)
multiply_by_two(5)
multiply_by_two(10)
#source code --> clcoding.com
Code Explanation:
Function Definition
def multiply_by_two(number):
def defines a function in Python.
multiply_by_two is the name of the function.
(number) is the parameter that the function accepts when called.
Function Docstring (Optional but Recommended)
"""This function takes a number and prints its double."""
This is a docstring, used for describing what the function does.
Helps in understanding the function when reading or debugging the code.
Multiply the Input by 2
result = number * 2
The input number is multiplied by 2 and stored in the variable result.
Example Calculation:
If number = 5, then result = 5 * 2 = 10.
If number = 10, then result = 10 * 2 = 20.
Print the Result
print("Double of", number, "is", result)
This prints the original number and its double.
Example Output:
Double of 5 is 10
Double of 10 is 20
Calling the Function
multiply_by_two(5)
multiply_by_two(10)
multiply_by_two(5) passes 5 as input, prints Double of 5 is 10.
multiply_by_two(10) passes 10 as input, prints Double of 10 is 20.
0 Comments:
Post a Comment