Bu eğitimde, Python'daki farklı operatör türleri, sözdizimleri ve bunları örneklerle nasıl kullanacağınız hakkında her şeyi öğreneceksiniz.
Video: Python'daki Operatörler
Python'daki operatörler nelerdir?
Operatörler, Python'da aritmetik veya mantıksal hesaplama yapan özel sembollerdir. Operatörün üzerinde çalıştığı değere işlenen denir.
Örneğin:
>>> 2+3 5
Burada +
toplama yapan operatördür. 2
ve 3
işlenenlerdir ve 5
işlemin çıktısıdır.
Aritmetik operatörler
Aritmetik operatörler, toplama, çıkarma, çarpma vb. Matematiksel işlemleri gerçekleştirmek için kullanılır.
Şebeke | Anlam | Misal |
---|---|---|
+ | İki işlenen veya tekli artı ekle | x + y + 2 |
- | Sağ işleneni soldan veya tek terimli eksiden çıkar | x - y- 2 |
* | İki işlenenle çarpın | x * y |
/ | Soldaki işleneni sağ olana böl (her zaman float ile sonuçlanır) | x / y |
% | Modül - sol operandın sağa bölünmesinin kalanı | x% y (x / y'nin kalanı) |
// | Kat bölme - sayı doğrusunda sola doğru ayarlanmış tam sayıya neden olan bölme | x // y |
** | Üs - sağın kuvvetine yükseltilmiş sol işlenen | x ** y (x'in y kuvvetine) |
Örnek 1: Python'da aritmetik operatörler
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)
Çıktı
x + y = 19 x - y = 11 x * y = 60 x / y = 3,75 x // y = 3 x ** y = 50625
Karşılaştırma operatörleri
Karşılaştırma operatörleri değerleri karşılaştırmak için kullanılır. Ya ya True
da duruma False
göre döner .
Şebeke | Anlam | Misal |
---|---|---|
> | Büyüktür - Sol işlenen sağdan büyükse doğrudur | x> y |
< | Küçüktür - Sol işlenen sağdan küçükse doğrudur | x <y |
== | Eşittir - Her iki işlenen de eşitse true | x == y |
! = | Eşit değildir - İşlenenler eşit değilse doğru | x! = y |
> = | Büyüktür veya eşittir - Sol işlenen sağdan büyük veya ona eşitse true | x> = y |
<= | Küçüktür veya eşittir - Sol işlenen sağdan küçük veya ona eşitse true | x <= y |
Örnek 2: Python'da karşılaştırma operatörleri
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)
Çıktı
x> y Yanlış x = y Yanlış x <= y Doğru
Mantıksal operatörler
Mantıksal operatörleri and
, or
, not
operatörler.
Şebeke | Anlam | Misal |
---|---|---|
ve | Her iki işlenen de doğruysa doğrudur | x ve y |
veya | İşlenenlerden biri doğruysa doğrudur | x veya y |
değil | İşlenen yanlışsa doğrudur (işleneni tamamlar) | x değil |
Örnek 3: Python'da Mantıksal Operatörler
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)
Çıktı
x ve y Yanlış x veya y Doğru değil x Yanlış
İşte bu operatörler için doğruluk tablosu.
Bitsel operatörler
Bitsel operatörler, işlenenler üzerinde ikili rakam dizileriymiş gibi hareket eder. Yavaş yavaş çalışırlar, dolayısıyla adı.
Örneğin, 2 10
ikili ve 7'dir 111
.
Aşağıdaki tabloda: x = 10 ( 0000 1010
ikili olarak) ve y = 4 ( 0000 0100
ikili olarak) olsun
Şebeke | Anlam | Misal |
---|---|---|
& | Bitsel AND | x & y = 0 ( 0000 0000 ) |
| | Bit tabanlı VEYA | x | y = 14 ( 0000 1110 ) |
~ | Bitsel DEĞİL | ~ x = -11 ( 1111 0101 ) |
^ | Bitsel ÖZELVEYA | x y = 14 ( 0000 1110 ) |
>> | Bitsel sağa kaydırma | x >> 2 = 2 ( 0000 0010 ) |
<< | Bitsel sola kaydırma | 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
ve not in
Python'daki üyelik operatörleri. Bir dizide bir değerin veya değişkenin bulunup bulunmadığını test etmek için kullanılırlar (dizi, liste, tuple, küme ve sözlük).
Bir sözlükte, değeri değil, yalnızca anahtarın varlığını test edebiliriz.
Şebeke | Anlam | Misal |
---|---|---|
içinde | Sırada değer / değişken bulunursa doğrudur | 5 inç x |
değil | Sırada değer / değişken bulunmazsa doğrudur | 5 x de değil |
Örnek 5: Python'da üyelik operatörleri
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)
Çıktı
Doğru Doğru Doğru Yanlış
Burada, 'H'
x içindedir 'hello'
, ancak x'de yoktur (unutmayın, Python büyük / küçük harfe duyarlıdır). Benzer şekilde, 1
anahtar ve 'a'
sözlük y'deki değerdir. Dolayısıyla 'a' in y
geri döner False
.