Python Math


Python.

All about learning Python math operators and their use cases.


Challenges

Now that you know how to get input from a user, you need to learn how to do things with that input. The first thing we are going to cover is math operators. Python supports many different Math Operators, the following are what we care about right now:

Operator Name Example
+ Addition a+b
- Subtraction a-b
* Multiplication a*b
/ Division a/b
% Modulus a*b
** Exponent a**b
// Floor Division a//b

You likely have used every operator before - besides Modulus and Floor Division. Modulus is like division, but returns the remainder. For example, 4%2 would return 0 since 2 can fit into 4 exactly twice (with a remainder of 0). What would 5%2 return?

Floor division is rather straight forward. It is just like division, but will round down if there is a remainder. For example, 5//2 would return 2, whereas 5/2 would return 2.5.

Here is an example of how you might use these math operators...

# add 2 numbers together
x = int( input("Enter your first number: ") )
y = int( input("Enter your second number: ") )

sum = x + y
print("{} + {} = {}".format(x, y, sum))

The output will look like this:

# example output
Enter your first number: 4
Enter your second number: 5
4 * 5 = 20

Challenge:

Use python to let the user enter a number and detect if that number is odd or even. Your program should print 0 for even and 1 for odd.

  1. Create a new file with the file extension .py
  2. Write the python code to get a number from the user (Don't forget to typecast)
  3. Use modulus to show if the number is odd or even. Your program should print 0 for even and 1 for odd. (Remember even numbers are always divisible by two)
# example output
Please enter a number: 5
1
# example output
Please enter a number: 8
0
  1. Test your code with python yourFile.py
  2. Verify your solution with verify yourFile.py

Challenge:

Use python to read in two integers as input and performs floor division.

  1. Create a new file with the file extension .py
  2. Write the python code to get two numbers from the user.
  3. Perform floor division on the two numbers using the appropriate operator.
# example output
Enter the first number: 7
Enter the second number: 3
The result of floor division is: 2
  1. Test your code with python yourFile.py
  2. Verify your solution with verify yourFile.py

Lets put your skills to the test! There are many common formulas that mathematicians need to memorize! In geometry, for example...

Finding the Area of shapes
Circle: π*r²
Square:
Triangle: 0.5 * b * h
Trapezoid: ((a + b) / 2) * h

To find the area of a square:

s = float(input("Length of side: "))
area = s**2 # ** to find exponent

print("Area = ", area)
# example output
Length of side: 5
Area =  25.0

Challenge:

Use python to read in the width and height of a triangle using float, then calculate and print the area.

Note: The area will be a float data type! Make sure to get user input and typecast it to a float.

  1. Create a new file with the file extension .py
  2. Write the python code to get two numbers from the user (triangle width and height)
  3. Use the formula to calculate the area of a triangle and print the result!
# example output
Base: 3
Height: 5
Area =  7.5
  1. Test your code with python yourFile.py
  2. Verify your solution with verify yourFile.py

Programming involves alot more than just doing math on numbers. Another important aspect to programming is comparison. Comparison Operators return True or False based on the comparison being made. We will cover this in more detail later, but for now understanding how they look and what they return is important. The following Comparison Operators are important to know:

Operator Name Example
== Equal a==b, returns true if a is the same as b
!= Not Equal a!=b, returns true if a is not the same as b
> Greater Than a>b, returns true if a is greater than b
< Less Than a<b, returns true if a is less than b
>= Greater Than or Equal to a>=b, returns true if a is greater than or equal to b
<= Less Than or Equal to a<=b, returns true if a is less than or equal to b

Comparison Operators also leads us to a new data type! These comparisons return a boolean result. Boolean variables can be either True or False - only one or the other at any given time. This means that instead of resulting in an integer or a float, comparisons result in a boolean or bool. You can treat this result like a normal variable - you can store it, print it, and update it.

x = int(input("Enter a number: "))
res = x > 0 # use a variable to store result
print("Checking if number is positive: ", res)
print("Checking if number is negative: ", x < 0) # print result without storing
print("Checking if number is 0: ", x == 0)

Here are some examples of what the output might look like...

Enter a number: 5
Checking if number is positive:  True
Checking if number is negative:  False
Checking if number is 0:  False
Enter a number: -8
Checking if number is positive:  False
Checking if number is negative:  True
Checking if number is 0:  False
Enter a number: 0
Checking if number is positive:  False
Checking if number is negative:  False
Checking if number is 0:  True

Challenge:

Use python to read two numbers from the user and detect if the first number is smaller than the second.

  1. Create a new file with the file extension .py
  2. Write the python code to read in two numbers from a user
  3. Use comparison operators to print True, if the first number is less than or equal to the second number. If this is not the case, print False

Example 1:

First number: 4
Second number: 8
Checking if first number is less than or equal to second:  True

Example 2:

First number: 23
Second number: 2
Checking if first number is less than or equal to second:  False
  1. Test your code with python yourFile.py
  2. Verify your solution with verify yourFile.py


30-Day Scoreboard:

This scoreboard reflects solves for challenges in this module after the module launched in this dojo.

Rank Hacker Badges Score