Python Operators Explained with Easy Examples

Python Operators Explained with Easy Examples

Operators in Python

Operators in Python are unique symbols that enable us to carry out various operations, such as assigning data, comparing values, and adding numbers. Consider operators to be tools that instruct Python on how to handle your data.

Let’s examine each of the primary operator types individually using clear and thorough examples!

1. The Arithmetic Operators

Arithmetic operators are used to perform arithmetic operations between two Variables or Values. There are various arithmetical operators that we can use in Python, as follows:

  • The Addition (+)

The Addition (+) operator in Python is used to add two numeric values (like integers or floats) and to join two strings together.

In the park, you select five pebbles. A friend gives you three more later. Check the total now. Eight appears in your hand after you count.

your_pebbles = 5

friend_pebbles = 3

total = your_pebbles + friend_pebbles

print(total)

Output

8
  • Subtraction (-)

The - Operator subtracts the value on the right from the value on the left.

In your wallet, you have ₹10. You pay ₹4 for stickers. You have ₹6 left over after paying.

allowance = 10

sticker_cost = 4

remaining = allowance - sticker_cost

print(remaining)

Output

6
  • Multiplication (*)

The * Operator multiplies two numeric values to produce their product.


On the table are 6 lunchboxes. Each box contains 2 chocolates. After completing the task, you discover that you have placed a total of 12 chocolates.

boxes = 6

chocolates_each = 2

total = boxes * chocolates_each

print(total)

Output

12
    • Exponent ()**

    The ** Operator is used to raise the number on the left to the power of the number on the right.
    In simple terms, it multiplies the left number by itself as many times as the right number says.

    There are 2 seeds at the beginning. Every day the number doubles. 3 days later, you have 8 seeds total.

    seeds = 2
    
    days = 3
    
    final = seeds ** days
    
    print(final)

    Output

    8
  • Division (/)

The / Operator divides the value on the left by the value on the right and returns a floating-point result.

You own ten apples. You give them to two people equally. Everyone gets five.

apples = 10

people = 2

per_person = apples / people

print(per_person)

Output

5.0
  • Floor Division by // 

     

Returns the Quotient after the division of two operands. It always gives output in the integer data type. For example

Python

first_number = 5 
second_number = 2 
quo = first_number // second_number 
print(quo)

Output

2
  • Modulus (%)

The % Operator returns the remainder left after dividing one number by another.

You attempt to arrange 10 marbles in 3 groups. There is only 1 marble left in your hand after 3 groups are full.

marbles = 10

group_size = 3

leftover = marbles % group_size

print(leftover)

Output

1

2. Comparison Operators

Their values are compared. The outcome is either True or False.

  • Equal to (==)

The == Operator checks whether two values are equal and returns True if they are the same.

You own five vehicles. Likewise, your sister has five. Both work well together.

your_cars = 5

sister_cars = 5

same = your_cars == sister_cars

print(same)

Output

True
  • Not Equal (!=)

The != Operator checks whether two values are different and returns True if they are not the same.

There are five crayons in your box. Three is what your friend has. The figures are not equal.

your_crayons = 5

friend_crayons = 3

not_same = your_crayons != friend_crayons

print(not_same)

Output

True
  • Greater Than (>)

The > Operator checks if the left value is greater than the right value.

Your age is seven years old. Your cousin is three years old. You’re older.

your_age = 7

cousin_age = 3

result = your_age > cousin_age

print(result)

Output

True
  • Less Than (<)

The < Operator checks if the left value is smaller than the right value.

Four books are in your possession. Your friend has six. Your book collection is smaller.

your_books = 4

friend_books = 6

result = your_books < friend_books

print(result)

Output

True
  • Greater Than or Equals To (>=)

The >= Operator checks if the left value is greater than or equal to the right value.

5 points are required to enter a game. Your score is five. Go ahead and get in.

need = 5

score = 5

allowed = score >= need

print(allowed)

Output

True
  • Less Than or Equals To (<=)

The <= Operator checks if the left value is smaller than or equal to the right value.

Children under the age of five are permitted in a play area. You are three years old. You’re free.

max_age = 5

your_age = 3

allowed = your_age <= max_age

print(allowed)

Output

True

3. Assignment Operators

The Assignment Operators are used to assign the value of the right expression to the left operand.

Do Not Confuse:

