Les opérateurs Python nous permettent d’effectuer des traitements courants sur les variables. Nous examinerons différents types d’opérateurs avec des exemples et également la précédence des opérateurs. Ce sont des symboles spéciaux qui peuvent manipuler les valeurs d’un ou plusieurs opérandes.
Liste des opérateurs Python
Les opérateurs Python peuvent être classés dans plusieurs catégories.
- Opérateurs d’affectation
- Opérateurs arithmétiques
- Opérateurs logiques
- Opérateurs de comparaison
- Opérateurs bit à bit
Opérateurs d’affectation Python
Les opérateurs d’affectation comprennent l’opérateur d’affectation de base signe égal (=).
Mais pour simplifier le code et réduire la redondance, Python inclut également des opérateurs d’affectation arithmétiques.
Cela inclut l’opérateur += en Python utilisé pour l’opération d’addition, //= opérateur d’affectation de division entière, et d’autres.
Voici une liste de tous les opérateurs d’affectation arithmétique en Python.
Operator | Description |
---|---|
+= | a+=b is equivalent to a=a+b |
*= | a*=b is equivalent to a=a*b |
/= | a/=b is equivalent to a=a/b |
%= | a%=b is equivalent to a=a%b |
**= | a**=b is equivalent to a=a**b (exponent operator) |
//= | a//=b is equivalent to a=a//b (floor division) |
Utilisation des opérateurs d’assignation
# Prenez deux variables, attribuez-leur des valeurs avec des opérateurs d'assignation
a=3
b=4
print("a: "+str(a))
print("b: "+str(b))
# C'est équivalent à a=a+b
a+=b
print("a: "+str(a))
print("b: "+str(b))
# C'est équivalent à a=a*b
a*=b
print("a: "+str(a))
print("b: "+str(b))
# C'est équivalent à a=a/b
a/=b
print("a: "+str(a))
print("b: "+str(b))
# C'est équivalent à a=a%b
a%=b
print("a: "+str(a))
print("b: "+str(b))
# C'est équivalent à a=a**b (opérateur d'exponentiation)
a**=b
print("a: "+str(a))
print("b: "+str(b))
# C'est équivalent à a=a//b (division entière)
a//=b
print("a: "+str(a))
print("b: "+str(b))
Opérateurs arithmétiques Python
Operator | Description | Example |
---|---|---|
+ | used to add two numbers | sum = a + b |
– | used for subtraction | difference = a – b |
* | used to multiply two numbers. If a string and int is multiplied then the string is repeated the int times. | mul = a*b>>> “Hi”*5 |
‘HiHiHiHiHi’ | ||
/ | used to divide two numbers | div = b/a |
% | modulus operator, returns the remainder of division | mod = a%b |
** | exponent operator |
# Créez deux variables
a=100
b=200
# Opérateur d'addition (+)
print(a+b)
# Opérateur de soustraction (-)
print(a-b)
# Opérateur de multiplication (*)
print(a*b)
# Opérateur de division (/)
print(b/a)
# Opérateur de modulo (%)
print(a%b) # prints the remainder of a/b
# Opérateur d'exponentiation (**)
print(a**b) #prints a^b
Opérateurs de comparaison Python
Operator | Description | Example |
---|---|---|
== | returns True if two operands are equal, otherwise False. | flag = a == b |
!= | returns True if two operands are not equal, otherwise False. | flag = a != b |
> | returns True if left operand is greater than the right operand, otherwise False. | flag = a > b |
< | returns True if left operand is smaller than the right operand, otherwise False. | flag = a < b |
>= | returns True if left operand is greater than or equal to the right operand, otherwise False. | flag = a > b |
<= | returns True if left operand is smaller than or equal to the right operand, otherwise False. | flag = a < b |
# create two variables
a=100
b=200
# (==) operator, checks if two operands are equal or not
print(a==b)
# (!=) operator, checks if two operands are not equal
print(a!=b)
# (>) operator, checks left operand is greater than right operand or not
print(a>b)
# (<) operator, checks left operand is less than right operand or not
print(a<b)
#(>=) operator, checks left operand is greater than or equal to right operand or not
print(a>=b)
# (<=) operator, checks left operand is less than or equal to right operand or not
print(a<=b)
# créer deux variables
Operator | Description | Example |
---|---|---|
& | Binary AND Operator | x = 10 & 7 = 2 |
Binary OR Operator | ||
^ | Binary XOR Operator | x = 10 ^ 7 = 13 |
~ | Binary ONEs Compliment Operator | x = ~10 = -11 |
<< | Binary Left Shift operator | x = 10<<1 = 20 |
>> | Binary Right Shift Operator | x = 10>>1 = 5 |
#create two variables
a=10 # binary 1010
b=7 # binary 0111
# Binary AND (&) operator, done binary AND operation
print(a&b)
# Binary OR (|) operator, done binary OR operation
print(a|b)
# Binary XOR (^) operator, done binary XOR operation
print(a^b)
# Binary ONEs Compliment (~) operator, done binary One's Compliment operation
print(~a)
# Binary Left Shift (<<) operator, done binary Left Shift operation
print(a<<1)
# Binary Right Shift (>>) operator, done binary Right Shift operation
print(a>>1)
# l’opérateur (==) vérifie si deux opérandes sont égales ou non
Operator | Description | Example |
---|---|---|
and | Logical AND Operator | flag = exp1 and exp2 |
or | Logical OR Operator | flag = exp1 or exp2 |
not | Logical NOT Operator | flag = not(True) = False |
# l'opérateur (!=) vérifie si deux opérandes ne sont pas égales
a=int(input())
# l'opérateur (>) vérifie si l'opérande gauche est plus grand que l'opérande droit ou non
if a%4==0 and a%3==0:
print("divided by both 4 and 3")
# (<) opérateur, vérifie si le premier opérande est inférieur au deuxième opérande ou non
if a%4==0 or a%3==0:
print("either divided by 4 or 3")
# (<=) opérateur, vérifie si l'opérande gauche est inférieur ou égal à l'opérande droit ou non
if not(a%4==0 or a%3==0):
print("neither divided by 4 nor 3")
Opérateurs Python de manipulation de bits
#créer deux variables
>>> 2+3*4
# Opérateur ET binaire (&), effectuer l’opération ET binaire
# Opérateur OU binaire (|), effectuer l’opération OU binaire
- # Opérateur XOR binaire (^), effectuer l’opération XOR binaire
- # Opérateur de complément à un binaire (~), effectuer l’opération de complément à un binaire
- # Décalage binaire à gauche (<<) opérateur, terminé binaire gauche décalage opération
- Opérateurs logiques Python
- #prendre une entrée utilisateur comme entier
- # opération ET logique
- # opération OU logique
- # opération NON logique
- Précédence des opérateurs Python
- La précédence de ces opérateurs signifie le niveau de priorité des opérateurs. Cela devient crucial lorsqu’une expression comporte plusieurs opérateurs. Par exemple, considérez l’expression suivante :
Source:
https://www.digitalocean.com/community/tutorials/python-operators