SoFunction
Updated on 2025-03-02

The use of basic operators in Python tutorials (Part 1)

Python operatorsUsually used to perform operations on values ​​and variables. These are standard notation for logical and arithmetic operations. In this article, we will look at different types of Python operators.

  • Operator: It is a special symbol. For example - + , * , / , etc.
  • OPERAND: It is the value of the applied operator.

Arithmetic operators

Arithmetic operators are used to perform mathematical operations such as addition, subtraction, multiplication, and division.

In Python, the result of division is a floating point number, while in Python, the division of 2 integers is an integer, and in Python, rounding (// integer) is used to obtain integer results.

Operators describe grammar
+ Addition: add two operands x + y
Subtraction: Subtract two operands x - y
* Multiplication: Multiply two operands x * y
/ Division (floating point number): Divide the first operand by the second operand x / y
// Division (floor): Divide the first operand by the second x // Yes
% Module: Returns the remainder when the first operand is divided by the second operand x % y
** Weight: Return to first and improve strength second x ** y

Priority

  • P - brackets
  • E - Index
  • M – Multiplication (multiple and division have the same priority)
  • D – Teacher
  • A – Addition (addition and subtraction have the same priority)
  • S - Subtraction

The modulus operator helps us extract the last digit of the number. For example:

  • x % 10 -> Generate the last digit
  • x % 100 -> produces the last two digits

Example: Arithmetic operators in Python

#Arithmetic operator examplea = 9
b = 4

# Add up numbersadd = a + b

# Number subtractionsub = a - b

# Multiplication of numbersmul = a * b

# Division of numbers (floating point numbers)div1 = a / b

# Number division (floor)div2 = a // b

# The modularity of two numbersmod = a % b

# Weightp = a ** b

# Print the resultsprint(add)
print(sub)
print(mul)
print(div1)
print(div2)
print(mod)
print(p)

Output

13 

36 
2.25 


6561

Comparison operator_

Comparison value of relational operators. It returns according to the conditionsTrueorFalse 。

Operators describe grammar
Greater than: True if the left operand is greater than the right operand x > y
< Less than: True if the left operand is less than the right operand x < y
== Equal to: True if two operands are equal x == y
!= Not equal - if operands are not equal, then true x != y
>= If the left operand is greater than or equal to the right operand, it is greater than or equal to True x >= y
<= If the left operand is less than or equal to the right operand, it is less than or equal to True x <= y
yes x is the same as y x is y
no x is different from y x is not y

= is the assignment operator and the == comparison operator.

Example: Comparison operator in Python

# Relational operator examplea = 13
b = 33

# a > b is falseprint(a &gt; b)

# a < b is trueprint(a &lt; b)

# a == b is falseprint(a == b)

# a != b is trueprint(a != b)

# a &gt;= b is False
print(a &gt;= b)

# a &lt;= b is True
print(a &lt;= b)

Output

False
True
False
True
False
True

Logical operators

Logical operator executionLogic andLogical orandLogical non-logicalOperation. It is used to combine conditional statements.

Operators describe grammar
and Logic &: If both operands are true, then true x and y
or Logical or: If any operand is true, then true x or y
not Logical non: if the operand is false, it is true not x

Example: Logical operators in Python

# Logical operator examplea = True
b = False

# Print a and b as Falseprint(a and b)

# Print a or b as Trueprint(a or b)

# Print not a as Falseprint(not a)

Output

False
True
False

bit operator

The bit operator acts on the bit and performs bit-by-bit operations. These are used to operate on binary numbers.

Operators describe grammar
& bitwise and x & y
| bitwise or x |y
~ Bitwise non ~x
^ bitwise xor x ^ y
>> Move right by bit x>>
<< Move left by bit x<<

Example: Bit operators in Python

# Bit operator examplea = 10
b = 4

# Print bitwise and operationprint(a &amp; b)

# Print bitwise or operationprint(a | b)

# Print bitwise non-operationprint(~a)

# Print bitwise XOR operationprint(a ^ b)

# Print bitwise rightprint(a &gt;&gt; 2)

# Print bitwise shift operationprint(a &lt;&lt; 2)

Output


14 
-11 
14 

40

Assignment operator

The assignment operator is used to assign values ​​to a variable.

Operators describe grammar
= Assign the value to the right of the expression to the operand on the left x = y + z
+= Add AND: Add the right operand to the left operand and assign it to the left operand a+=b a=a+b
-= Subtract AND: Subtract the right operand from the left operand and assign it to the left operand a-=b a=ab
*= Multiply and: Multiply the right operand with the left operand and assign the value to the left operand a*=b a=a*b
/= Division and: Divide the left operand from the right operand and assign it to the left operand a/=b a=a/b
%= Modulus AND: Use left and right operands to get the modulus and assign the result to the left operand a%=b a=a%b
//= Divide(floor) AND: Divide the left operand from the right operand, and assign the value (floor) to the left operand a//=b a=a//b
**= Exponentials and: Calculate the exponential value using operands (increase the power) and assign the value to the left operand a**=b a=a**b
&= Perform bitwise and assign values ​​to the left operand a&=b a=a&b
|= Perform bitwise or assign value to the left operand a|=b a=a|b
^= Perform bitwise exclusive OR on operands and assign values ​​to the left operands a^=b a=a^b
>>= Perform bitwise shifts on operands and assigns values ​​to left operands a>>=b a=a>>b
<<= Perform bit-left shifts on operands and assigns values ​​to left operands a <<= b a= a << b

Example: Assignment operators in Python

# Assignment operator examplea = 10

# Assignmentb = a
print(b)

# Add and assign valuesb += a
print(b)

# Subtraction and assignmentb -= a
print(b)

# Multiply and assignb *= a
print(b)

# bitwise stripeft operatorb &lt;&lt;= a
print(b)

Output

10 
20 
10 
100 
102400

Identity operator

isandis notis an identity operator, both of which are used to check if two values ​​are in the same part of memory. Two equal variables do not mean that they are the same.

is               True if the operand is the same

is not       True if the operands are not the same

Example: Identity Operator

a = 10
b = 20
c = a

print(a is not b)
print(a is c)

Output

True
True

Member operators

inandnot inis a member operator; used to test whether a value or variable is in a sequence.

in                 True if the value is found in the sequence

not in         true if no value is found in the sequence

Example: Member operator

# Python program to illustrate not 'in' operatorx = 24
y = 20
list = [10, 20, 30, 40, 50]

if (x not in list):
	print("x is NOT present in given list")
else:
	print("x is present in given list")

if (y in list):
	print("y is present in given list")
else:
	print("y is NOT present in given list")

Output

x is NOT present in given list
y is present in given list

This is the article about the use of basic operators in Python tutorial (Part 1). For more related Python operator content, please search for my previous articles or continue browsing the related articles below. I hope you will support me in the future!