avatarSamer Sallam

Summary

This web content provides a comprehensive guide on arithmetic operators in Python, detailing their usage, syntax, and examples.

Abstract

The article "Arithmetic Operators: Python Complete Course — Part 12" is designed to educate readers on the fundamental arithmetic operators in Python. It is part of a larger Python programming course and offers insights into addition, subtraction, multiplication, division, modulus, exponentiation, and floor division. The author, Samer Sallam, provides code examples for each operator, demonstrating how they work with numeric values and outlining the expected input and output. The article also includes multimedia resources such as images and a YouTube video to enhance understanding. Additionally, Sallam encourages readers to subscribe for more content and consider Medium membership for unlimited access to stories and the opportunity to support writers directly.

Opinions

  • The author emphasizes the importance of understanding arithmetic operators for working with numbers in Python.
  • Samer Sallam suggests that readers should be familiar with the number of operands, their data types, and the result data type for each arithmetic operator.
  • The article promotes the idea that learning through examples is effective, as evidenced by the inclusion of multiple code snippets.
  • The author values reader engagement and support, as shown by the invitation to subscribe and follow on Medium, as well as the encouragement to join Medium membership.
  • Sallam acknowledges the convenience of floor division in Python, highlighting its ability to round down the result of a division to the nearest integer.
  • The use of real-life photographs and a YouTube video indicates the author's belief in the power of visual aids to complement textual explanations.
  • By providing a GitHub link, the author endorses the practical application of the concepts taught, encouraging readers to explore the code examples further.

Arithmetic Operators: Python Complete Course — Part 12

Photo by Saad Ahmad on Unsplash

Before we start let me tell you that:

  • This article is a part of Python Complete Beginner to Expert Course which you can find it here.
  • All resources are available in the “Resources” section below.
  • This article is also available as a YouTube video here.

Introduction

While you are working with numbers in Python, you might need to add one number to another or subtract one number from another. So, to deal with numbers in Python, you should be familiar with the available arithmetic operations in Python and the supported operators for these operations.

So, this lesson will cover the following outlines:

  1. Arithmetic Operators
  2. Addition (+)
  3. Subtraction (-)
  4. Multiplication (*)
  5. Division (/)
  6. Modulus (%)
  7. Exponentiation (**)
  8. Floor Division (//)

1. Arithmetic Operators

These operators are used with numeric values (numbers) to perform common mathematical operations like multiplication (*), division (/), addition(+), and so on… As mentioned in the previous lesson, for each operator you have to know the number of operands that this operator takes, operands data type, and the result data type.

In case of arithmetic operators, for each operator:

  • It takes two operands
  • Operands should be numbers.
  • The result is a number.

Now let us see an example about each operator.

2. Addition (+)

Input:

a = 10
b = 20
c = a + b
print(c)

Output:

30

As you saw in the previous example, “+” is an operator that represents addition, and it takes two operands “a” and “b”. Both of these operands are numbers, and the final result which is “c” is also number, and as you see its value is 30.

3. Subtraction (-)

Input:

c = a - b
print(c)

Output:

-10

As you saw in the previous example, if you want to use another operator, just use its related symbol. So to do subtraction only use “-”

4. Multiplication (*)

Input:

c = a * b
print(c)

Output:

200

5. Division (/)

Input:

c = a / b
print(c)

Output:

0.5

6. Modulus (%)

Modulus is the remainder after division, and it is represented by “%” sign.

Input:

c = a % b
print(c)

Output:

10

As you saw in the previous example, the result of the modulus operator above is “10”, because if you divide 10 by 20 you will get 0.5 and the remainder is 10.

7. Exponentiation (**)

Or in other words the power operator.

Input:

#let b equal to 2 
b = 2
c = a ** b
print(c)

Output:

100

In the example above, you can say “a” to the power of “b” and the result will be stored in “c”.

8. Floor Division (//)

In Python, there is an operator for floor division and this operator is represented by “//”.

Input:

#let us redefine the variables
a = 10
b = 20
c = a // b
print(c)

Output:

0

As you saw in the previous example, the result is “0” because 10 divided by 20 is 0.5, but you aren’t making a division operator; it is floor division which means: you will do the division first, then you will apply floor function which rounds any number (the result of division operator in this case) downward to the nearest integer.

Now, let us summarize what we have learned in this article:

Photo by Ann H on pexels
  • Arithmetic operators: are used with numeric values (Numbers) to perform common mathematical operations.
  • Arithmetic operators take exactly two operands and these operands should be numbers.
  • The final result of the operators operations is number.

P.S.: A million thanks for your time reading my story. Before you leave let me mention quickly two points:

  • First, to get my posts in your inbox directly, would you please subscribe here, and you can follow me here.
  • Second, writers made thousands of $$ on Medium. To get unlimited access to Medium stories and start earning, sign up now for Medium membership which only costs $5 per month. By signing up with this link, you can directly support me at no extra cost to you.

To get back to the previous article, you can use the following link:

Part 11:Python Numbers

To move on to the next article, you can use the following link:

Part 13:Comparison Operators

Resources:

Python
Programming
Arithmetic Operators
Arithmetic Operations
Python Programming
Recommended from ReadMedium