and are both different operators. is the assignment operator, while is a comparison operator. = : It assigns the value of the right expression to the left operand. For example:

Python

a = 5 
print(a)

Output

5

: We have often used expressions like a = a+5 , it will add 5 in the previous value of a and then assign it to a. We can simply write this expression as a+=5.For example:

 

Python

x = 5 
x = x+5 
print(x)

Output

10

We can write x = x + 5 as x+=5.

Python

x = 5 
x+=5 
print(x)
Output
10

Note:

+= performs an operation on the same variable and stores the value in the same variable. This is how rest assignment operators
(

=,=,/=,=,//=−=,∗=,/=,∗∗=,//=,%=) In Python, work.

Python

x = 10
x += 5   # 15 
x *= 2   # 30 
x //= 4  # 7
print(x)

Output

7

4. Logical Operators

Work with conditions that are True or False.

  • AND

The and Operator returns True only if both conditions being compared are True.

You go play after cleaning your room and finishing your homework.

homework = True

room_clean = True

play = homework and room_clean

print(play)

Output

True
  • OR

The or Operator returns True if at least one of the conditions being compared is True.

If you assist around the house or eat vegetables, you get dessert. Dessert is yours since you helped.

veggies = False

helped = True

dessert = veggies or helped

print(dessert) 

Output

True
  • NOT

The not Operator reverses the result of a condition, returning True if the condition is False.

You step outside because it’s not raining.

raining = False

go_outside = not raining

print(go_outside) 

Output

True

The primary Python operators are now familiar to you. Try running the code and altering the values. You learn to code more quickly if you practice every day.

Assignment

  1. The Output of a = 2+3-4/2**2, and also tell its type?

  2. Suppose there are 5 rupees in your pocket.

    I am giving you 17 Rs. How many rupees do you have now?

    Now I am giving you 39 Rs. How many rupees do you have now?

    Now I am taking 23 Rs. back. How many rupees do you have now?

    Again, I am giving you 932 Rs. How many rupees do you have now?


    What will be the output of the following program:

a = False
b = False
x = not(a)
y = not(b)
print(a and b)
print(a and x)
print(y and b)
print(x and y)
  1. Calculate the value of ( a ) in the expression: a =( 5 + 3 – 2 / 2 ).
  1. Write a Program where the output is the value raised to the power 100.
  2. Suppose you have 150 rupees.

You spend 45 rupees on a meal. How many rupees do you have now?

Then, you spend 32 rupees on a movie ticket. How many rupees do you have now?

Next, you spend 18 rupees on snacks. How many rupees do you have now?

Finally, you spend 25 rupees on transportation. How many rupees do you have now?

Further learning

Python Memory Allocation Explained for Beginners

Python Data Types for Beginners: A Simple Guide to 4 Key Types – int, float, str, and bool

Python Official Documentation for Data Types:

URLhttps://www.w3schools.com/python/python_operators.asp

Conclusion:

The fusion of data science in the finance sector is not just a technological evolution but also a fundamental shift in the way the financial industry operates. From predictive analytics to personalized financial services, the applications of data science are reshaping traditional practices and opening up new possibilities. As we all move forward, the synergy between finance and data science will continue to evolve, creating a more robust, efficient, and resilient financial ecosystem. In this data-driven era, those who embrace the power of data science will be at the forefront of innovations and success in the world of finance.

Want to know what else can be done by Data Science?

If you wish to learn more about data science or want to advance your career in the data science field, feel free to join our free workshop on Master’s in Data Science with Power BI, where you will get to know how exactly the data science field works and why companies are ready to pay handsome salaries in this field.

In this workshop, you will get to know each tool and technology from scratch, which will make you skillfully eligible for any data science profile.

To join this workshop, register yourself on ConsoleFlare, and we will call you back.

Thinking, Why Console Flare?

Recently, ConsoleFlare has been recognized as one of the Top 10 Most Promising Data Science Training Institutes of 2023.

Console Flare offers the opportunity to learn Data Science in Hindi, just like how you speak daily.
Console Flare believes in the idea of “What to learn and what not to learn,” and this can be seen in their curriculum structure. They have designed their program based on what you need to learn for data science and nothing else.

Want more reasons?
Register yourself on ConsoleFlare, and we will call you back.
Log in or sign up to view
See posts, photos, and more on Facebook.

Console Flare

Leave a Reply

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

Back To Top