Šajā apmācībā jūs uzzināsiet visu par dažādiem Python operatoru veidiem, to sintaksi un kā tos izmantot, izmantojot piemērus.
Video: operatori Python
Kas ir Python operatori?
Operatori ir īpaši Python simboli, kas veic aritmētisko vai loģisko aprēķinu. Vērtību, ar kuru darbojas operators, sauc par operandu.
Piemēram:
>>> 2+3 5
Šeit +
ir operators, kurš veic papildinājumu. 2
un 3
ir operandi un 5
ir operācijas rezultāts.
Aritmētiskie operatori
Aritmētiskos operatorus izmanto, lai veiktu matemātiskas darbības, piemēram, saskaitīšanu, atņemšanu, reizināšanu utt.
Operators | Nozīme | Piemērs |
---|---|---|
+ | Pievienojiet divus operandus vai unāros plusus | x + y + 2 |
- | Atņemiet labo operandu no kreisā vai unārā mīnusa | x - y- 2 |
* | Reiziniet divus operandus | x * y |
/ | Sadaliet kreiso operandu ar labo (vienmēr rezultāts ir peldošs) | x / y |
% | Modulis - atlikušais kreisā operanda sadalījums pa labi | x% y (atlikums x / y) |
// | Grīdas dalījums - dalījums, kura rezultātā vesels skaitlis tiek koriģēts pa kreisi skaitļu rindā | x // y |
** | Eksponents - kreisais operands, kas paaugstināts līdz labās varas spēkam | x ** y (x uz jaudu y) |
1. piemērs: Aritmētiskie operatori 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)
Rezultāts
x + y = 19 x - y = 11 x * y = 60 x / y = 3,75 x // y = 3 x ** y = 50625
Salīdzināšanas operatori
Vērtību salīdzināšanai tiek izmantoti salīdzināšanas operatori. Tā atgriež nu True
vai False
saskaņā ar nosacījumu.
Operators | Nozīme | Piemērs |
---|---|---|
> | Lielāks nekā - taisnība, ja kreisais operands ir lielāks par labo | x> y |
< | Mazāk nekā - taisnība, ja kreisais operands ir mazāks par labo | x <y |
== | Vienāds ar - Patiesi, ja abi operandi ir vienādi | x == y |
! = | Nav vienāds ar - Patiesi, ja operandi nav vienādi | x! = y |
> = | Lielāks par vai vienāds ar - Patiesi, ja kreisais operands ir lielāks vai vienāds ar labo | x> = y |
<= | Mazāk vai vienāds ar - Patiesi, ja kreisais operands ir mazāks vai vienāds ar labo | x <= y |
2. piemērs: salīdzināšanas operatori Python
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)
Rezultāts
x> y ir kļūdains x = y ir nepareizs x <= y ir patiess
Loģiskie operatori
Loģiskie operatori ir and
, or
, not
operatori.
Operators | Nozīme | Piemērs |
---|---|---|
un | Patiesi, ja abi operandi ir patiesi | x un y |
vai | Patiesi, ja kāds no operandiem ir patiess | x vai y |
nē | Patiesi, ja operands ir nepatiess (papildina operandu) | nevis x |
3. piemērs: loģiskie operatori 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)
Rezultāts
x un y ir kļūdains x vai y ir patiess, nevis x ir nepareizs
Šeit ir sniegta patiesības tabula šiem operatoriem.
Operatori pa bitiem
Operatori, kas darbojas pa bitiem, darbojas uz operandiem tā, it kā tie būtu bināro ciparu virknes. Viņi darbojas pamazām, tāpēc arī nosaukums.
Piemēram, 2 ir 10
binārā un 7 ir 111
.
Zemāk esošajā tabulā: Ļaujiet x = 10 ( 0000 1010
binārā) un y = 4 ( 0000 0100
binārā)
Operators | Nozīme | Piemērs |
---|---|---|
& | Bitu virzienā UN | x & y = 0 ( 0000 0000 ) |
| | Bitwise OR | x | y = 14 ( 0000 1110 ) |
~ | Bitwise NĒ | ~ x = -11 ( 1111 0101 ) |
^ | Bitor XOR | x y = 14 ( 0000 1110 ) |
>> | Pa labi pa labi | x >> 2 = 2 ( 0000 0010 ) |
<< | Bitu kustība pa kreisi | 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
un not in
ir Python dalības operatori. Tos izmanto, lai pārbaudītu, vai vērtība vai mainīgais ir atrodams secībā (virkne, saraksts, kopa, kopa un vārdnīca).
Vārdnīcā mēs varam pārbaudīt tikai atslēgas klātbūtni, nevis vērtību.
Operators | Nozīme | Piemērs |
---|---|---|
iekšā | Patiesi, ja secībā tiek atrasta vērtība / mainīgais | 5 x |
nav iekšā | Patiesi, ja secībā vērtība / mainīgais nav atrodams | 5 nav x |
5. piemērs: Dalības operatori Python
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)
Rezultāts
True True True False
Šeit 'H'
ir x, bet 'hello'
nav x (atcerieties, ka Python ir reģistrjutīgs). Līdzīgi 1
ir atslēgas un 'a'
vērtība vārdnīcā y. Tādējādi 'a' in y
atgriežas False
.