DEV Community

Cover image for Python3 Programming - Exercise 4 - Arithmetic Operators
Michael Otu
Michael Otu

Posted on • Edited on

1 1

Python3 Programming - Exercise 4 - Arithmetic Operators

Arithmetic Operators

Arithmetic operators are reserved symbols used for performing mathematical operations ( calculations).

Examples

operators symbols use return type
Addition + 1 + 3 int
Subtraction - 3 - 1 int
Multiplication * 3 * 2 int
Exponent ** 3 ** 2 int
Float division / 3 / 2 float
Integer division // 3 // 2 int
Modulo % 3 % 2 int

Note

  • If one of the operands is a float, then the resulting value is casted ( converted) into a float. Eg: 1.0 + 1 = 2.0 and 1 + 1 = 1 .
  • // , returns the whole number part ( quotient) of the division. So, given: 22.0 // 3 = 7.0 and 22 // 3 = 7 .
  • / , returns the quotient and the remainder as a float, together. Eg: 22 /3 = 7.333333333333333 and 0.25 / 0.5 = 0.5 .

Casting

Casting means, converting or changing from one type to another. To know the type of a value, use the type(obj) function. Eg: type(2) and type('2') will return <class 'int'> and <class 'str'> respectively. Meaning that 2 is an integer and '2' is a string.

Cating

Note

The values of x and y don't change after the casting, except that we do, y = float(y).

Practicals

write a program to evaluate and print the results of the following given that a = 2 and b = 5 :

    • a _ (2 _ b) - 5
    • 2 * (b - a) + b
  1. (-(a _ b) ** 2 - (4 _ a * b) ) / ((b // a) // 3 + (16 / a / b))
  2. ((a ** 2) - (b ** 2)) // ((b - a) ** 2)
  3. ((a + b) % 2) - ((b % a) + 1)

Summary

  • +, -, *, **, /, //, % are reserved for mathematical operations.
  • Rule of precedence is (), **, *, //, /, +, - .
  • Use parentheses to change the precedence.

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

nextjs tutorial video

Youtube Tutorial Series 📺

So you built a Next.js app, but you need a clear view of the entire operation flow to be able to identify performance bottlenecks before you launch. But how do you get started? Get the essentials on tracing for Next.js from @nikolovlazar in this video series 👀

Watch the Youtube series

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay