เอกสารประกอบการสอน 5 การตรวจสอบเง อนไข

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

เงื่อนไขเชิงตรรกศาสตร์ (Logical conditions)

In [ ]: # > มากกว่า

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

# < น้อยกว่า

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

# == เท่ากับ

# != ไม่เท่ากับ

# is เป็ นวัตถุเดียวกัน

# is not ไม่ได้เป็ นวัตถุเดียวกัน

In [7]: a = 3

a == 4

False
Out[7]:

In [6]: print(a)

การตรวจสอบเงื่อนไขหลาย ๆ เงื่อนไขพร้อมกัน จะเป็ นจริงเมื่อทุกเงื่อนไขเป็ นจริง และจะเป็ นเท็จหากมี


เพียงหนึ่งเงื่นไขเป็ นเท็จ

In [11]: X = 2

Y = 4

Z = 6

print(X < Y < Z)

print(X < Y and Y < Z)

print(X < Y > Z)

print(X < Y and Y > Z)

True

True

False

False

สามารถทำการเชื่อมประพจน์และนิเสธของประพจน์ได้ในลักษณะเดียวกันกับตัวแปรประเภท boolean

In [17]: print( not ( 3>=3 and 3<2 ) )

True

In [18]: print(False+False)

print(False+True)

print(True+True)

print(True+5)

print(True*3.0)

print(False*3.0)

3.0

0.0

In [21]: list1 = ['a', 'b', 'c']

list2 = ['a', 'b', 'c']

list3 = list1

print(list1 is not list2)

True

คำสั่ง if-elif-else
Syntax
if condition:

do something

do something

...

elif condition:

do another thing

do another thing

...

else:

otherwise, do this

otherwise, do this

...

In [33]: a = 12

b = 12.0

In [34]: if a > b:

print('a is higher than b')

elif a < b:

print('a is less than b')

else:

print('a must be equal to b')

a must be equal to b

In [ ]: if a<b:

print("a is less than b")

elif a>b:

print("a is higher than b")

else:

print("a must be equal to b")

a must be equal to b

แบบฝึกหัด
จงรับ string จากคีย์บอร์ด แล้วตรวจสอบว่าเป็ นตัวเลขหรือหนังสือ

In [42]: string = input("Please type something")

Please type something12345

In [51]: string = input("Please type something")

if string.isalpha():

print('It is a text')

elif string.isnumeric():

print("It is a number")

elif string.isalnum():

print("It is both alphabet and number.")

else:

print("I don't know")

Please type somethingKMUTT@2022

I don't know

การตรวจสอบว่า string ดังกล่าวเป็ นตัวเลขหรือไม่

In [49]: string.isnumeric()

False
Out[49]:

In [3]: string = input('type something:')

type something:Bangkok1234

In [4]: if string.isnumeric():

print("It's a number")

elif string.isalpha(): #การตรวจสอบว่า string ดังกล่าวเป็ นพยัญชนะหรือไม่

print("It's a text")

else:

print("I don't know")

I don't know

การตรวจสอบเงื่อนไปแบบมีหลายระดับ หรือ if
ซ้อน if (Nested if)
จงรับค่าจากคีย์บอร์ดแล้วตรวจสอบว่าเป็ น พยัญชนะ หรือ ตัวเลข หรือ ทั้งพยัญชนะหรือตัวเลขรวมกัน

ถ้าเป็ นกลุ่มของพยัญชนะ ตรวจสอบว่าเป็ นตัวพิมพ์เล็กหรือพิมพ์ใหญ่หรือเป็ น title (ตัวพิมพ์ใหญ่เฉพาะตัว


แรก)

In [55]: string = input("Please type something")

if string.isalpha():

print('It is a text')

if string.islower():

print("It's lower case.")

elif string.isupper():

print("It's upper case.")

elif string.istitle():

print("It's a title.")

elif string.isnumeric():

print("It is a number")

elif string.isalnum():

print("It is both alphabet and number.")

else:

print("I don't know")

Please type somethingBangkok1234

It is both alphabet and number.

You might also like