Operators in Python

0/5 Votes: 0
Report this app

Description

An operator is a symbol that tells the Python interpreter to perform specific arithmetic or logical operations on variables and values.

For example –

# Here + and = are examples of operators in Python
sum = 5+6
print(sum)

Python has the following type of built-in operators.

  • Arithmetic operators
  • Relational operators
  • Logical operators
  • Bitwise operators
  • Assignment operators
  • Python special operators

So in this article, we will learn about each type of operator along with some examples.

Arithmetic operators

These operators are used to perform mathematical operations such as addition, subtraction, multiplication, division, modulus, etc. The list of arithmetic operators used in Python is given below.

App NameYoutube Vanced APK
Platform SupportedAndroid
Android Version RequiredAndroid 4.4 or above
APK Size45 MB
APK Version15.33.34
DpendencymicroG v0.2.6.17455_28052019

Floor division –

This can be new to many of you when you use // operator it performs floor division. The division which results into the whole number is known as floor division. You can differentiate between normal and floor by understanding the given example.

x=18
y=5
# Normal division
print(f"x/y is {x/y}")
# Floor division
print(f"x//y is {x//y}")

You can see the output of this code in the given image –

Example of using arithmetic operators –

a=10
b=4
print(f"Addition of a and b is {a+b}")
print(f"Subtraction of a and b is {a-b}")
print(f"Multiplication of a and b is {a*b}")
print(f"Division of a and b is {a/b}")
print(f"The remainder when a is divided by b is {a%b}")
print(f"a to the power b is {a**b}")
print(f"Floor division of a and b is {a//b}")

Execute this code on your own system to see the output.

Relational operators

A relational or comparison operator is used to compare two values. The following table shows the list of relational operators used in Python.

App NameGame Killer APK
Platform SupportedAndroid
Android Version supportedAndroid 4.0.1 or above.

APK Size500KB
Root RequiredYES

It compares two values if the condition goes true it returns True otherwise, it returns False.

For example –

a=4
b=5
#Since a is not greater than b this will return False
print("a >b is", a>b)

See the output in the image below –

Similarly, you can use other comparison operators in Python.

Logical operators

Logical operators in Python are used to combine two conditional statements. The list of logical operators is given in the table below.

App NameSpotify Premium APK
Platform SupportedAndroid, iOS, Windows PC and MAC
Android Version Android 4.1.1 or Above.
APK Size38MB
Version 8.40

For example –

a=4
b=5
# Here both the statements are true the and operation will return True
print("a<10 and b>4 is",a<10 and b>4 )

You can see the output in the given image –

Bitwise operators

The bitwise operators work on bits these perform operation bit by bit. The following table shows the list of bitwise operators that are used in Python.

App Name Guns of Boom
DeveloperGame Insight
Platform Android
Android VersionAndroid 4.1 and above.
APK Size77.19 MB
Version5.3.5

For example-

Lets a=4 and b=5  that means in 4-bit binary a will be 0100 and b will be 0101

         b          a&b

0          0           0

1          1           1

0          0           0

0          1           0

After performing bitwise and operation i.e. a&b will be 4 you can see the output in the given image-

Assignment operators

Assignment operators are used to assigning a value to a variable. Equal to i.e. = is a simple assignment operator we can combine this with other types of operators to calculate and assign a value to a variable. For example, + = will add and assign the calculated value to the variable.

The given table shows the list of assignment operators used in Python.

App NameOmlet Arcade Mod Apk
App DeveloperOmlet, Inc.
OS SupportedAndroid
OS Version4.4 and above
App Version1.76.6
App Size29.1 MB
Mod FeaturesOmlet Plus Membership Unlocked with premium features.

Python special operators

Python has some special operators i.e. identity and membership operators.

Identity operators

Identity operators check if the two values or variables are equal and at the same location in memory. There are two identity operators, you can see them in the given table.

App namePrimo
PlatformAndroid
Android version requiredAndroid 4.0.1 or above

Root RequiredNo
APK Size40Mb
Version1.0.48

For example –

a=5
b=5
c=[6,7,8]
d=c
# b is identical to a so the given statement will be True
print("b is a is",b is a)
# c is identical to d so the given statement will be False
print("c is not d", c is not d)

You can see the output in the image below –

Membership operators

You can use membership operators to test whether a value or variable is in the sequence or not. You can see membership operators in the given table.

App NameGB Instagram
Platform Supported Android
Android VersionAndroid 4.0.1 or above.
File Size38.9 MB
Version1.60 (LATEST)
Base Version71.0.0.18.102
a="Hello World!"
b="World"
c=[6,7,8]
d=[10]
# b is in a so the given statement will be True
print("b in a is",b in a)
# d is not in c so the given statement will also return True
print("d not in c", d not in c)

You can see the output in the given image –

Operator precedence and associativity

An expression in programming is the combination of operators and operands. The 5+2*3 is an example of a valid Python expression. Python interpreter performs the calculation on the basis of operator precedence and associativity.

For example in the expression 5+2*3 Python interpreter will first perform multiplication and later it will perform addition. That means * operator has higher precedence over +.

The following table shows the operator precedence and associativity. It is in descending order that means upper groups have higher precedence than lower ones.

App NameNova Launcher Prime APK
App PublisherTeslaCoil Software
Supported OSAndroid
Android VersionVaries with device
App Size~7 - 9 MB

Now which operator will be calculated first if they fall under the same group? Here associativity helps to determine the order of operations. So almost all the groups have left to right associativity that means the operator on the left side will be calculated first.

Conclusion

For better understanding try to evaluate Python expressions on your own and then try to find them with code. Now If you find any difficulty then write us in the comments below.

Leave a Reply

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