Operator Python

Assalamualaikum Wr. Wb.

Pada kesempatan kali ini, kita akan membahas Operator Python.

Operator Python

Operator digunakan untuk melakukan operasi pada variabel dan nilai.

Python membagi operator dalam kelompok-kelompok berikut:

  • Operator aritmatika
  • Operator penugasan
  • Operator pembanding
  • Operator logis
  • Operator identitas
  • Operator keanggotaan
  • Operator bitwise
Operator Aritmatika Python

Operator aritmatika digunakan dengan nilai numerik untuk melakukan operasi matematika umum:

OperatorNameExample
+Additionx + y
Subtractionx – y
*Multiplicationx * y
/Divisionx / y
%Modulusx % y
**Exponentiationx ** y
//Floor divisionx // y
Operator Tugas Python

Operator tugas digunakan untuk menetapkan nilai ke variabel:

Operator ExampleSame As
=x = 5x = 5
+=x += 3x = x + 3
-=x -= 3x = x – 3
*=x *= 3x = x * 3
/=x /= 3x = x / 3
%=x %= 3 x = x % 3
//=x //= 3x = x // 3
**=x **= 3x = x ** 3
&=x &= 3x = x & 3
|=x |= 3x = x | 3
^=x ^= 3x = x ^ 3
>>=x >>= 3x = x >> 3
<<=x <<= 3x = x << 3
Operator Perbandingan Python

Operator perbandingan digunakan untuk membandingkan dua nilai:

OperatorNameExample
==Equalx == y
!=Not equalx != y
>Greater thanx > y
<Less thanx < y
>=Greater than or equal tox >= y
<=Less than or equalx <= y
Operator Logika Python

Operator logis digunakan untuk menggabungkan pernyataan bersyarat:

OperatorDescriptionExample
andReturns True if both statements are truex < 5 and  x < 10
OrReturns True if one of the statements is truex < 5 or x < 4
notReverse the result, returns False if the result is truenot(x < 5 and x < 10)
Operator Identitas Python

Operator identitas digunakan untuk membandingkan objek, bukan jika mereka sama, tetapi jika mereka sebenarnya adalah objek yang sama, dengan lokasi memori yang sama:

OperatorDescriptionExample
IsReturns true if both variables are the same objectx is y
is notReturns true if both variables are not the same objectx is not y
Operator Keanggotaan Python

Operator keanggotaan digunakan untuk menguji apakah urutan disajikan dalam suatu objek:

OperatorDescriptionExample
InReturns True if a sequence with the specified value is present in the
object
x in y
not inReturns True if a sequence with the specified value is not present inx not in y
Operator Bitwise Python

Operator bitwise digunakan untuk membandingkan angka (biner):

OperatorNameDescription
&ANDSets each bit to 1 if both bits are 1
|ORSets each bit to 1 if one of two bits is 1
^XORSets each bit to 1 if only one of two bits is 1
~NOTInverts all the bits
<<Zero fill left shiftShift left by pushing zeros in from the right and let the leftmost bits fall off
>>Signed right shiftShift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off

Demikianlah share ilmu pada artikel kali ini

semoga bermanfaat, ya

Wassalamualaikum Wr. Wb