This article is participating in Python Theme Month. See the link to the event for more details

directory

  • The foreword ๐Ÿฅ‡ ๐Ÿฅ‡

  • ๐ŸŽช 1. The arithmetic operator

  • ๐Ÿฐ 2. The comparison operator

  • ๐Ÿฌ 3. Logical operators

  • ๐ŸŽฏ 4. Bitwise operators

  • ๐Ÿ›ซ 5. Assignment operator

  • ๐Ÿฅ… 6. Advanced Python operators

  • ๐Ÿบ Quick summary — Python operators

  • More on this at ๐Ÿงต

preface

This tutorial introduces operators in Python. There are different types of Python operators available, such as arithmetic, comparison, assignment, logic, bitwise, identity, and membership operators. You’ll learn their syntax and use them through the numerous examples provided here.

Each operator has a specific symbol that represents it. We will examine all the relevant symbols and understand what they mean. Each of them performs a specific operation and uses one or more operands, or variables.

For example, a | b and a & b. Text a and b are operands, and “|” and “&” is the operator. The former perform bitwise OR (|), while the latter (&) AND operation.

Now, let’s see what these operators are for and how to use them in real time.

The following table will help you learn about Python operators.

What are operators in Python?

Like many programming languages, Python retains some special characters as operators. Each operator performs operations such as addition and multiplication to manipulate data and variables. The variables passed as input to an operator are called operands. We also recommend that you read keywords in Python.

Example –

>>> 7%4
3
Copy the code

In this case, “%” is the modulo operator that calculates the remainder of the division. The numbers “7” and “4” passed as input are operands, while the number “3” is the result of performing the operation.

Back to the top


Arithmetic operator

Using arithmetic operators, we can perform various arithmetic operations, such as addition, subtraction, multiplication, division, modulus, exponent, etc. Python provides a variety of arithmetic computations, such as eval functions, declaring variables, and evaluating or calling functions.

The following table summarizes the built-in arithmetic operators in Python.

The operator purpose usage
+ Addition – The sum of two operands a+b
Subtraction – The difference between two operands a-b
* Multiplication – The product of two operands a*b
/ Float division – The quotient of two operands a/b
// Floor division – the quotient of two operands (no decimal part) a//b
% Modulus – the integer remainder of ‘a’ divided by ‘b’. a%b
ๆ”ฏ้‚ฃ The exponent — the product of a transpose itself, a to the b transpose. a**b

Example –

a=7
b=3

