Pythoni operaatorid: aritmeetika, võrdlus, loogika ja muu.

Selles õpetuses saate teada kõike Pythoni erinevat tüüpi operaatorite kohta, nende süntaksit ja kuidas neid koos näidetega kasutada.

Video: operaatorid Pythonis

Mis on Pythoni operaatorid?

Operaatorid on Pythoni erisümbolid, mis teostavad aritmeetilist või loogilist arvutust. Väärtust, millel operaator opereerib, nimetatakse operandiks.

Näiteks:

 >>> 2+3 5

Siin +on operaator, kes lisab. 2ja 3on operandid ning 5on operatsiooni väljund.

Aritmeetikaoperaatorid

Aritmeetilisi operaatoreid kasutatakse matemaatiliste toimingute sooritamiseks nagu liitmine, lahutamine, korrutamine jne.

Operaator Tähendus Näide
+ Lisage kaks operandi või unaarpluss x + y + 2
- Lahutage parem operand vasakust või unaarist miinus x - y - 2
* Korrutage kaks operandi x * y
/ Jagage vasak operand paremaga (tulemuseks on alati ujuk) x / y
% Modulus - ülejäänud vasakpoolse operandi jagunemine paremale x% y (ülejäänud osa x / y-st)
// Korrusjaotus - jagamine, mille tulemuseks on numbrireas vasakule kohandatud täisarv x // y
** Eksponent - vasak operand tõstetud parempoolsele võimule x ** y (x võimsuseni y)

Näide 1: aritmeetikaoperaatorid Pythonis

 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)

Väljund

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

Võrdlusoperaatorid

Väärtuste võrdlemiseks kasutatakse võrdlusoperaatoreid. Ta naaseb kas Truevõi Falsevastavalt seisukorras.

Operaator Tähendus Näide
> Suurem kui - Tõsi, kui vasak operand on paremast parem x> y
< Vähem kui - tõene, kui vasak operand on paremast parem x <y
== Võrdne - tõene, kui mõlemad operandid on võrdsed x == y
! = Pole võrdne - tõene, kui operandid pole võrdsed x! = y
> = Suurem või võrdne - tõene, kui vasak operand on paremast võrdne või sellega võrdne x> = y
<= Vähem või võrdne - tõene, kui vasak operand on paremast väiksem või sellega võrdne x <= y

Näide 2: Pythoni võrdlusoperaatorid

 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)

Väljund

 x> y on väär x = y on vale x <= y on tõene

Loogilised operaatorid

Loogiline operaatorid on and, or, notoperaatorid.

Operaator Tähendus Näide
ja Tõsi, kui mõlemad operandid vastavad tõele x ja y
või Tõsi, kui kumbki operand on tõene x või y
mitte Tõsi, kui operand on vale (täiendab operandi) mitte x

Näide 3: Pythoni loogilised operaatorid

 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)

Väljund

 x ja y on vale x või y on tõene, mitte x on vale

Siin on nende operaatorite tõetabel.

Bitipõhised operaatorid

Bitipõhised operaatorid toimivad operandidel nii, nagu oleksid need binaarsete numbritega stringid. Nad tegutsevad vähehaaval, sellest ka nimi.

Näiteks 2 on 10binaarne ja 7 on 111.

Allpool olevas tabelis: olgu x = 10 ( 0000 1010binaararv) ja y = 4 ( 0000 0100binaararv)

Operaator Tähendus Näide
& Pikkade kaupa JA x & y = 0 ( 0000 0000)
| Piki VÕI x | y = 14 ( 0000 1110)
~ Bititi EI ~ x = -11 ( 1111 0101)
^ Piki XOR x y = 14 ( 0000 1110)
>> Parempoolne parempoolne nihe x >> 2 = 2 ( 0000 0010)
<< Pikk vasakpoolne nihe 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

inja not inon Pythoni liikmesoperaatorid. Nende abil testitakse, kas väärtus või muutuja leitakse järjestusest (string, loend, dupleks, komplekt ja sõnastik).

Sõnaraamatus saame testida ainult võtme olemasolu, mitte väärtust.

Operaator Tähendus Näide
aastal Tõsi, kui jadast leitakse väärtus / muutuja 5 x
mitte sisse Tõsi, kui väärtust / muutujat jadast ei leita 5 mitte x-is

Näide 5: Pythoni liikmesuse operaatorid

 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)

Väljund

 Tõsi Õige Tõsi Vale

Siin 'H'on täht x, kuid 'hello'seda pole x-is (pidage meeles, et Python on tõstutundlik). Samamoodi 1on võti ja 'a'väärtus sõnastikus y. Seega 'a' in ynaaseb False.

Huvitavad Artiklid...