Tässä opetusohjelmassa opit kaiken erityyppisistä operaattoreista Pythonissa, niiden syntaksista ja niiden käytöstä esimerkkien avulla.
Video: Operaattorit Pythonissa
Mitä operaattorit ovat pythonissa?
Operaattorit ovat erityisiä symboleja Pythonissa, jotka suorittavat aritmeettisen tai loogisen laskennan. Arvoa, jolla operaattori toimii, kutsutaan operandiksi.
Esimerkiksi:
>>> 2+3 5
Tässä +
on operaattori, joka suorittaa lisäyksen. 2
ja 3
ovat operandit ja 5
ovat operaation tulos.
Aritmeettiset operaattorit
Aritmeettisia operaattoreita käytetään matemaattisten operaatioiden, kuten yhteenlasku, vähennyslasku, kertolasku, jne. Suorittamiseen.
Operaattori | Tarkoitus | Esimerkki |
---|---|---|
+ | Lisää kaksi operandia tai unary plus | x + y + 2 |
- | Vähennä oikea operandi vasemmasta tai unary-miinuksesta | x - y- 2 |
* | Kerro kaksi operandia | x * y |
/ | Jaa vasen operandi oikealla (johtaa aina kellumaan) | x / y |
% | Modulus - loppu vasemman operandin jakamisesta oikealle | x% y (loppuosa x / y) |
// | Kerrosjako - jako, joka johtaa kokonaislukuun, joka on mukautettu vasemmalle numerorivillä | x // y |
** | Eksponentti - vasen operandi korotettu oikealle | x ** y (x tehoon y) |
Esimerkki 1: Aritmeettiset operaattorit Pythonissa
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)
Tuotos
x + y = 19 x - y = 11 x * y = 60 x / y = 3,75 x // y = 3 x ** y = 50625
Vertailuoperaattorit
Arvojen vertailuun käytetään vertailuoperaattoreita. Se palaa joko True
tai False
ehdon mukaan.
Operaattori | Tarkoitus | Esimerkki |
---|---|---|
> | Suurempi kuin - Tosi, jos vasen operandi on suurempi kuin oikea | x> y |
< | Pienempi kuin - Tosi, jos vasen operandi on pienempi kuin oikea | x <y |
== | Yhtä - Tosi, jos molemmat operandit ovat samat | x == y |
! = | Ei yhtä suuri - Totta, jos operandit eivät ole yhtä suuria | x! = y |
> = | Suurempi tai yhtä suuri - Tosi, jos vasen operandi on suurempi tai yhtä suuri kuin oikea | x> = y |
<= | Pienempi tai yhtä suuri - Tosi, jos vasen operandi on pienempi tai yhtä suuri kuin oikea | x <= y |
Esimerkki 2: Vertailuoperaattorit Pythonissa
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)
Tuotos
x> y on väärä x = y on väärä x <= y on tosi
Loogiset operaattorit
Loogiset operaattorit ovat and
, or
, not
operaattorit.
Operaattori | Tarkoitus | Esimerkki |
---|---|---|
ja | Totta, jos molemmat operandit ovat totta | x ja y |
tai | Totta, jos jompikumpi operanteista on totta | x tai y |
ei | Tosi, jos operandi on väärä (täydentää operandia) | ei x |
Esimerkki 3: Loogiset operaattorit Pythonissa
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)
Tuotos
x ja y on väärä x tai y on totta, ei x on väärä
Tässä on totuustaulukko näille operaattoreille.
Bit-operaattorit
Bittikohtaiset operaattorit toimivat operandeilla ikään kuin ne olisivat binäärilukuja. Ne toimivat vähitellen, joten nimi.
Esimerkiksi 2 on 10
binäärinen ja 7 on 111
.
Alla olevassa taulukossa: Olkoon x = 10 ( 0000 1010
binäärisenä) ja y = 4 ( 0000 0100
binäärisenä)
Operaattori | Tarkoitus | Esimerkki |
---|---|---|
& | Bittikohtaisesti JA | x & y = 0 ( 0000 0000 ) |
| | Bittiä TAI | x | y = 14 ( 0000 1110 ) |
~ | Bittikohtaisesti EI | ~ x = -11 ( 1111 0101 ) |
^ | Bitteittäin XOR | x y = 14 ( 0000 1110 ) |
>> | Oikea siirtymä bittiä kohti | x >> 2 = 2 ( 0000 0010 ) |
<< | Bitti vasemmalle | 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
ja not in
ovat Pythonin jäsenoperaattoreita. Niitä käytetään testaamaan, löydetäänkö arvo tai muuttuja sekvenssistä (merkkijono, luettelo, sarja, joukko ja sanakirja).
Sanakirjassa voimme testata vain avaimen läsnäoloa, ei arvoa.
Operaattori | Tarkoitus | Esimerkki |
---|---|---|
sisään | Tosi, jos sarjasta löytyy arvo / muuttuja | 5 x |
ei mukana | Tosi, jos arvoa / muuttujaa ei löydy sarjasta | 5 ei x: ssä |
Esimerkki # 5: Jäsenoperaattorit Pythonissa
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)
Tuotos
True True True False
Tässä 'H'
on x, mutta sitä 'hello'
ei ole x: ssä (muista, että Python eroaa isoista ja pienistä kirjaimista). Samoin 1
on avain ja 'a'
arvo sanakirjassa y. Siksi 'a' in y
palaa False
.