top of page

Math Operators

In python we have many built-in functions of math operators, in this practice, the most basic ones will be reviewed


Math Operators

  • Addition

    • +

  • Multiplication

    • *

  • Division

    • /

  • Subtraction

    • -

  • Power

    • *

  • Floor Division

    • //

  • Modulus

    • %

For Example

1. Operations in the print function

x = 15
y = 3
print(x + y)
print(x - y)
print(x / y)
print(x * y)
print(x // y)
print(x ** y)
print(x % y)

2. Operations on Variables

x = 8
y = 4
z = x+y
print(z)

3. Combining Variables with numbers

x = 4
y = 3
print(x - 2)
print(y + 8)

4. Simple Numbers

print(5 + 2)
print(16 % 4)

Exercises

  • Try performing different tasks in your calculator and in Python and see how they are the same

  • Investigate about numPy and Pandas libraries if you want to get more into Python's math

  • Try concatenating strings with numbers and see what happens, when an error is raised learn how to fix it, down the answer is posted

Solution of exercise number 3

x = 5
y = 10
z = x + y
print(str(x) + " + " + str(y) + " = " + str(z))

Explanation

At first, there are three variables initialized like how we've done so far, when printing an integer variable with a concatenated string, python raises an error since strings and integers are different data types, so to fix that, we use the str() method so this way python reads the number as a string ("5") and not an integer (5)


Comentários


bottom of page