Python Operators

   อัปเดตล่าสุด April 21, 2024

Python Operator คืออะไร 

Operator ในภาษา Python คือสัญลักษณ์หรือคำสงวนที่ใช้สำหรับการดำเนินการทางคณิตศาสตร์ การเปรียบเทียบ และการกำหนดค่า ตัวอย่างเช่น

x = 10
y = 5

print(x + y) # Output: 15
print(x > y) # Output: True

ในตัวอย่างนี้  +  และ  >  คือ operator ที่ใช้สำหรับการบวกและการเปรียบเทียบค่าตามลำดับ

ประเภทของ Operators

Operator ใน Python สามารถแบ่งได้เป็น 7 ประเภทหลัก ๆ ได้แก่

1. Arithmetic Operators

ใช้สำหรับการคำนวณทางคณิตศาสตร์ เช่น +, -, *, /, %, **, //

x = 10
y = 3

print(x + y) # Output: 13
print(x - y) # Output: 7
print(x * y) # Output: 30
print(x / y) # Output: 3.3333333333333335
print(x % y) # Output: 1
print(x ** y) # Output: 1000
print(x // y) # Output: 3


2. Assignment Operators

ใช้สำหรับการกำหนดค่าให้กับตัวแปร เช่น =, +=, -=, *=, /=, %=, **=, //=

x = 5
x += 3 # equivalent to x = x + 3
print(x) # Output: 8

y = 10
y //= 3 # equivalent to y = y // 3
print(y) # Output: 3


3. Comparison Operators

ใช้สำหรับการเปรียบเทียบค่า เช่น ==, !=, >, <, >=, <=

x = 5
y = 3

print(x == y) # Output: False
print(x != y) # Output: True
print(x > y) # Output: True
print(x < y) # Output: False
print(x >= y) # Output: True
print(x <= y) # Output: False


4. Logical Operators

ใช้สำหรับการรวมเงื่อนไขทางตรรกะ เช่น  and ,  or ,  not 

x = 5
y = 3

print(x > 3 and y < 5) # Output: True
print(x > 3 or y > 5) # Output: True
print(not(x > 3)) # Output: False


5. Identity Operators

ใช้สำหรับการเปรียบเทียบว่าวัตถุสองอย่างเป็นวัตถุเดียวกันหรือไม่ เช่น  is  is not  (ยังกับเขียนภาษาอังกฤษน่ะ :))

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

print(x is z) # Output: True
print(x is y) # Output: False
print(x == y) # Output: True


6. Membership Operators

ใช้สำหรับการตรวจสอบว่าค่าหนึ่งอยู่ในลำดับหรือไม่ เช่น  in  not in 

x = ["apple", "banana"]

print("banana" in x) # Output: True
print("cherry" in x) # Output: False


7. Bitwise Operators

ใช้สำหรับการดำเนินการระดับบิต เช่น &, |, ^, ~, <<, >>

x = 10 # Binary: 1010
y = 4 # Binary: 0100

print(x & y) # Output: 0
print(x | y) # Output: 14
print(x ^ y) # Output: 14
print(~x) # Output: -11
print(x << 2) # Output: 40
print(x >> 2) # Output: 2


Operator เป็นส่วนสำคัญในการเขียนโปรแกรมด้วยภาษา Python ที่ช่วยให้สามารถดำเนินการและจัดการข้อมูลได้อย่างมีประสิทธิภาพ ความเข้าใจในการใช้งาน operator แต่ละประเภทจะช่วยให้สามารถเขียนโค้ดที่มีประสิทธิภาพและอ่านง่ายมากยิ่งขึ้นไปอีกครับ


ตารางตัวดำเนินการทางคณิตศาสตร์ (Arithmetics Operators)

เครื่องหมาย

ชื่อ/ความหมาย

ตัวอย่าง

 + 

บวก (Addition)

3 + 5 = 8

 - 

ลบ (Subtraction)

7 - 4 = 3

 * 

คูณ (Multiplication)

2 * 6 = 12

 / 

หาร (Division)

10 / 2 = 5

 % 

หารเอาเศษ (Modulas หรือมักเรียกสั้น ๆ ว่า Mod)

7 % 3 = 1

 ** 

ยกกำลัง (Exponential)

2 ** 3 = 8

 // 

หารไม่เอาเศษ (Floor Division)

7 // 2 = 3


ตารางตัวดำเนินการเปรียบเทียบ (Comparison Operators)

สัญลักษณ์

ชื่อ/ความหมาย

ตัวอย่าง

 == 

เท่ากับ

5 == 5 (True)

 != 

ไม่เท่ากับ

5 != 3 (True)

 > 

มากกว่า

7 > 4 (True)

 < 

น้อยกว่า

2 < 6 (True)

 >= 

มากกว่าหรือเท่ากับ

4 >= 4 (True)

 <= 

น้อยกว่าหรือเท่ากับ

3 <= 5 (True)




คอร์สเรียนแนะนำ