Introduction to Python Resources

Begin your free Python journey now,
at your own pace

check

Syntax: Multiplication

check

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:

  1. When multiplying one number by another:

       product = 7 * 6
    
  2. When multiplying a variable value by a number:

      val = 32
      product = val * 5
    
  3. 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:

  1. Define a variable named days_in_a_year and assign it 365.

  2. Define a variable named days_in_a_decade and, using the multiplication operator and days_in_a_year, assign it the number of days in a decade (ignoring leap years).

  3. Print the value contained in the days_in_a_decade variable.

    HINT*: Click here for a hint
    *Hints are best viewed after completing a task, or after spending some time and effort attempting it. In cases where a hint contains example output, it is important to understand why it is correct, as there may be many correct outputs.

    You should see the total number of days in a decade:

    > 3650
    SOLUTION*: Click here for an example solution
    *Example solutions are best viewed after completing a task and understanding the outcome. In most cases, there are multiple ways to complete a task, and the example solution is only one example.
        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:

  1. Define a variable named seconds_per_minute and assign it the value 60.

  2. Define a variable named minutes_per_hour and assign it the value 60.

  3. Define a variable named hours_per_day and assign it the value 24.

  4. Define a variable named days_per_week and assign it the value 7.

  5. Define a variable named seconds_per_week and, using the multiplication operator and the previous variables, assign it number of seconds in a week.

  6. Print the value contained in the seconds_per_week variable.

    HINT*: Click here for a hint
    *Hints are best viewed after completing a task, or after spending some time and effort attempting it. In cases where a hint contains example output, it is important to understand why it is correct, as there may be many correct outputs.

    You should see the total seconds in a week:

    > 604800
    SOLUTION*: Click here for an example solution
    *Example solutions are best viewed after completing a task and understanding the outcome. In most cases, there are multiple ways to complete a task, and the example solution is only one example.
      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!