1 min read
Create a Python program to multiply two numbers.
num_1 = 2
num_2 = 3
product = num_1 * num_2
print("Product of {} and {} is {}".format(num_1, num_2,product))
Product of 2 and 3 is 6
First, the two numbers are stored in the variables num_1
and num_2
, respectively. Multiplication in Python is done by ( *
) operator, Then the result is saved in the product variable and printed out using string formatting.
To learn more about string formatting in Python read - How To Use String Formatting In Python
However, the more memory-efficient way to perform a multiplication in Python is by not using any variables at all it can be done in a line, but it can make the code hard to read.
print(2*3)
6
Create a Python Program to multiply two numbers provided by the user in real-time.
num_1 = input("Enter the first number")
num_2 = input("Enter the second number")
product = float(num_1) * float(num_2)
print("Product of {} and {} is {}".format(num_1, num_2,product))
Enter the first number 2
Enter the second number 3
Product of 2 and 3 is 6.0
In this program first, we are taking user input with the built-in input()
function of Python then before multiplying the numbers we are converting the inputs to float type using the float()
function because the input()
function returns the object as a string.
To learn more about Python data types read the following articles.
PROGRAMSLatest from djangocentral
2 min read
2 min read