Python 3 Tutorial for Beginners #02 – Arithmetic and Logical Operators

Python 3 Tutorial for Beginners #02: Introduction

In this Python 3 tutorial for beginners, we are going to cover arithmetic and logical operators!

Pre-requisites

If you’re an absolute newbie, we suggest you take a look at our first Python 3 tutorial for beginners in the Tutorials category on your right.

Introduction to Python 3 Operators

Let’s get started! Operator is a way to indicate that you need to do an operation. For instance, if you need to add two numbers, you can use “+” operator.  And if you want to multiply two numbers you can use “*” operator.

Operators are essential building blocks of programming. You can use them to build up expressions, statements, functions and eventually full-sized programs. It’s really quite straight forward.

In case you are wondering what a “statement” is I can assure you it has nothing to do with written documents or legal statements. It is simply the smallest standalone element that performs an action.

In this Python 3 tutorial for beginners, we are going to be covering some basic arithmetic operators.

Python 3 Operators
Okay, so let’s declare 2 variables ‘a‘ and ‘b‘. We will assign them two different values. In this case the “=” sign is also an operator that performs assignment.

a = 2
b = 5

Now let’s print the sum of ‘a‘ and ‘b‘.

ans = a + b

First let’s calculate the sum and assign it to variable ‘ans‘. Now we can print it.

print("a + b", ans)

There is another way to print a + b

print("a + b", a + b)

This way we don’t need to create an extra variable. We can simply pass a + b to print. In this scenario a + b will happen first and then it will be passed as an argument to print function call.

Python 3 Tutorial for Beginners – Operator List

Arithmetic Operators

Operator Sample
+ Addition 2 + 3 = 5
– Subtraction 3 – 2 = 1
* Multiplication 2 * 3 = 6
/ Division 6 / 4 = 1.5
% Modulus

Remainder after division.

10 % 5 = 0
** Power 2 ** 3 = 8
// Floor Division

Calculates the floor after division. (Works like integer division in C family languages)

3 // 2 = 1

Let’s try out the operators with following code block.

print("2 + 3 =", 2 + 3)
print("3 - 2 =", 3 - 2)
print("2 * 3 =", 2 * 3)
print("6 / 4 =", 6 / 4)
print("10 % 5 =", 10 % 5)
print("2 ** 3 =", 2 ** 3)
print("3 // 2 =", 3 // 2)

Logical and Relational Operators

Operator Sample
< Less than 5 < 6 —>> True
> Greater than 2 > 4 —>> False
<= Less or equal 2 <= 3 —>> True
>= Greater or equal 7 >= 1 —>> True
== Equal 6 == 4 —>> False
!= Not Equal 8 != 5 —>> True
and True and True —>> True
or False or True —>> True
not not True —>> False

Let’s try these operators too.

print("5 < 6 --->>", 5 < 6) 
print("2 > 4 --->>", 2 > 4)
print("2 <= 3 --->>", 2 <= 3) 
print("7 >= 1 --->>", 7 >= 1)
print("6 == 4 --->>", 6 == 4)
print("8 != 5 --->>", 8 != 5)
print("True and True --->>", True and True)
print("False or True --->>", False or True)
print("not True --->>", not True)

In case you are wondering what are these ‘True‘ and ‘False‘ values. Those are Boolean values. A Boolean data type can hold either true or false. Logical operators can be used to derive Boolean values.

Combining Operators

Imagine “John” and “Jack” wants to go on a ride in local amusement park. You need to know if both of them can go.

john = 69
jack = 68

min_height = 65

go_on_ride = john > min_height and jack > min_height
print("Can go on ride=", go_on_ride)

That’s it for this tutorial. No exercises. Try out all the code examples, play around and make changes. Enjoy!

Got any questions? Let us know in the comments below!

Here’s a link to the next Python tutorial (#03) of this series. We’ve got more Python beginner tutorials right here for you! We’ve also got tutorials on using Irfanview and Greenshot to resize/crop images. Check out our Python tutorials for dabblers while you’re at it! Also, while you’re here, don’t forget to check out the latest tech news and info on fresh tech releases too.

Like what you see? Subscribe to our email list and hit us with a like on our Facebook page to get the latest news updates and tutorials straight to your newsfeed!

2 thoughts on “Python 3 Tutorial for Beginners #02 – Arithmetic and Logical Operators

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.