W tym samouczku dowiesz się wszystkiego o różnych typach operatorów w Pythonie, ich składni i sposobach ich używania z przykładami.
Wideo: Operatory w Pythonie
Co to są operatory w Pythonie?
Operatory to specjalne symbole w Pythonie, które wykonują obliczenia arytmetyczne lub logiczne. Wartość, na której operuje operator, nazywana jest operandem.
Na przykład:
>>> 2+3 5
Tutaj +
jest operator, który wykonuje dodawanie. 2
i 3
są operandami i 5
są wynikiem operacji.
Operatory arytmetyczne
Operatory arytmetyczne służą do wykonywania operacji matematycznych, takich jak dodawanie, odejmowanie, mnożenie itp.
Operator | Znaczenie | Przykład |
---|---|---|
+ | Dodaj dwa operandy lub jednoargumentowy plus | x + y + 2 |
- | Odejmij prawy operand od lewego lub jednoargumentowego minus | x - y- 2 |
* | Pomnóż dwa operandy | x * y |
/ | Podziel lewy operand przez prawy (zawsze daje w wyniku liczbę zmiennoprzecinkową) | x / y |
% | Moduł - pozostała część podziału lewego operandu przez prawy | x% y (reszta z x / y) |
// | Podział piętra - podział na liczbę całkowitą wyrównaną w lewo na osi liczbowej | x // y |
** | Exponent - lewy operand podniesiony do potęgi prawej | x ** y (x do potęgi y) |
Przykład 1: Operatory arytmetyczne w Pythonie
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)
Wynik
x + y = 19 x - y = 11 x * y = 60 x / y = 3,75 x // y = 3 x ** y = 50625
Operatory porównania
Operatory porównania służą do porównywania wartości. Zwraca albo True
albo False
zgodnie z warunkiem.
Operator | Znaczenie | Przykład |
---|---|---|
> | Większe niż - Prawda, jeśli lewy operand jest większy niż prawy | x> y |
< | Mniejsze niż - Prawda, jeśli lewy operand jest mniejszy niż prawy | x <y |
== | Równe - Prawda, jeśli oba operandy są równe | x == y |
! = | Nie równe - Prawda, jeśli operandy nie są równe | x! = y |
> = | Większe lub równe - Prawda, jeśli lewy operand jest większy lub równy prawemu | x> = y |
<= | Mniejsze lub równe - Prawda, jeśli lewy operand jest mniejszy lub równy prawemu | x <= y |
Przykład 2: Operatory porównania w Pythonie
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)
Wynik
x> y to fałsz x = y to fałsz x <= y to prawda
Operatory logiczne
Operatory logiczne są and
, or
, not
operatorzy.
Operator | Znaczenie | Przykład |
---|---|---|
i | Prawda, jeśli oba operandy są prawdziwe | x i y |
lub | Prawda, jeśli którykolwiek z operandów jest prawdziwy | x lub y |
nie | Prawda, jeśli operand jest fałszywy (uzupełnia operand) | nie x |
Przykład 3: Operatory logiczne w Pythonie
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)
Wynik
x i y to fałsz x lub y to prawda, a nie x to fałsz
Oto tabela prawdy dla tych operatorów.
Operatory bitowe
Operatory bitowe działają na operandach tak, jakby były ciągami cyfr binarnych. Działają krok po kroku, stąd nazwa.
Na przykład 2 jest 10
binarne, a 7 to 111
.
W poniższej tabeli: Niech x = 10 ( 0000 1010
binarnie) i y = 4 ( 0000 0100
binarnie)
Operator | Znaczenie | Przykład |
---|---|---|
& | Bitowe i | x & y = 0 ( 0000 0000 ) |
| | Bitowe OR | x | y = 14 ( 0000 1110 ) |
~ | Bitowe NIE | ~ x = -11 ( 1111 0101 ) |
^ | Bitowe XOR | x y = 14 ( 0000 1110 ) |
>> | Przesunięcie bitowe w prawo | x >> 2 = 2 ( 0000 0010 ) |
<< | Przesunięcie bitowe w lewo | 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 in
są operatorami członkostwa w Pythonie. Służą do sprawdzania, czy wartość lub zmienna znajduje się w sekwencji (łańcuch, lista, krotka, zbiór i słownik).
W słowniku możemy sprawdzić tylko obecność klucza, a nie wartość.
Operator | Znaczenie | Przykład |
---|---|---|
w | Prawda, jeśli w sekwencji znajduje się wartość / zmienna | 5 w x |
nie w | Prawda, jeśli wartość / zmienna nie została znaleziona w sekwencji | 5 nie w x |
Przykład 5: operatory członkostwa w Pythonie
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)
Wynik
True True True False
Tutaj 'H'
znajduje się w x, ale 'hello'
nie występuje w x (pamiętaj, Python rozróżnia wielkość liter). Podobnie 1
jest kluczem i 'a'
jest wartością w słowniku y. Dlatego 'a' in y
wraca False
.