Operatori Python: Aritmetică, comparație, logică și multe altele.

În acest tutorial, veți afla totul despre diferitele tipuri de operatori din Python, sintaxa lor și cum să le folosiți cu exemple.

Video: Operatori în Python

Ce sunt operatorii în Python?

Operatorii sunt simboluri speciale în Python care efectuează calcule aritmetice sau logice. Valoarea pe care operează operatorul se numește operand.

De exemplu:

 >>> 2+3 5

Aici +este operatorul care efectuează adăugarea. 2și 3sunt operanzi și 5este rezultatul operației.

Operatori aritmetici

Operatorii aritmetici sunt utilizați pentru a efectua operații matematice cum ar fi adunarea, scăderea, multiplicarea etc.

Operator Sens Exemplu
+ Adăugați doi operanzi sau plus unar x + y + 2
- Scădeți operandul drept din stânga sau din minus unar x - y- 2
* Înmulțiți doi operanzi X y
/ Împărțiți operandul stâng cu cel drept (rezultă întotdeauna în float) X y
% Modul - restul diviziunii operandului stâng de către dreapta x% y (restul lui x / y)
// Divizarea etajului - diviziune care rezultă în număr întreg ajustat la stânga în linia numerică X y
** Exponent - operand stâng ridicat la puterea dreptei x ** y (x la puterea y)

Exemplul 1: Operatori aritmetici în Python

 x = 15 y = 4 # Output: x + y = 19 print('x + y =',x+y) # Output: x - y = 11 print('x - y =',x-y) # Output: x * y = 60 print('x * y =',x*y) # Output: x / y = 3.75 print('x / y =',x/y) # Output: x // y = 3 print('x // y =',x//y) # Output: x ** y = 50625 print('x ** y =',x**y)

Ieșire

 x + y = 19 x - y = 11 x * y = 60 x / y = 3,75 x // y = 3 x ** y = 50625

Operatori de comparație

Operatorii de comparație sunt folosiți pentru a compara valorile. Revine fie Truesau în Falsefuncție de condiție.

Operator Sens Exemplu
> Mai mare decât - Adevărat dacă operandul din stânga este mai mare decât cel din dreapta x> y
< Mai puțin de - Adevărat dacă operandul din stânga este mai mic decât cel din dreapta x <y
== Egal cu - Adevărat dacă ambii operanzi sunt egali x == y
! = Nu este egal cu - Adevărat dacă operanzii nu sunt egali x! = y
> = Mai mare sau egal cu - Adevărat dacă operandul stâng este mai mare sau egal cu dreapta x> = y
<= Mai mic sau egal cu - Adevărat dacă operandul stâng este mai mic sau egal cu dreapta x <= y

Exemplul 2: Operatori de comparație în Python

 x = 10 y = 12 # Output: x> y is False print('x> y is',x>y) # Output: x < y is True print('x < y is',x= y is False print('x>= y is',x>=y) # Output: x <= y is True print('x <= y is',x<=y)

Ieșire

 x> y este fals x = y este fals x <= y este adevărat

Operatori logici

Operatori logici sunt and, or, notoperatorii.

Operator Sens Exemplu
și Adevărat dacă ambii operanzi sunt adevărați x și y
sau Adevărat dacă oricare dintre operanzi este adevărat x sau y
nu Adevărat dacă operandul este fals (completează operandul) nu x

Exemplul 3: Operatori logici în Python

 x = True y = False print('x and y is',x and y) print('x or y is',x or y) print('not x is',not x)

Ieșire

 x și y este Fals x sau y este Adevărat nu x este Fals

Iată tabelul adevărului pentru acești operatori.

Operatori bitwise

Operatorii bitwise acționează asupra operanzilor ca și cum ar fi șiruri de cifre binare. Acestea funcționează bit cu bit, de unde și numele.

De exemplu, 2 este 10în binar și 7 este 111.

În tabelul de mai jos: Fie x = 10 ( 0000 1010în binar) și y = 4 ( 0000 0100în binar)

Operator Sens Exemplu
& Bitwise ȘI x & y = 0 ( 0000 0000)
| OR bit x | y = 14 ( 0000 1110)
~ Bitwise NU ~ x = -11 ( 1111 0101)
^ Bitwise XOR x y = 14 ( 0000 1110)
>> Deplasare la dreapta pe biți x >> 2 = 2 ( 0000 0010)
<< Shift în stânga x << 2 = 40 (0010 1000)

Assignment operators

Assignment operators are used in Python to assign values to variables.

a = 5 is a simple assignment operator that assigns the value 5 on the right to the variable a on the left.

There are various compound operators in Python like a += 5 that adds to the variable and later assigns the same. It is equivalent to a = a + 5.

Operator Example Equivalent to
= x = 5 x = 5
+= x += 5 x = x + 5
-= x -= 5 x = x - 5
*= x *= 5 x = x * 5
/= x /= 5 x = x / 5
%= x %= 5 x = x % 5
//= x //= 5 x = x // 5
**= x **= 5 x = x ** 5
&= x &= 5 x = x & 5
|= x |= 5 x = x | 5
^= x ^= 5 x = x 5
>>= x>>= 5 x = x>> 5
<<= x <<= 5 x = x << 5

Special operators

Python language offers some special types of operators like the identity operator or the membership operator. They are described below with examples.

Identity operators

is and is not are the identity operators in Python. They are used to check if two values (or variables) are located on the same part of the memory. Two variables that are equal does not imply that they are identical.

Operator Meaning Example
is True if the operands are identical (refer to the same object) x is True
is not True if the operands are not identical (do not refer to the same object) x is not True

Example 4: Identity operators in Python

 x1 = 5 y1 = 5 x2 = 'Hello' y2 = 'Hello' x3 = (1,2,3) y3 = (1,2,3) # Output: False print(x1 is not y1) # Output: True print(x2 is y2) # Output: False print(x3 is y3)

Output

 False True False

Here, we see that x1 and y1 are integers of the same values, so they are equal as well as identical. Same is the case with x2 and y2 (strings).

But x3 and y3 are lists. They are equal but not identical. It is because the interpreter locates them separately in memory although they are equal.

Membership operators

inși not insunt operatorii de membru în Python. Sunt folosite pentru a testa dacă o valoare sau o variabilă se găsește într-o secvență (șir, listă, tuplu, set și dicționar).

Într-un dicționar putem testa doar prezența cheii, nu valoarea.

Operator Sens Exemplu
în Adevărat dacă valoarea / variabila se găsește în secvență 5 în x
nu în Adevărat dacă valoarea / variabila nu este găsită în secvență 5 nu în x

Exemplul nr. 5: Operatori de membru în Python

 x = 'Hello world' y = (1:'a',2:'b') # Output: True print('H' in x) # Output: True print('hello' not in x) # Output: True print(1 in y) # Output: False print('a' in y)

Ieșire

 Adevărat Adevărat Adevărat Fals

Aici, 'H'este în x, dar 'hello'nu este prezent în x (amintiți-vă, Python este sensibil la majuscule și minuscule). În mod similar, 1este cheie și 'a'este valoarea din dicționarul y. Prin urmare, 'a' in yse întoarce False.

Articole interesante...