U ovom ćete uputstvu naučiti sve o različitim vrstama operatora u Pythonu, njihovoj sintaksi i kako ih koristiti s primjerima.
Video: Operatori u Pythonu
Što su operatori u pythonu?
Operatori su posebni simboli u Pythonu koji provode aritmetičko ili logičko računanje. Vrijednost na kojoj operator operira naziva se operand.
Na primjer:
>>> 2+3 5
Ovdje +
je operator koji vrši zbrajanje. 2
i 3
jesu operandi i 5
je rezultat operacije.
Aritmetički operatori
Aritmetički operatori koriste se za izvođenje matematičkih operacija poput zbrajanja, oduzimanja, množenja itd.
Operater | Značenje | Primjer |
---|---|---|
+ | Dodajte dva operanda ili unarni plus | x + y + 2 |
- | Oduzmi desni operand od lijevog ili unarnog minusa | x - y - 2 |
* | Pomnožite dva operanda | x * y |
/ | Podijelite lijevi operand s desnim (uvijek rezultira floatom) | x / y |
% | Modul - ostatak podjele lijevog operanda na desni | x% y (ostatak x / y) |
// | Podna podjela - podjela koja rezultira cijelim brojem prilagođenim lijevo u brojevnoj crti | x // y |
** | Eksponent - lijevi operand podignut u moć desnog | x ** y (x u stepen y) |
Primjer 1: Aritmetički operatori u Pythonu
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)
Izlaz
x + y = 19 x - y = 11 x * y = 60 x / y = 3,75 x // y = 3 x ** y = 50625
Operatori usporedbe
Operatori usporedbe koriste se za usporedbu vrijednosti. Vraća se True
ili False
prema stanju.
Operater | Značenje | Primjer |
---|---|---|
> | Veće od - Tačno ako je lijevi operand veći od desnog | x> y |
< | Manje od - Tačno ako je lijevi operand manji od desnog | x <y |
== | Jednako s - Tačno ako su oba operanda jednaka | x == y |
! = | Nije jednako - vrijedi ako operandi nisu jednaki | x! = y |
> = | Veće ili jednako - Tačno ako je lijevi operand veći ili jednak desnom | x> = y |
<= | Manje ili jednako - Tačno ako je lijevi operand manji ili jednak desnom | x <= y |
Primjer 2: Operatori usporedbe u Pythonu
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)
Izlaz
x> y je False x = y je False x <= y je True
Logički operatori
Logički operatori su and
, or
, not
operatori.
Operater | Značenje | Primjer |
---|---|---|
i | Istina ako su oba operanda istinita | x i y |
ili | Istina ako je istinit bilo koji od operanda | x ili y |
ne | Tačno ako je operand false (nadopunjuje operand) | ne x |
Primjer 3: Logički operatori u Pythonu
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)
Izlaz
x i y je False x ili y je True nije x nije False
Evo tablice istine za ove operatore.
Bitovni operatori
Bitovni operatori djeluju na operande kao da su nizovi binarnih znamenki. Djeluju malo po malo, pa otuda i naziv.
Na primjer, 2 je 10
u binarnom, a 7 je 111
.
U donjoj tablici: Neka je x = 10 ( 0000 1010
u binarnom) i y = 4 ( 0000 0100
u binarnom)
Operater | Značenje | Primjer |
---|---|---|
& | Bitno I | x & y = 0 ( 0000 0000 ) |
| | Bitno ILI | x | y = 14 ( 0000 1110 ) |
~ | Bitno NE | ~ x = -11 ( 1111 0101 ) |
^ | Bitno XOR | x y = 14 ( 0000 1110 ) |
>> | Pomak u bit udesno | x >> 2 = 2 ( 0000 0010 ) |
<< | Smjena ulijevo | 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
operateri su članstva u Pythonu. Koriste se za ispitivanje nalazi li se vrijednost ili varijabla u nizu (niz, popis, skup, skup i rječnik).
U rječniku možemo testirati samo prisutnost ključa, a ne vrijednost.
Operater | Značenje | Primjer |
---|---|---|
u | Tačno ako se vrijednost / varijabla pronađe u nizu | 5 u x |
ne u | Tačno ako vrijednost / varijabla nije pronađena u nizu | 5 nije u x |
Primjer # 5: Operatori za članstvo u Pythonu
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)
Izlaz
True True True False
Ovdje 'H'
je u x, ali 'hello'
nije prisutan u x (sjetite se, Python razlikuje velika i mala slova). Slično tome, 1
ključ 'a'
je i vrijednost u rječniku y. Dakle, 'a' in y
vraća se False
.