Notes & software courses · Free to learn
Aph's Blog
On this page

วันที่ 9 — เงื่อนไข (Conditionals)

👋 อ่านฟรีทั้งหมดบน Aph's Blog — เนื้อหาภาษาไทย ทำตามทีละหน้าใน sidebar ได้เลย หากมีข้อเสนอแนะหรืออยากให้เพิ่มหัวข้อไหน บอกได้เสมอ

เรียนรู้การควบคุมทิศทางของโปรแกรมด้วย if, elif, else และ logical operators

เงื่อนไข (Conditionals)

โดย default คำสั่งใน Python script จะถูกรันเรียงตามลำดับจากบนลงล่าง ถ้า processing logic ต้องการ การรันตามลำดับสามารถเปลี่ยนได้สองวิธี:

  • Conditional execution: block ของคำสั่งหนึ่งตัวหรือมากกว่าจะถูกรันถ้า expression หนึ่งเป็น true
  • Repetitive execution: block ของคำสั่งจะถูกรันซ้ำตราบเท่าที่ expression หนึ่งยังเป็น true ในส่วนนี้จะเรียน if, else, elif statements ตัวดำเนินการเปรียบเทียบและ logical operators ที่เรียนมาแล้วจะมีประโยชน์มากที่นี่

If Condition

ใน Python และภาษาโปรแกรมอื่น keyword if ใช้ตรวจสอบว่าเงื่อนไขเป็น true หรือไม่แล้วรัน block code จำ indentation หลัง colon:

python
# syntax
if condition:
    this part of code runs for truthy conditions

ตัวอย่าง 1:

python
a = 3
if a > 0:
    print('A is a positive number')
# A is a positive number

จากตัวอย่างข้างต้น 3 มากกว่า 0 เงื่อนไขเป็น true และ block code ถูกรัน แต่ถ้าเงื่อนไขเป็น false เราจะไม่เห็นผลลัพธ์ เพื่อให้เห็นผลลัพธ์เมื่อเงื่อนไขเป็น false เราต้องใช้ else

If Else

ถ้าเงื่อนไขเป็น true block แรกจะถูกรัน ถ้าไม่ใช่ else block จะรัน:

python
# syntax
if condition:
    this part of code runs for truthy conditions
else:
     this part of code runs for false conditions
python
a = 3
if a < 0:
    print('A is a negative number')
else:
    print('A is a positive number')

เงื่อนไขข้างต้นเป็น false ดังนั้น else block จึงถูกรัน ถ้าเงื่อนไขมีมากกว่าสองแบบเราใช้ elif

If Elif Else

ในชีวิตประจำวันเราตัดสินใจโดยตรวจสอบหลายเงื่อนไข เช่นเดียวกับชีวิต การเขียนโปรแกรมก็เต็มไปด้วยเงื่อนไข เราใช้ elif เมื่อมีหลายเงื่อนไข:

python
# syntax
if condition:
    code
elif condition:
    code
else:
    code
python
a = 0
if a > 0:
    print('A is a positive number')
elif a < 0:
    print('A is a negative number')
else:
    print('A is zero')

Short Hand

python
# syntax
code if condition else code
python
a = 3
print('A is positive') if a > 0 else print('A is negative') # first condition met, 'A is positive' will be printed

Nested Conditions

เงื่อนไขสามารถซ้อนกันได้:

python
# syntax
if condition:
    code
    if condition:
    code
python
a = 0
if a > 0:
    if a % 2 == 0:
        print('A is a positive and even integer')
    else:
        print('A is a positive number')
elif a == 0:
    print('A is zero')
else:
    print('A is a negative number')

เราหลีกเลี่ยง nested condition ได้โดยใช้ logical operator and:

If Condition และ Logical Operators

python
# syntax
if condition and condition:
    code
python
a = 0
if a > 0 and a % 2 == 0:
        print('A is an even and positive integer')
elif a > 0 and a % 2 !=  0:
     print('A is a positive integer')
elif a == 0:
    print('A is zero')
else:
    print('A is negative')

If และ Or Logical Operators

python
# syntax
if condition or condition:
    code
python
user = 'James'
access_level = 3
if user == 'admin' or access_level >= 4:
        print('Access granted!')
else:
    print('Access denied!')

💻 แบบฝึกหัด — วันที่ 9

ระดับ 1

  1. รับ input อายุจากผู้ใช้ด้วย input("Enter your age: ") ถ้าผู้ใช้อายุ 18 ปีขึ้นไปให้แสดง: You are old enough to drive. ถ้าต่ำกว่า 18 ให้แสดงว่าต้องรออีกกี่ปี
sh
Enter your age: 30
You are old enough to learn to drive.

Enter your age: 15
You need 3 more years to learn to drive.
  1. เปรียบเทียบค่า my_age กับ your_age ด้วย if…else ใครอายุมากกว่ากัน? ใช้ input("Enter your age: ") รับอายุ ใช้ nested condition เพื่อ print 'year' ถ้าต่างกัน 1 ปี 'years' ถ้าต่างกันมากกว่า และข้อความ custom ถ้าอายุเท่ากัน
sh
Enter your age: 30
You are 5 years older than me.
  1. รับตัวเลขสองตัวจากผู้ใช้ ถ้า a มากกว่า b ให้แสดง a is greater than b ถ้า a น้อยกว่า b ให้แสดง a is smaller than b ไม่งั้นให้แสดง a is equal to b
sh
Enter number one: 4
Enter number two: 3
4 is greater than 3

ระดับ 2

  1. เขียนโค้ดที่ให้เกรดนักเรียนตามคะแนน:
sh
90-100, A
80-89, B
70-79, C
60-69, D
0-59, F
  1. รับเดือนจาก input แล้วเช็คว่าเป็นฤดูอะไร:
    September, October หรือ November = Autumn
    December, January หรือ February = Winter
    March, April หรือ May = Spring
    June, July หรือ August = Summer
  1. List ต่อไปนี้มีผลไม้:
python
fruits = ['banana', 'orange', 'mango', 'lemon']

ถ้าผลไม้ไม่มีใน list ให้เพิ่มแล้ว print list ที่แก้ไขแล้ว ถ้ามีอยู่แล้วให้ print: That fruit already exist in the list

ระดับ 3

  1. Person dictionary ต่อไปนี้ (แก้ไขได้ตามต้องการ):
python
person={
    'first_name': 'Asabeneh',
    'last_name': 'Yetayeh',
    'age': 250,
    'country': 'Finland',
    'is_married': True,
    'skills': ['JavaScript', 'React', 'Node', 'MongoDB', 'Python'],
    'address': {
        'street': 'Space street',
        'zipcode': '02210'
    }
}
  • เช็คว่า person dictionary มี key skills หรือไม่ ถ้ามีให้ print middle skill ใน skills list
  • เช็คว่า person มี skill 'Python' หรือไม่แล้ว print ผลลัพธ์
  • ถ้า skills มีแค่ JavaScript กับ React ให้ print 'He is a front end developer' ถ้ามี Node, Python, MongoDB ให้ print 'He is a backend developer' ถ้ามี React, Node และ MongoDB ให้ print 'He is a fullstack developer' ไม่งั้นให้ print 'unknown title'
  • ถ้า person แต่งงานแล้วและอยู่ใน Finland ให้ print ข้อมูลในรูปแบบ: Asabeneh Yetayeh lives in Finland. He is married.