
Syntax: Multiplication
Multiply two numbers together with the Python multiplication operator.
Multiplication in Python
The next operator you will be learning about is the multiplication operator. When you want to multiply a numeric value by another numeric value, the multiplication operator is the one to use. In Python, as in many other languages, the * symbol represents the multiplication operator.
Typical syntax usage for the multiplication operator are as follows:
-
When multiplying one number by another:
product = 7 * 6 -
When multiplying a variable value by a number:
val = 32 product = val * 5 -
When multiplying one variable value by another:
val1 = 50 val2 = 25 product = val1 * val2
Practice: Multiplying Integers
Based on the above information, please complete the following steps:
-
Define a variable named
days_in_a_yearand assign it365. -
Define a variable named
days_in_a_decadeand, using the multiplication operator anddays_in_a_year, assign it the number of days in a decade (ignoring leap years). -
Print the value contained in the
days_in_a_decadevariable.HINT*: Click here for a hint
You should see the total number of days in a decade:
> 3650
SOLUTION*: Click here for an example solution
days_in_a_year = 365 days_in_a_decade = 10 * days_in_a_year print(days_in_a_decade)
Like the subtraction and addition operators, the multiplication operator can also be used multiple times in a single statement.
product = 10 * 5 * 3
Based on the above information, please complete the following steps:
-
Define a variable named
seconds_per_minuteand assign it the value60. -
Define a variable named
minutes_per_hourand assign it the value60. -
Define a variable named
hours_per_dayand assign it the value24. -
Define a variable named
days_per_weekand assign it the value7. -
Define a variable named
seconds_per_weekand, using the multiplication operator and the previous variables, assign it number of seconds in a week. -
Print the value contained in the
seconds_per_weekvariable.HINT*: Click here for a hint
You should see the total seconds in a week:
> 604800
SOLUTION*: Click here for an example solution
seconds_per_minute = 60 minutes_per_hour = 60 hours_per_day = 24 days_per_week = 7 seconds_per_week = seconds_per_minute * minutes_per_hour * hours_per_day * days_per_week print(seconds_per_week)
Once you have completed the above steps and you are getting the correct output in the interpreter. You are ready to move on to the next exercise!