print('add:, a+b)
print(':', a-b)
print('by:, a*b)
print('divide (float) :', a/b)
print(Divide (floor) : ', a//b)
print('Modulus:', a%b)
print('Index:', a**b)
Copy the code

The output –

Add:10Subtraction:4By:21In addition,float) :  2.3333333333333335In addition to the (floor) :2Module:1Index:343
Copy the code

Back to the top


Comparison operator

In Python programming, comparison operators allow us to determine whether two values are equal or whether one value is greater than the other, and then make a decision based on the results.

The following table gives an overview of the built-in comparison operators in Python.

The operator purpose usage
> Greater than – Returns true if the left operand is greater than the right. a>b
< Less than – Returns true if the left operand is less than the right. a<b
= = Equal – Returns true if the two operands are equal. a==b
! = Not equal – Returns true if the two operands are not equal. a! =b
> = Greater than or equal – Returns true if the left-hand operand is greater than or equal to the right-hand operand. a>=b
< = Less than or equal – Returns true if the left-hand operand is less than or equal to the right-hand operand. a<=b

Example –

a=9
b=8

print('a > b is',a>b)

print('a < b is',a<b)

print('a == b is',a==b)

print('a ! = b is',a! =b)print('a >= b is',a>=b)

print('a <= b is',a<=b)
Copy the code

The output –

a > b is True
a < b is False
a == b is Falsea ! = bis True
a >= b is True
a <= b is False
Copy the code

Back to the top


Logical operator

Logical Python operators enable us to make decisions based on multiple conditions. Operands act as conditions that can lead to true or false values. The result of this operation is true or false (that is, a Boolean).

However, not all of these operators return Boolean results. The ‘and’ and’ or’ operators do return one of their operands, not a pure Boolean. The “not” operator always gives a true Boolean result.

See the following table and examples to see how these operators work in Python.

The operator purpose usage
and “A” if “a” is false, otherwise “b” a and b
or ‘B’ if ‘a’ is false, otherwise ‘A’ a or b
not True if “a” is false, false otherwise not a

Example –

a=7
b=4

A and b are 4
print('a and b is',a and b)

A or B is 7
print('a or b is',a or b)

Not a is False
print('not a is'.not a)
Copy the code

The output –

a and b is 4
a or b is 7
not a is False
Copy the code

Back to the top


Bitwise operators

The bitwise Python operator handles the bits of an integer value. They think of them as sequences of bits.

We can use the bitwise operator to check whether a particular bit is set. For example, an iot application reads data from a sensor depending on whether a particular bit is set up or not. In that case, these operators can help.

Example –

Let’s consider the numbers 4 and 6, whose binary representation is “00000100” and “00000110.” Now we will perform an AND on these numbers.

a=4
b=6

#Bitwise AND: The result of 'a & b' is 4
print('a & b is',a & b)
Copy the code

Output-

a & b is 4
Copy the code

Here’s how the AND(‘&’) operation works.

 0 0 0 0 0 1 0 0 &
 0 0 0 0 0 1 1 0
 ------------------
 0 0 0 0 0 1 0 0(digital4Binary representation of)Copy the code

Back to the top


Assignment operator

In Python, we can use the assignment operator to set values to variables.

The instruction A = 4 uses the original assignment operator to assign the value 4 to the left-hand operand.

Below is a list of compound operators available in Python. For example, the statement a += 4 is added to the variable and then assigned the same value. This is going to be the same thing as a is equal to a plus 4.

The operator example The equivalent of
= a=4 a=4
+ = a+=4 a=a+4
– = a-=4 a=a-4
* = a*=4 a=a*4
/ = a/=4 a=a/4
% = % = 4 a=a%4
* * = * * = 4 a=a**4
& = a&=4 a=a&4
= a = 4
^ = ^ = 4 a=a^4
> > = a>>=4 a=a>>4
< < = A < < = 4 a=<<4

Back to the top


Advanced Python operators

Python also bundles operators for special purposes. These are called advanced Python operators, such as the identity operator or member operator.

Identity operator

These operators allow us to compare the memory locations of two Python objects/variables. They allow us to find out whether objects share the same memory address. The variables that remain equivalent are not necessarily the same.

Alternatively, we can use these operators to determine whether a value belongs to a particular class or type.

See the table below for more information about them.

The operator purpose usage
is True – False if both operands point to the same object A is B (true if id(a) and ID (b) are the same)
is not True – False if the operands refer to different objects A is not B (true if id(a) and ID (b) are different)

Example –

# Use the 'is' identity operator
a = 7
if (type(a) is int) :print("true")
else:
 print("false")

# use "` ` `
is notThe identity operator b =7.5
if (type(b) is not int) :print("true")
else:
 print("false")
Copy the code

The output –

Really, reallyCopy the code

Back to the top


Member operator

The member operator allows us to test whether a value is a member of another Python object, such as a string, list, or tuple.

In C, the membership test traverses a sequence and checks each value. But compared to C, Python makes it much easier to establish membership.

Also note that this operator can also be tested against dictionaries, but only for keys, not values.

The operator purpose usage
in True – if the value exists in the sequence 7 in [3, 7, 9]
not in True – if the value is not found in the sequence 7 not in [3, 5, 9]

Example –

Use the member operator

str = 'Python operators'
dict = {6:'June'.12:'Dec'}

print('P' in str) 
print('Python' in str)
print('python' not in str)
print(6 in dict) 
print('Dec' in dict)
Copy the code

The output –

True
True
True
True
False
Copy the code

Back to the top


Quick summary — Python operators

This tutorial covers the various Python operators and their syntax, and describes their operations with examples. As a result, you should now find it easier to use operators in Python.

If you enjoyed this article and are interested in seeing more of it, follow me here (Github/Gitee) for more information. Here is a summary of all my original and work source code

More on this at ๐Ÿงต

  • 30 Python tutorials and tips | Python theme month
  • Python statements, expressions, and indentation | Python theme month
  • Python keywords, identifiers, and variables | Python theme month
  • How to write comments and multi-line comments in Python | Python Theme Month
  • 20 Python Tips Everyone must Know | Python Theme month
  • Python data types — Basic to advanced learning | Python topic month
  • 100 Basic Python Interview Questions Part 1 (1-20) | Python topic month
  • 100 Basic Python Interview Questions Part 2 (21-40) | Python topic month
  • 100 Basic Python Interview Questions Part 3 (41-60) | Python topic month
  • 100 Basic Python Interview Questions Part 4 (61-80) | Python topic month
  • 100 Basic Python Interview Questions Part 5 (81-100) | Python topic month

Recommended articles of the past:

  • Teach you to use Java to make a gobang game
  • Interesting way to talk about the history of JavaScript โŒ›
  • The output of a Java program | Java exercises ใ€‘ ใ€ 7 sets (including parsing)
  • โค๏ธ5 VS Code extensions that make refactoring easy โค๏ธ
  • It is said that if you have light in your eyes, everywhere you go is light
  • 140000 | 400 multichannel JavaScript ๐ŸŽ“ interview questions with answers ๐ŸŒ  items (the fifth part 371-424)

If you do learn something new from this post, like it, bookmark it and share it with your friends. ๐Ÿค— Finally, don’t forget โค or ๐Ÿ“‘ for support