Conditional statements และตัวดำเนินการ

Python เบื้องต้นสำหรับนักพัฒนา

Jasmin Ludolf

Senior Data Science Content Developer

Boolean

# Boolean variable
the_truth = True
print(the_truth)
True
  • ใช้สำหรับการเปรียบเทียบ
Python เบื้องต้นสำหรับนักพัฒนา

ตัวดำเนินการ

  • ตัวดำเนินการเปรียบเทียบ

    • สัญลักษณ์หรือชุดของสัญลักษณ์
    • ใช้เปรียบเทียบค่า
  • ตรวจสอบว่าสองค่าเท่ากันหรือไม่

    • ==
Python เบื้องต้นสำหรับนักพัฒนา

การตรวจสอบความเท่ากัน

# Compare if 2 is equal to 3
2 == 3
False
# Check that 2 is not equal to 3
2 != 3
True
  • กรณีใช้งานทั่วไป: ตรวจสอบข้อมูลล็อกอิน
Python เบื้องต้นสำหรับนักพัฒนา

ตัวดำเนินการเปรียบเทียบตัวเลข

# Is 5 less than 7?
5 < 7
True
# Is 5 less than or equal to 7?
5 <= 7
True
# Is 5 greater than 7?
5 > 7
False
# Is 5 greater or equal to 7?
5 >= 7
False
Python เบื้องต้นสำหรับนักพัฒนา

การเปรียบเทียบประเภทอื่น

# Is James greater than Brian
"James" > "Brian"
True
  • สตริงถูกประเมินตามลำดับตัวอักษร
Python เบื้องต้นสำหรับนักพัฒนา

Conditional statements

  • หากเงื่อนไข if เป็นจริง จะดำเนินการ มิฉะนั้นจะข้ามไป
# Check pasta quantities
required_quantity = 500
pasta_quantity = 200


# Compare pasta quantities if pasta_quantity >= required_quantity
Python เบื้องต้นสำหรับนักพัฒนา

Conditional statements

  • หากเงื่อนไข if เป็นจริง จะดำเนินการ มิฉะนั้นจะข้ามไป
# Check pasta quantities
required_quantity = 500
pasta_quantity = 200


# Compare pasta quantities if pasta_quantity >= required_quantity:
Python เบื้องต้นสำหรับนักพัฒนา

Conditional statements

  • หากเงื่อนไข if เป็นจริง จะดำเนินการ มิฉะนั้นจะข้ามไป
# Check pasta quantities
required_quantity = 500
pasta_quantity = 200


# Compare pasta quantities if pasta_quantity >= required_quantity:
print("You have enough pasta!")
Python เบื้องต้นสำหรับนักพัฒนา

การเยื้องบรรทัด

# Check pasta quantities
required_quantity = 500
pasta_quantity = 200


# Compare pasta quantities if pasta_quantity >= required_quantity:
print("You have enough pasta!") # This line is not indented
    print("You have enough pasta!")
    ^
IndentationError: expected an indented block
Python เบื้องต้นสำหรับนักพัฒนา

คำสั่ง Elif

# Check pasta quantities
required_quantity = 500
pasta_quantity = 200

# Compare pasta quantities if pasta_quantity >= required_quantity:
print("You have enough pasta!")
elif pasta_quantity >= 300: print("Nearly enough pasta. Try a smaller portion.")
  • ใช้คีย์เวิร์ด elif ได้มากเท่าที่ต้องการ!
Python เบื้องต้นสำหรับนักพัฒนา

คำสั่ง Else

# Check pasta quantities
required_quantity = 500
pasta_quantity = 200

# Compare pasta quantities if pasta_quantity >= required_quantity:
print("You have enough pasta!")
elif pasta_quantity >= 300: print("Nearly enough pasta. Try a smaller portion.")
# Otherwise... else: print("Not enough pasta.")
Not enough pasta.
Python เบื้องต้นสำหรับนักพัฒนา

สรุปตัวดำเนินการเปรียบเทียบ

ตัวดำเนินการ ฟังก์ชัน
== เท่ากับ
!= ไม่เท่ากับ
> มากกว่า
>= มากกว่าหรือเท่ากับ
< น้อยกว่า
<= น้อยกว่าหรือเท่ากับ
คีย์เวิร์ด ฟังก์ชัน การใช้งาน
if หากเงื่อนไขเป็นจริง ใช้เป็นลำดับแรก
elif ถ้าไม่ใช่ ให้ตรวจเงื่อนไขถัดไป หลัง if
else ถ้าไม่ใช่ ให้ดำเนินการนี้ หลัง elif
Python เบื้องต้นสำหรับนักพัฒนา

มาฝึกกันเถอะ!

Python เบื้องต้นสำหรับนักพัฒนา

Preparing Video For Download...