Operators

Operator digunakan untuk melakukan operasi pada variabel dan nilai.

Dalam contoh di bawah ini, kami menggunakan operator + untuk menjumlahkan dua nilai:

print(10 + 5)

Arithmetic Operators

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

Operator

Name

Example

+

Addition

x + y

-

Subtraction

x - y

*

Multiplication

x * y

/

Division

x / y

%

Modulus

x % y

**

Exponentiation

x ** y

//

Floor division

x // y

Assignment Operators

Operator penugasan digunakan untuk menetapkan nilai ke variabel:

Operator

Example

Same As

=

x = 5

x = 5

+=

x += 3

x = x + 3

-=

x -= 3

x = x - 3

*=

x *= 3

x = x * 3

/=

x /= 3

x = x / 3

%=

x %= 3

x = x % 3

//=

x //= 3

x = x // 3

**=

x **= 3

x = x ** 3

&=

x &= 3

x = x & 3

|=

x |= 3

x = x | 3

^=

x ^= 3

x = x ^ 3

>>=

x >>= 3

x = x >> 3

<<=

x <<= 3

x = x << 3

:=

print(x := 3)

x = 3; print(x)

The Walrus Operator

Python 3.8 memperkenalkan operator :=, yang dikenal sebagai "operator walrus". Operator ini menetapkan nilai ke variabel sebagai bagian dari ekspresi yang lebih besar:

numbers = [1, 2, 3, 4, 5]
count = len(numbers)
if count > 3:
    print(f"List has {count} elements")

if (count := len(numbers)) > 3:
    print(f"List has {count} elements")

Comparison Operators

Operator perbandingan digunakan untuk membandingkan dua nilai:

Operator

Name

Example

==

Equal

x == y

!=

Not equal

x != y

>

Greater than

x > y

<

Less than

x < y

>=

Greater than or equal to

x >= y

<=

Less than or equal to

x <= y

Operator perbandingan mengembalikan Benar atau Salah berdasarkan perbandingan:

x = 5
y = 3

print(x == y)
print(x != y)
print(x > y)
print(x < y)
print(x >= y)
print(x <= y)

Chaining Comparison Operators

Python memungkinkan Anda untuk merangkai operator perbandingan:

x = 5

print(1 < x < 10)

print(1 < x and x < 10)

Logical Operators

Operator logika digunakan untuk menggabungkan pernyataan kondisional:

Operator

Description

Example

and

Returns True if both statements are true

x < 5 and x < 10

or

Returns True if one of the statements is true

x < 5 or x < 4

not

Reverse the result, returns False if the result is true

not(x < 5 and x < 10)

Identity Operators

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

Operator

Description

Example

is

Returns True if both variables are the same object

x is y

is not

Returns True if both variables are not the same object

x is not y

contoh:

Operator is mengembalikan True jika kedua variabel menunjuk ke objek yang sama:

x = ["apple", "banana"]
y = ["apple", "banana"]
z = x

print(x is z)
print(x is y)
print(x == y)

Perbedaan Antara is dan ==

  • is - Memeriksa apakah kedua variabel menunjuk ke objek yang sama di memori

  • == - Memeriksa apakah nilai kedua variabel sama

x = [1, 2, 3]
y = [1, 2, 3]

print(x == y)
print(x is y)

Membership Operators

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

Operator

Description

Example

in

Returns True if a sequence with the specified value is present in the object

x in y

not in

Returns True if a sequence with the specified value is not present in the object

x not in y

Check if "banana" is present in a list:

fruits = ["apple", "banana", "cherry"]
print("banana" in fruits)

Membership in Strings

Operator keanggotaan juga bekerja dengan string:

text = "Hello World"

print("H" in text) # return True
print("hello" in text) # return False
print("z" not in text) # return True

Bitwise Operators

Operator bitwise digunakan untuk membandingkan angka (biner):

Ops

Nama

Contoh

Deskripsi

&

AND

x & y

Sets each bit to 1 if both bits are 1

|

OR

x | y

Sets each bit to 1 if one of two bits is 1

^

XOR

x ^ y

Sets each bit to 1 if only one of two bits is 1

~

NOT

~x

Inverts all the bits

<<

Zero fill left shift

x << 2

Shift left by pushing zeros in from the right and let the leftmost bits fall off

>>

Signed right shift

x >> 2

Shift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off

Contoh

# The & operator compares each bit and set it to 1 if both are 1, otherwise it is set to 0
# 6 = 0110
# 3 = 0011
# --------
# 2 = 0010

print(6 & 3) # return 2
# The | operator compares each bit and set it to 1 if one or both is 1, otherwise it is set to 0
# 6 = 0110
# 3 = 0011
# --------
# 7 = 0111

print(6 | 3) # return 7

Operator Precedence

Prioritas (precedence) operator menggambarkan urutan pelaksanaan operasi.

print((6 + 3) - (6 + 3)) # return 0

"""
Parenthesis have the highest precedence, and need to be evaluated first.
The calculation above reads 9 - 9 = 0
"""
print(100 + 5 * 3) # return 115

"""
Multiplication has higher precedence than addition, and needs to be evaluated first.
The calculation above reads 100 + 15 = 115
"""

Urutan Prioritas

Urutan prioritas dijelaskan dalam tabel di bawah ini, dimulai dari prioritas tertinggi di bagian atas:

Operator

Description

()

Parentheses

**

Exponentiation

+x -x ~x

Unary plus, unary minus, and bitwise NOT

* / // %

Multiplication, division, floor division, and modulus

+ -

Addition and subtraction

<< >>

Bitwise left and right shifts

&

Bitwise AND

^

Bitwise XOR

|

Bitwise OR

== != > >= < <= is is not in not in

Comparisons, identity, and membership operators

not

Logical NOT

and

AND

or

OR