1️⃣

3.1 Mathematical Operators

Math Operators

We can use math to change numbers around and edit our data. You can use these operators on an integer and another integer, on a float and another float, or on a combination of the two.

Table

Format:
Operator
Usage (Code Example)
Note
  • Addition (+)
    • Example
      • x = 5 y = 3 print(x + y) # prints 8
        View code on GitHub.
  • Subtraction (-)
    • Example
      • x = 5 y = 3 print(x - y) # prints 2
        View code on GitHub.
  • Multiplication (*)
    • Example
      • x = 5 y = 3 print(x * y) # prints 15
        View code on GitHub.
  • (Floating Point) Division (/)
    • Example
      • x = 15 y = 3 print(x / y) # prints 5.0 x = 5 y = 3 print(x / y) # prints 1.6666666666666667
        View code on GitHub.
    • This type of division gives a floating-point number (float) with a decimal remainder (the remainder divided by the divisor is the decimal)
  • (Floor) Division (//)
    • Example
      • x = 5 y = 2 print(x // y) # prints 2
        View code on GitHub.
    • This type of division gives you an integer and ignores any remainder
  • Modulus (%)
    • Example
      • x = 14 y = 3 print(x % y) # prints 2
        View code on GitHub.
    • This gives you the remainder after dividing the 2 numbers.
  • Exponent (**)
    • Example
      • x = 2 y = 3 print(x ** y) # prints 8
        View code on GitHub.
    • It is equivalent to x to the power of y

Practice

Math

Ask the user for a number, and store it in a variable called x. Divide the number x by 5, then add 2 to it, then store it in a variable called y. Print the result (y).

Cylinder Volume

Write a program that asks for the radius and height of a cylinder and returns the volume of the cylinder. The formula for the volume of a cylinder is volume = pi * (radius ** 2) * height

Circle

Write a program that asks the user for the radius of a circle. Then, print the area and circumference of the circle. Use 3.14 as the value for pi. Don't forget to add comments!
Hint: area = pi * (radius ** 2)
Hint #2: circumference = pi * 2 * radius

Power

Write a program that accepts two numbers, x and y. Compute and print the quantity (x + y) ** 5. Display the entire equation. The program should accept decimals as inputs.

Temperature

Write a program that converts Celsius to Fahrenheit. It should ask the user for the temperature in degrees Celsius and then print the temperature in degrees Fahrenheit. The formula is: F = C * 9/5 + 32

Decimal

Write a program that asks the user for a floating-point number as input and outputs the decimal part (the part to the right of the decimal point). Don't worry about floating point errors! Note: To display a more exact output, you can use rounded_decimal = round(decimal_variable, 10) to set rounded_decimal to your decimal rounded to a precision of 10 decimal places.

Temperature

Write a program that prompts the user with the following questions:
  • How many cookies did you make?
  • How many cookies did your friend make?
  • How many cookies are burnt?
  • How many friends do you have?
Then, output the number of cookies that will be leftover if you throw out all the burnt cookies and all the friends get the same amount of cookies.

Lunch Tables

Ask the user to input how many people are in the lunchroom and how many people can sit at each table. Output the number of people that will be left without a table.

Change

Write code that takes, as input, the number of dollars a person has (a floating number) and outputs how much they have in dollars, quarters, dimes, nickels, and pennies.
For example, if someone has $4.62, the program would print the following:
4 dollars
2 quarters
1 dime
0 nickels
2 cents
The starting code is given.
💡
This is a challenging problem! Don't feel bad or disheartened if you can't solve it. Your teacher can go over it next class

Previous Chapter