0% found this document useful (0 votes)
6 views9 pages

Python Operators Explained

The document provides an overview of operators in Python, categorizing them into five main families: arithmetic, assignment, bitwise, comparison, and logical operators. It includes examples of each operator type, demonstrating their functionality and usage. Additionally, it outlines the precedence of operators to guide their order of evaluation in expressions.

Uploaded by

alaa.ach2404
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views9 pages

Python Operators Explained

The document provides an overview of operators in Python, categorizing them into five main families: arithmetic, assignment, bitwise, comparison, and logical operators. It includes examples of each operator type, demonstrating their functionality and usage. Additionally, it outlines the precedence of operators to guide their order of evaluation in expressions.

Uploaded by

alaa.ach2404
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Operators in Python

Les Operateurs en Python


What’s an operator ? Qu’est ce qu’un
operateur
• As defined before an operator is a specific symbol, that makes a
specific operation on variables
• it’s important to know how to use them, and how they work
• Operators by give different results with different types of varibles
• There is seven families’ operators, here we will see only five :
• Arithmetic operators
• Assignment operators
• Bitwise operators
• Comparison operators
• Logical operators
• Membership operators
Electronic Dpt.2025‐2026/Mrs AMIROUCHE N.
Arithmetic Operators :
Operateurs Arithmétiques:

Symbol Operation Example with result


** Exponentiatio 3**2 gives 9
n
* Multiplication 5.4 *2 gives 10.8
/ Division 6.5/2 gives 3.25
// Floor division 6.5//2 gives 3.0
+ Addition 110+10 gives 120
‐ Soubstraction 110‐10 gives 100
% Modulus 6.5//2 gives 0.5
Electronic Dpt.2025‐2026/Mrs AMIROUCHE N.
Assignment operators Opérateurs d’affectation

Symbol Operation Example with result equivalent


= Assignement operation x=5 gives to x the value 5
+= Addition assignment x+=5 adds 5 to x value and overwrites its old x=x+5
value
‐= substraction assignment x‐=5 substracts 5 to x value and overwrites its x=x‐5
old value
*= multiplication X*=5 multiplies the x value by 5 and x=x*5
assignment overwrites its old value
/= Division assignment x/=5 divides the x value by 5 and overwrites its x=x/5
old value
//= Floor division x//=5 performs floor division of x by 5 and x=x//5
assignment overwrites its old value
**= Exponent assignment x**=5 puts x to the power 5 and overwrites its x=x**5
old value
%= Remainder assignment x%=5 gives the remainder of the floor division x=x%5
of x by 5 and overwrites its old value
Electronic Dpt.2025‐2026/Mrs AMIROUCHE N.
Comparison Operators
Operateurs de comparaison

Symbol Operation Example with result


== Equal to ‐2==0 gives False
!= Not equal ‐2!=0 gives True
> Greater than 8.5>8.1 gives True
< Less than 8.5<8.1 gives False
>= Greater than or Equal x=0 and x>=0 gives True
<= Less than or Equal x=0 and x<=0 gives False

Electronic Dpt.2025‐2026/Mrs AMIROUCHE N.


Logical Operators
Operateurs logiques

Symbol Operation Rules Example with result


and Expr1 and Returns True if both expr1 and (2<3) and (3<2) returns False
Expr2 expr2 are True
or Expr1 or Expr2 Returns True if one of expr1 or (2<3) or (3<2) returns True
expr2 is True
not not Expr Returns True if Expr is False vice not(2<3) returns False
versa not(3<2) returns True

Electronic Dpt.2025‐2026/Mrs AMIROUCHE N.


Membership Operators
Operateurs d’appartenance

Symbol Rules Example with result


in Returns True, if element is part of the set 3 in [0, 3, 5,7,9] returns True
not in Returns True, if element is not part of the set 3 not in [0, 3, 5,7,9] returns False

Electronic Dpt.2025‐2026/Mrs AMIROUCHE N.


Bitwise Operators
operteurs binaires

Symbol name Example with result


& and 3 & 4 returns 0
| or 3 | 4 returns 7
^ xor 3 ^ 4 returns 7
~ not ~3 returns‐ 4, bits inversion and
use of 2’s complement
<< Zero fill left shift 3<<2 returns 12
>> Signed right shift x>>2 returns 3

(3)b=0000 0011 (3)b&(4)b (3)b|(4)b (3)b^(4)b


(4)b=0000 0100 0000 0011 0000 0011 0000 0011
& | ^
0000 0100 0000 0100 0000 0100
0000 0000 =0 0000 0111 =7 0000 0111 =7

Remplaced by 0
(3) b<<1 out 0000 0110=(6)b
0000 0011 Electronic Dpt.2025‐2026/Mrs AMIROUCHE N.
Operators Precedence
Priorité des opérateurs

Level Symbol name Level Symbol name


1st () Parentheses 8th ^ Bitwise xor
2nd ** Exponentiation 9th | Bitwise or
3rd +x,‐x,~x Unary plus and minus, bitwise 10th ==,!=,<,<=,>,>= comparaison operators
Not in, not in Membership operators
4th *,/,//,% Multiplication, division, floor 11th not Logical not
division, modulus
12th and Logical and
5th +,‐ Modulus, substraction
13th or Logical or
6th << , >> Bitwise left and right shifts
7th & Bitwise and

Electronic Dpt.2025‐2026/Mrs AMIROUCHE N.

You might also like