Gli operatori Python ci permettono di eseguire operazioni comuni sulle variabili. Esamineremo diversi tipi di operatori con esempi e anche la precedenza degli operatori. Sono i simboli speciali che possono manipolare i valori di uno o più operandi.
Elenco degli operatori Python
Gli operatori Python possono essere classificati in diverse categorie.
- Operatori di assegnazione
- Operatori aritmetici
- Operatori logici
- Operatori di confronto
- Operatori bitwise
Operatori di assegnazione Python
Gli operatori di assegnazione includono l’operatore di assegnazione di base uguale (=).
Ma per semplificare il codice e ridurre la ridondanza, Python include anche operatori di assegnazione aritmetica.
Questo include l’operatore += in Python usato per l’assegnazione di addizione, //= operatore di assegnazione divisione intera, e altri.
Ecco un elenco di tutti gli operatori di assegnazione aritmetica in 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) |
Utilizzando gli operatori di assegnazione
# prendere due variabili, assegnare valori con gli operatori di assegnazione
a=3
b=4
print("a: "+str(a))
print("b: "+str(b))
# è equivalente a a=a+b
a+=b
print("a: "+str(a))
print("b: "+str(b))
# è equivalente a a=a*b
a*=b
print("a: "+str(a))
print("b: "+str(b))
# è equivalente a a=a/b
a/=b
print("a: "+str(a))
print("b: "+str(b))
# è equivalente a a=a%b
a%=b
print("a: "+str(a))
print("b: "+str(b))
# è equivalente a a=a**b (operatore esponente)
a**=b
print("a: "+str(a))
print("b: "+str(b))
# è equivalente a a=a//b (divisione intera)
a//=b
print("a: "+str(a))
print("b: "+str(b))
Operatori Aritmetici 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 |
# creare due variabili
a=100
b=200
# operatore di addizione (+)
print(a+b)
# operatore di sottrazione (-)
print(a-b)
# operatore di moltiplicazione (*)
print(a*b)
# operatore di divisione (/)
print(b/a)
# operatore di modulo (%)
print(a%b) # prints the remainder of a/b
# operatore di esponente (**)
print(a**b) #prints a^b
Operatori di Confronto 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)
# creare due variabili
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’operatore (==) controlla se due operandi sono uguali o meno
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'operatore (!=) controlla se due operandi non sono uguali
a=int(input())
# l'operatore (>) controlla se l'operando sinistro è maggiore dell'operando destro o meno
if a%4==0 and a%3==0:
print("divided by both 4 and 3")
# l'operatore (<) controlla se l'operando sinistro è minore dell'operando destro o meno< operatore, controlla l'operando sinistro è minore del operando destro o meno
if a%4==0 or a%3==0:
print("either divided by 4 or 3")
# l'operatore (<=) controlla se l'operando sinistro è minore o uguale all'operando destro o meno
if not(a%4==0 or a%3==0):
print("neither divided by 4 nor 3")
Operatori bitwise di Python
#crea due variabili
>>> 2+3*4
# Operatore AND binario (&), eseguita l’operazione AND binaria
# Operatore OR binario (|), eseguita l’operazione OR binaria
- # Operatore XOR binario (^), eseguita l’operazione XOR binaria
- # Operatore di Complemento UNO binario (~), eseguita l’operazione di Complemento UNO binario
- # Operatore di Spostamento a Sinistra binario (<<) operatore, fatto binario Sinistra Spostamento operazione
- Operatori Logici Python
- #prendi input utente come int
- # operazione AND logica
- # operazione OR logica
- # operazione NOT logica
- Precedenza degli Operatori Python
- La precedenza di questi operatori indica il livello di priorità degli operatori. Questo diventa vitale quando un’espressione contiene più operatori. Ad esempio, considera la seguente espressione:
Source:
https://www.digitalocean.com/community/tutorials/python-operators