„Python“ operatoriai: aritmetika, palyginimas, logika ir dar daugiau.

Šioje pamokoje sužinosite viską apie skirtingus „Python“ operatorių tipus, jų sintaksę ir kaip juos naudoti su pavyzdžiais.

Vaizdo įrašas: „Python“ operatoriai

Kas yra „Python“ operatoriai?

Operatoriai yra specialūs „Python“ simboliai, atliekantys aritmetinį arba loginį skaičiavimą. Vertė, kurią operuoja operatorius, vadinama operandu.

Pavyzdžiui:

 >>> 2+3 5

Čia +yra operatorius, kuris atlieka papildymą. 2ir 3yra operandai ir 5yra operacijos išvestis.

Aritmetiniai operatoriai

Aritmetiniai operatoriai naudojami atliekant matematines operacijas, tokias kaip sudėjimas, atimimas, dauginimas ir kt.

operatorius Reikšmė Pavyzdys
+ Pridėkite du operandus arba unarinį pliusą x + y + 2
- Atimkite dešiniąjį operandą iš kairiojo arba unarinio minuso x - y- 2
* Padauginkite du operandus x * y
/ Padalinkite kairįjį operandą iš dešiniojo (visada gaunamas į plūduriuojantį) x / y
% Modulis - likusi kairiojo operando dalijimo į dešinę dalis x% y (likusi x / y dalis)
// Aukštų dalijimas - padalijimas, kurio rezultatas yra sveikas skaičius, pakoreguotas kairėje skaičių eilutėje x // y
** Eksponentas - kairysis operandas, pakeltas dešinės galia x ** y (x iki galios y)

1 pavyzdys: aritmetiniai operatoriai „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)

Rezultatas

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

Palyginimo operatoriai

Vertėms lyginti naudojami palyginimo operatoriai. Jis grįžta Truearba Falsepagal sąlygą.

operatorius Reikšmė Pavyzdys
> Didesnis nei - Tiesa, jei kairysis operandas yra didesnis už dešinįjį x> y
< Mažiau nei - tiesa, jei kairysis operandas yra mažesnis nei dešinysis x <y
== Lygu - Tiesa, jei abu operandai yra lygūs x == y
! = Nelygu - tiesa, jei operandai nėra lygūs x! = y
> = Didesnis arba lygus - Tiesa, jei kairysis operandas yra didesnis arba lygus dešiniajam x> = y
<= Mažesnis arba lygus - tiesa, jei kairysis operandas yra mažesnis arba lygus dešiniajam x <= y

2 pavyzdys: „Python“ operatorių palyginimas

 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)

Rezultatas

 x> y yra klaidingas x = y yra klaidingas x <= y yra teisingas

Loginiai operatoriai

Loginiai operatoriai yra and, or, notoperatoriai.

operatorius Reikšmė Pavyzdys
ir Tiesa, jei teisingi abu operandai x ir y
arba True if either of the operands is true x or y
not True if operand is false (complements the operand) not x

Example 3: Logical Operators in 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)

Output

 x and y is False x or y is True not x is False

Here is the truth table for these operators.

Bitwise operators

Bitwise operators act on operands as if they were strings of binary digits. They operate bit by bit, hence the name.

For example, 2 is 10 in binary and 7 is 111.

In the table below: Let x = 10 (0000 1010 in binary) and y = 4 (0000 0100 in binary)

Operator Meaning Example
& Bitwise AND x & y = 0 (0000 0000)
| Bitwise OR x | y = 14 (0000 1110)
~ Bitwise NOT ~x = -11 (1111 0101)
^ Bitwise XOR x y = 14 (0000 1110)
>> Bitwise right shift x>> 2 = 2 (0000 0010)
<< Bitwise left shift 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

inir not inyra „Python“ narystės operatoriai. Jie naudojami norint patikrinti, ar sekoje randama reikšmė ar kintamasis (eilutė, sąrašas, paketas, rinkinys ir žodynas).

Žodyne galime patikrinti tik raktą, o ne vertę.

operatorius Reikšmė Pavyzdys
į Tiesa, jei sekoje randama reikšmė / kintamasis 5 x
ne Tiesa, jei reikšmė / kintamasis nerandamas sekoje 5 ne x

5 pavyzdys: „Python“ narystės operatoriai

 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)

Rezultatas

 Tiesa Tikra Tiesa Netiesa

Čia 'H'yra x, bet 'hello'nėra x (atminkite, kad „Python“ skiria didžiosios ir mažosios raidės). Panašiai 1yra raktas ir 'a'y žodyno vertė. Vadinasi, 'a' in ygrįžta False.

Įdomios straipsniai...