Function builtin python ม ก ฟ งก ช น

จริงๆแล้วเรายังสามารถนำมาประยุกต์ใช้กรณีที่ต้องการแปลงตัวอักษา

> ascii(1) '1'
ascii('hey') "'hey'"
ascii([]) '[]' ascii({}) '{}'>>> type(ascii([])) <class 'str'>>>> type(ascii({})) <class 'str'>>>> type(ascii(1)) <class 'str'>>>> type(ascii('encode')) <class 'str'>>>> ascii('รักเธอ') "'\u0e23\u0e31\u0e01\u0e40\u0e18\u0e2d'"

7 ให้เป็น

> ascii(1) '1'
ascii('hey') "'hey'"
ascii([]) '[]' ascii({}) '{}'>>> type(ascii([])) <class 'str'>>>> type(ascii({})) <class 'str'>>>> type(ascii(1)) <class 'str'>>>> type(ascii('encode')) <class 'str'>>>> ascii('รักเธอ') "'\u0e23\u0e31\u0e01\u0e40\u0e18\u0e2d'"

8 ก็สามารถทำได้ในลักษณะนี้

ปกติ A=65 นะ เราจะนำมาลบ 64 จะเริ่มต้นที่ค่า 1 (ทีนี้เราจะได้ A-Z ที่ได้ 1-26 โดยไม่ต้องเขียนยาวๆเลยนะ)

จาก student_code ที่มี Z อยู่ภายใน, function จะแปลงให้เป็น 26 เองเลย

student_code = '59Z10053211'

def convert_student_code_alphabet_to_number(student_code: str):

result = []  
for code in student_code:  
    if code.isdigit():  
        result.append(int(code))  
    else:  
        result.append(ord(code.upper()) - 64)  
return result
print(convert_student_code_alphabet_to_number(student_code=student_code))

output

[5, 9, 26, 1, 0, 0, 5, 3, 2, 1, 1] # z คือ 26 นะ

5. chr(i) เป็นการแปลงค่าย้อนกลับหรือ เป็น inverse of ord หรือ คือการเแปลง interger (Decimal) ให้เป็น character (ASCII)เช่น

> chr(97) 'a'
chr(65) 'A'
chr(66) 'B'

6. bin(x) เป็นการ return ค่า Binary ของ integer ที่รับเข้ามา โดยผลลัพธ์จะนำหน้าด้วย ob เช่น

> bin(36) '0b100100'
ิิ
bin(11) '0b1011' bin(-4) '-0b100'

7. class bool([x]) เป็นการ return ค่า boolean ของ specified object ด้านใน เช่น

> any([1,0,1]) True
any([0,0,0]) False # แบบนี้คือ ไม่มีตัวไหนเป็น 1 หรือเป็น True เลย>>> any(map(lambda x:x>0,[-7,-1,2,-5])) True # เพราะมี 2 ซึ่งมากกว่า 0 แค่ตัวเดียวก้อ return True เลย
any(map(lambda x:x>0,[-7,-1,0,-5])) False

0

8. breakpoint(*args, **kws) เป็นการ set breakpoint การใช้งานจะเหมือนกับการใช้ pdb แบบเดิมๆคับ

ซึ่งในการใช้งานเราเพียงแค่เพิ่ม breakpoint() ในจุดที่ต้องการ เช่น

> any([1,0,1]) True
any([0,0,0]) False # แบบนี้คือ ไม่มีตัวไหนเป็น 1 หรือเป็น True เลย>>> any(map(lambda x:x>0,[-7,-1,2,-5])) True # เพราะมี 2 ซึ่งมากกว่า 0 แค่ตัวเดียวก้อ return True เลย
any(map(lambda x:x>0,[-7,-1,0,-5])) False

1

เมื่อ i = 5 จะเรียกใช้งาน pdb (แทนการเรียกใช้แบบรูปด้านบน)

ผลลัพธ์เมื่อมาหยุดที่ breakpoint()

9. class bytes([source[, encoding[, errors]]]) เป็น class ของ bytes ที่จะ return a bytes object มาให้เรา โดยสามารถระบุ encoding ได้ด้วย

> any([1,0,1]) True
any([0,0,0]) False # แบบนี้คือ ไม่มีตัวไหนเป็น 1 หรือเป็น True เลย>>> any(map(lambda x:x>0,[-7,-1,2,-5])) True # เพราะมี 2 ซึ่งมากกว่า 0 แค่ตัวเดียวก้อ return True เลย
any(map(lambda x:x>0,[-7,-1,0,-5])) False

2

10. class bytearray([source[, encoding[, errors]]]) สำหรับ bytearray() จะเป็น function ที่ return a bytearray object คล้ายกับ bytes() เพียงแค่เป็น type bytearray() เท่านั้น

> any([1,0,1]) True
any([0,0,0]) False # แบบนี้คือ ไม่มีตัวไหนเป็น 1 หรือเป็น True เลย>>> any(map(lambda x:x>0,[-7,-1,2,-5])) True # เพราะมี 2 ซึ่งมากกว่า 0 แค่ตัวเดียวก้อ return True เลย
any(map(lambda x:x>0,[-7,-1,0,-5])) False

3

11. callable(object) เป็นการตรวจสอบว่าเป็น object ไหม ถ้าใช่ก้อจะ return เป็น True (เพราะ callable), แต่ถ้าไม่ใช่ก้อจะเป็น False

สิ่งที่ callable ได้คือ Functions, classes, methods, Instances of classes

> any([1,0,1]) True
any([0,0,0]) False # แบบนี้คือ ไม่มีตัวไหนเป็น 1 หรือเป็น True เลย>>> any(map(lambda x:x>0,[-7,-1,2,-5])) True # เพราะมี 2 ซึ่งมากกว่า 0 แค่ตัวเดียวก้อ return True เลย
any(map(lambda x:x>0,[-7,-1,0,-5])) False

4เปรียบเทียบ String, function และ class

12. @classmethod เป็นการ Converts a method into a class method หรือ การทำให้ function เป็นเหมือน class หนึ่ง (เพื่อให้เราสามารถสร้าง constructor ได้หลายๆแบบ ให้มากกว่า constructor ที่ผ่านตัว __init__แค่อย่างเดียวเท่านั้น)

> any([1,0,1]) True
any([0,0,0]) False # แบบนี้คือ ไม่มีตัวไหนเป็น 1 หรือเป็น True เลย>>> any(map(lambda x:x>0,[-7,-1,2,-5])) True # เพราะมี 2 ซึ่งมากกว่า 0 แค่ตัวเดียวก้อ return True เลย
any(map(lambda x:x>0,[-7,-1,0,-5])) False

5

13. compile(source, filename, mode, flags=0, dont_inherit=False, optimize=-1) เป็น function ที่ return the specified source as a code object, ready to be executed.

> any([1,0,1]) True
any([0,0,0]) False # แบบนี้คือ ไม่มีตัวไหนเป็น 1 หรือเป็น True เลย>>> any(map(lambda x:x>0,[-7,-1,2,-5])) True # เพราะมี 2 ซึ่งมากกว่า 0 แค่ตัวเดียวก้อ return True เลย
any(map(lambda x:x>0,[-7,-1,0,-5])) False

6

14. class complex([real[, imag]]) เป็น class ที่ return a complex number (จำนวนเชิงซ้อน) เช่น

> any([1,0,1]) True
any([0,0,0]) False # แบบนี้คือ ไม่มีตัวไหนเป็น 1 หรือเป็น True เลย>>> any(map(lambda x:x>0,[-7,-1,2,-5])) True # เพราะมี 2 ซึ่งมากกว่า 0 แค่ตัวเดียวก้อ return True เลย
any(map(lambda x:x>0,[-7,-1,0,-5])) False

7

15. delattr(object, name) ย่อมาจาก (delete atrribute) เป็น function ที่ช่วย deletes attribute จาก class ได้เลยนะ เช่น

> any([1,0,1]) True
any([0,0,0]) False # แบบนี้คือ ไม่มีตัวไหนเป็น 1 หรือเป็น True เลย>>> any(map(lambda x:x>0,[-7,-1,2,-5])) True # เพราะมี 2 ซึ่งมากกว่า 0 แค่ตัวเดียวก้อ return True เลย
any(map(lambda x:x>0,[-7,-1,0,-5])) False

8

​16. class

> ascii(1) '1'
ascii('hey') "'hey'"
ascii([]) '[]' ascii({}) '{}'>>> type(ascii([])) <class 'str'>>>> type(ascii({})) <class 'str'>>>> type(ascii(1)) <class 'str'>>>> type(ascii('encode')) <class 'str'>>>> ascii('รักเธอ') "'\u0e23\u0e31\u0e01\u0e40\u0e18\u0e2d'"

9(**kwarg) class

> ascii(1) '1'
ascii('hey') "'hey'"
ascii([]) '[]' ascii({}) '{}'>>> type(ascii([])) <class 'str'>>>> type(ascii({})) <class 'str'>>>> type(ascii(1)) <class 'str'>>>> type(ascii('encode')) <class 'str'>>>> ascii('รักเธอ') "'\u0e23\u0e31\u0e01\u0e40\u0e18\u0e2d'"

9(mapping, **kwarg) class

> ascii(1) '1'
ascii('hey') "'hey'"
ascii([]) '[]' ascii({}) '{}'>>> type(ascii([])) <class 'str'>>>> type(ascii({})) <class 'str'>>>> type(ascii(1)) <class 'str'>>>> type(ascii('encode')) <class 'str'>>>> ascii('รักเธอ') "'\u0e23\u0e31\u0e01\u0e40\u0e18\u0e2d'"

9(iterable, **kwarg)

เป็น class ที่จะ return dictionary ออกมาโดยรับอะไรเข้าไปก็ได้ เป็น keyword arguments เช่น

> any([1,0,1]) True
any([0,0,0]) False # แบบนี้คือ ไม่มีตัวไหนเป็น 1 หรือเป็น True เลย>>> any(map(lambda x:x>0,[-7,-1,2,-5])) True # เพราะมี 2 ซึ่งมากกว่า 0 แค่ตัวเดียวก้อ return True เลย
any(map(lambda x:x>0,[-7,-1,0,-5])) False

9

17. dir([object]) เป็น function ที่จะ return lists ของ properties ทั้งหมด และ methods ของ objects นั้นๆออกมาให้เห็น โดยที่ไม่มี values ออกมานะ เช่น

data = [

1,  
'end_date',  
3,  
4,  
5  
]
has_data = any(key in data for key in ['start_date', 'end_date']) print(has_data)

Output: True

0

18. divmod(a, b) เป็น function ที่ return ผลหาร เป็น tuple ของผลหารและเศษ เช่น

data = [

1,  
'end_date',  
3,  
4,  
5  
]
has_data = any(key in data for key in ['start_date', 'end_date']) print(has_data)

Output: True

1

19. enumerate(iterable, start=0) อ่านว่า (อะนิวมะเรท) แปลว่า แจกแจงนะคับ คือ ใช้ในการแจกแจงค่า index และข้อมูลใน index ในรูปแบบ Tuple ดังนี้ (Index, Value) โดยต้องใช้กับข้อมูลชนิด list โดยมี syntax คือ

enumerate(iterable, start)

data = [

1,  
'end_date',  
3,  
4,  
5  
]
has_data = any(key in data for key in ['start_date', 'end_date']) print(has_data)

Output: True

2

x เป็น tuple เก็บ String ไว้ 3 ค่า เมื่อทำการแจกแจงออกมาแล้วเก็บค่าไว้ที่ตัวแปร y และนำมาเก็บใน list จึงได้ [(0, ‘apple’), (1, ‘banana’), (2, ‘cherry’)] นั่นเอง

เปรียบเทียบระหว่าง for กับ for enumerate()ปกติค่าเริ่มต้น enumerate() จะเริ่มที่ 0 แต่เรากำหนด argument ตัวที่ 2 ของ enumerate() ให้เริ่มที่เท่าไหร่ก็ได้

20. eval(expression[, globals[, locals]]) eval คือ evaluates เป็น function ที่จะทำการเช็คตัว expression ถ้าถูกต้องตามรูปแบบ Python ก้อจะทำการ execute ให้เลย มี syntax คือ

eval(expression, globals, locals)

data = [

1,  
'end_date',  
3,  
4,  
5  
]
has_data = any(key in data for key in ['start_date', 'end_date']) print(has_data)

Output: True

3

21. exec(object[, globals[, locals]]) เป็น function ที่จะ execute code ที่เราได้ระบุไว้ เช่น

data = [

1,  
'end_date',  
3,  
4,  
5  
]
has_data = any(key in data for key in ['start_date', 'end_date']) print(has_data)

Output: True

4

22. filter(function, iterable) เป็นการกรองค่าออกมาจาก iterable object เช่น ในตัวอย่างด้านล่าง จะทำการ print(x) เมื่อ ages มีค่ามากกว่าหรือเท่ากับ 18 เท่านั้น เพราะ return เป็น True

data = [

1,  
'end_date',  
3,  
4,  
5  
]
has_data = any(key in data for key in ['start_date', 'end_date']) print(has_data)

Output: True

5

23. class float([x]) อันนี้น่าจะใช้กันบ่อย คือ แปลงค่าให้เป็น float นั่นเอง

data = [

1,  
'end_date',  
3,  
4,  
5  
]
has_data = any(key in data for key in ['start_date', 'end_date']) print(has_data)

Output: True

6

24. format(value[, format_spec]) อันนี้ใช้ในการ format a specified value into a specified format เช่น

data = [

1,  
'end_date',  
3,  
4,  
5  
]
has_data = any(key in data for key in ['start_date', 'end_date']) print(has_data)

Output: True

7

ที่จะใช้อีกที่หนึ่งก้อคือตอนสั่ง print แล้วใช้ .format() ร่วมด้วย เช่น

data = [

1,  
'end_date',  
3,  
4,  
5  
]
has_data = any(key in data for key in ['start_date', 'end_date']) print(has_data)

Output: True

8

25.

> ord("a") 97
ord("A") 65
ord("B") 66

2(function, iterable, ...) เป็นการ map ให้ได้ค่าใหม่ที่เราต้องการ โดยจะใส่ iterable กี่ชุดก้อได้

data = [

1,  
'end_date',  
3,  
4,  
5  
]
has_data = any(key in data for key in ['start_date', 'end_date']) print(has_data)

Output: True

9

อีก

> ord("a") 97
ord("A") 65
ord("B") 66

3 นะคับ สมมติว่าเราต้องการแยก

> ord("a") 97
ord("A") 65
ord("B") 66

4 แบบไม่มี

> ord("a") 97
ord("A") 65
ord("B") 66

5 ออกมา เราจะใช้

> ord("a") 97
ord("A") 65
ord("B") 66

6ไม่ได้นะ ลองใช้

> ord("a") 97
ord("A") 65
ord("B") 66

2 แทนก็น่าสนใจ

โดยใส่

> ord("a") 97
ord("A") 65
ord("B") 66

8 เข้าไปจะเป็น

> ord("a") 97
ord("A") 65
ord("B") 66

9,

ปกติ A=65 นะ เราจะนำมาลบ 64 จะเริ่มต้นที่ค่า 1 (ทีนี้เราจะได้ A-Z ที่ได้ 1-26 โดยไม่ต้องเขียนยาวๆเลยนะ)

จาก student_code ที่มี Z อยู่ภายใน, function จะแปลงให้เป็น 26 เองเลย

student_code = '59Z10053211'

def convert_student_code_alphabet_to_number(student_code: str):

result = []  
for code in student_code:  
    if code.isdigit():  
        result.append(int(code))  
    else:  
        result.append(ord(code.upper()) - 64)  
return result
print(convert_student_code_alphabet_to_number(student_code=student_code))

output

[5, 9, 26, 1, 0, 0, 5, 3, 2, 1, 1] # z คือ 26 นะ

0 ก็ได้ และตามด้วย

ปกติ A=65 นะ เราจะนำมาลบ 64 จะเริ่มต้นที่ค่า 1 (ทีนี้เราจะได้ A-Z ที่ได้ 1-26 โดยไม่ต้องเขียนยาวๆเลยนะ)

จาก student_code ที่มี Z อยู่ภายใน, function จะแปลงให้เป็น 26 เองเลย

student_code = '59Z10053211'

def convert_student_code_alphabet_to_number(student_code: str):

result = []  
for code in student_code:  
    if code.isdigit():  
        result.append(int(code))  
    else:  
        result.append(ord(code.upper()) - 64)  
return result
print(convert_student_code_alphabet_to_number(student_code=student_code))

output

[5, 9, 26, 1, 0, 0, 5, 3, 2, 1, 1] # z คือ 26 นะ

1 นั่นคือ

ปกติ A=65 นะ เราจะนำมาลบ 64 จะเริ่มต้นที่ค่า 1 (ทีนี้เราจะได้ A-Z ที่ได้ 1-26 โดยไม่ต้องเขียนยาวๆเลยนะ)

จาก student_code ที่มี Z อยู่ภายใน, function จะแปลงให้เป็น 26 เองเลย

student_code = '59Z10053211'

def convert_student_code_alphabet_to_number(student_code: str):

result = []  
for code in student_code:  
    if code.isdigit():  
        result.append(int(code))  
    else:  
        result.append(ord(code.upper()) - 64)  
return result
print(convert_student_code_alphabet_to_number(student_code=student_code))

output

[5, 9, 26, 1, 0, 0, 5, 3, 2, 1, 1] # z คือ 26 นะ

2

data = {

'start_date': 'love',  
'q': 'pig'  
}

result = any(key in data.keys() for key in ['start_date', 'end_date']) print(result)

Output: True

0

26. open(file, mode)

เป็น function สำหรับเปิดไฟล์ และ return ออกมาเป็น file object.

data = {

'start_date': 'love',  
'q': 'pig'  
}

result = any(key in data.keys() for key in ['start_date', 'end_date']) print(result)

Output: True

1Parameter Values

27. next(iterable, default)

เป็น function ที่จะ return next item ใน iterator (iter) ออกมา เช่น

data = {

'start_date': 'love',  
'q': 'pig'  
}

result = any(key in data.keys() for key in ['start_date', 'end_date']) print(result)

Output: True

2

แต่ถ้าข้อมูลเราเป็น list ธรรมดา จะใช้ next() ไม่ได้ ต้องแปลงเป็น iterable ด้วยฟังก์ชัน iter() ในลักษณะนี้

data = {

'start_date': 'love',  
'q': 'pig'  
}

result = any(key in data.keys() for key in ['start_date', 'end_date']) print(result)

Output: True

3

แบบนี้ใช้ไม่ได้ ต้องแปลงด้วยฟังก์ชัน iter()

data = {

'start_date': 'love',  
'q': 'pig'  
}

result = any(key in data.keys() for key in ['start_date', 'end_date']) print(result)

Output: True

4

จากนั้นก็ใช้งาน next() ได้ล่ะ

data = {

'start_date': 'love',  
'q': 'pig'  
}

result = any(key in data.keys() for key in ['start_date', 'end_date']) print(result)

Output: True

5https://ichi.pro/th/iterable-vs-iterator-ni-python-261276439314782

Iterables คือการทำอะไรซ้ำๆ โดยปกติจะทำกับ List หรือ Array เนื้อหา iterable สามารถอ่านได้สน link คับ

28. reversed(sequence)

อันนี้ตรงๆตัวเลยคับ คือ ช่ย reverse และ return ออกมาเป็น object ฉะนั้นจึงต้องใช้ loop for ช่วยถ้าต้องการดูค่าภายใน

data = {

'start_date': 'love',  
'q': 'pig'  
}

result = any(key in data.keys() for key in ['start_date', 'end_date']) print(result)

Output: True

6

29. vars(object)

เป็น function ที่ returns the __dic__ attribute of an object __dict__ attribute คือ dictionary ที่เก็บ object ของ attribute ที่สามารถเปลี่ยนแปลงได้

data = {

'start_date': 'love',  
'q': 'pig'  
}

result = any(key in data.keys() for key in ['start_date', 'end_date']) print(result)

Output: True

7

30. zip(iterator1, iterator2, iterator3 …)

เป็น function ที่ return a zip object คือ ช่วยให้เรารวมข้อมูลใน list ตำแหน่งเดียวกัน ให้จับคู่กันได้ ไม่ว่าจะให้ออกมาเป็น

ปกติ A=65 นะ เราจะนำมาลบ 64 จะเริ่มต้นที่ค่า 1 (ทีนี้เราจะได้ A-Z ที่ได้ 1-26 โดยไม่ต้องเขียนยาวๆเลยนะ)

จาก student_code ที่มี Z อยู่ภายใน, function จะแปลงให้เป็น 26 เองเลย

student_code = '59Z10053211'

def convert_student_code_alphabet_to_number(student_code: str):

result = []  
for code in student_code:  
    if code.isdigit():  
        result.append(int(code))  
    else:  
        result.append(ord(code.upper()) - 64)  
return result
print(convert_student_code_alphabet_to_number(student_code=student_code))

output

[5, 9, 26, 1, 0, 0, 5, 3, 2, 1, 1] # z คือ 26 นะ

3,

ปกติ A=65 นะ เราจะนำมาลบ 64 จะเริ่มต้นที่ค่า 1 (ทีนี้เราจะได้ A-Z ที่ได้ 1-26 โดยไม่ต้องเขียนยาวๆเลยนะ)

จาก student_code ที่มี Z อยู่ภายใน, function จะแปลงให้เป็น 26 เองเลย

student_code = '59Z10053211'

def convert_student_code_alphabet_to_number(student_code: str):

result = []  
for code in student_code:  
    if code.isdigit():  
        result.append(int(code))  
    else:  
        result.append(ord(code.upper()) - 64)  
return result
print(convert_student_code_alphabet_to_number(student_code=student_code))

output

[5, 9, 26, 1, 0, 0, 5, 3, 2, 1, 1] # z คือ 26 นะ

4 หรือ

> ascii(1) '1'
ascii('hey') "'hey'"
ascii([]) '[]' ascii({}) '{}'>>> type(ascii([])) <class 'str'>>>> type(ascii({})) <class 'str'>>>> type(ascii(1)) <class 'str'>>>> type(ascii('encode')) <class 'str'>>>> ascii('รักเธอ') "'\u0e23\u0e31\u0e01\u0e40\u0e18\u0e2d'"

9 ก็ได้ อยู่ที่เราจะกำหนดให้กับ

ปกติ A=65 นะ เราจะนำมาลบ 64 จะเริ่มต้นที่ค่า 1 (ทีนี้เราจะได้ A-Z ที่ได้ 1-26 โดยไม่ต้องเขียนยาวๆเลยนะ)

จาก student_code ที่มี Z อยู่ภายใน, function จะแปลงให้เป็น 26 เองเลย

student_code = '59Z10053211'

def convert_student_code_alphabet_to_number(student_code: str):

result = []  
for code in student_code:  
    if code.isdigit():  
        result.append(int(code))  
    else:  
        result.append(ord(code.upper()) - 64)  
return result
print(convert_student_code_alphabet_to_number(student_code=student_code))

output

[5, 9, 26, 1, 0, 0, 5, 3, 2, 1, 1] # z คือ 26 นะ

6

data = {

'start_date': 'love',  
'q': 'pig'  
}

result = any(key in data.keys() for key in ['start_date', 'end_date']) print(result)

Output: True

8

อาจจะมองไม่เห็นภาพใช่ไหมครับ ว่า used case จริงๆ จะใช้ในกรณีไหน นี่เลย

ตัวอย่างการใช้ zip() จับคู่ด้วย index by index ระหว่าง weight_list และ student_code

data = {

'start_date': 'love',  
'q': 'pig'  
}

result = any(key in data.keys() for key in ['start_date', 'end_date']) print(result)

Output: True

9

31. print(*objects, sep=' ', end='n', file=sys.stdout, flush=False)

if not any([student_registration_code, credit_bank_registration_code]):

raise ValidationError({'detail': 'Student code is required.'})

ถ้าไม่มีค่าเข้ามาเลยทั้ง 2 ตัวแปรใน any() ก็จะทำใน raise นะ

0

print() นี่คือง่ายๆเลยนะคับ วิธีการใช้เพิ่มเติมดูจากที่เขียนไว้ ที่นี่ นะคับ

32. min(arg1, arg2, *args[,key])

โดย function นี้จะคำนวณค่าที่น้อยที่สุดระหว่าง arg1 และ arg2 และ return ค่าที่น้อยที่สุดที่คำนวณได้ออกมา (แต่ arguments ทั้งหมดต้องเป็นข้อมูลชนิดเดียวกันนะคับ)

if not any([student_registration_code, credit_bank_registration_code]):

raise ValidationError({'detail': 'Student code is required.'})

ถ้าไม่มีค่าเข้ามาเลยทั้ง 2 ตัวแปรใน any() ก็จะทำใน raise นะ

1

จะ return ค่า -199 ที่น้อยที่สุดออกมา

33. max(arg1, arg2, *args[, key])

อันนี้จะตรงข้ามกับ min() นะคับ แค่เปลี่ยนจากหาค่าน้อยที่สุดเป็น หาค่ามากที่สุดแทน

if not any([student_registration_code, credit_bank_registration_code]):

raise ValidationError({'detail': 'Student code is required.'})

ถ้าไม่มีค่าเข้ามาเลยทั้ง 2 ตัวแปรใน any() ก็จะทำใน raise นะ

2

34. abs(x)

Function นี้คือ absolute (ค่าสัมบูรณ์) โดยจะทำให้ค่าที่ return ออกมาเป็นเลขจำนวนเต็มบวกเท่านั้น

if not any([student_registration_code, credit_bank_registration_code]):

raise ValidationError({'detail': 'Student code is required.'})

ถ้าไม่มีค่าเข้ามาเลยทั้ง 2 ตัวแปรใน any() ก็จะทำใน raise นะ

3

35. round(number[, ndigits])

จะเป็นการปัดเลขให้เป็นจำนวนเต็มแบบวิทยาศาสตร์เลย คือ ถ้าค่ามากกว่า 0.5 ก็จะปัดให้เป็นจำนวนเต็มเลย เช่น 2.6 ก้อจะปัดให้เป็น 3 ,ส่วน 2.5 ก็ยังเป็น 2 อยู่เป็นต้น

if not any([student_registration_code, credit_bank_registration_code]):

raise ValidationError({'detail': 'Student code is required.'})

ถ้าไม่มีค่าเข้ามาเลยทั้ง 2 ตัวแปรใน any() ก็จะทำใน raise นะ

4

36. pow(base, exp[, mod])

เป็น function ในการคำนวณเลขยกกำลัง โดย base = ฐานที่ใช้ในการยกกำลัง และ exp (exponential) = เลขชี้กำลัง และจะ return ค่าเป็นผลลัพธ์จากการยกกำลังออกมานั่นเอง เช่น

if not any([student_registration_code, credit_bank_registration_code]):

raise ValidationError({'detail': 'Student code is required.'})

ถ้าไม่มีค่าเข้ามาเลยทั้ง 2 ตัวแปรใน any() ก็จะทำใน raise นะ

5

37. hasattr(object, name)

เป็น function ที่ใช้ในการตรวจสอบว่ามี attribute (property/ method) ที่เราระบุไว้ไหม โดยจะ return ออกมาเป็น True หรือ False เช่น

if not any([student_registration_code, credit_bank_registration_code]):

raise ValidationError({'detail': 'Student code is required.'})

ถ้าไม่มีค่าเข้ามาเลยทั้ง 2 ตัวแปรใน any() ก็จะทำใน raise นะ

6

ถัดไปลองมาดูการประยุกต์ใช้ใน Django ดูนะคับ

การสร้าง method เพื่อให้พ่นค่า order_id ออกไปผ่านทาง Serializers โดยใช้ hasattr() เข้ามาช่วยเช็คว่ามี attribute นั้นรึป่าว เราสามารถเช็คค่าใน obj ได้ก่อนด้วย ipdb หรือคำสั่ง dir(obj) เพื่อดูค่า attribute ด้านใน

38. set()

if not any([student_registration_code, credit_bank_registration_code]):

raise ValidationError({'detail': 'Student code is required.'})

ถ้าไม่มีค่าเข้ามาเลยทั้ง 2 ตัวแปรใน any() ก็จะทำใน raise นะ

7

สำหรับ set method เป็น 1 ใน 4 built-in data types (List, Tuple, Dict )ที่ใช้ในการเก็บ collections of data ซึ่งใช้ในการแปลงข้อมูลที่ซ้ำกัน ให้เหลือเพียงแค่ตัวเดียว (Duplicates not allowed)

set จะ collection ที่ unordered และ unindexed นะคับ คือ เราไม่สามารถจัดเรียงและระบุ index ก็ไม่ได้

‘apple’ ซ้ำกัน 2 อันจึงถูกตัดออกไปเลย, distinct = การแสดงข้อมูลโดยไม่ซ้ำกัน

สำหรับประโยชน์ของมันนะคับ คือ ใช้ในการกำจัด elements ที่ซ้ำกันออกไป และถ้าเราต้องการที่จะ index ได้ ก็เพียงแต่แปลงให้เป็น list อีกครั้ง เช่น

แสดงขั้นตอนแปลงเป็น set() และ list()

39. isinstance()

เป็นการเช็คว่าเป็น type หรือ class นั้นๆรึป่าว

argument ข้างในมี 2 ตัว คือ obj, กับ type

ตัวอย่างนะ เป็นการเช็คว่า object นี้เป็นมาจาก class หรือ type นี้รึป่าว

จริงๆแล้ว เราสามารถรับเป็น list หรือ values อะไรก็ได้นะ ไม่จำเป็นต้องเป็น object ของ class เสมอไป เพราะใน Python ทุกอย่างคือ object หมดเลย

เช็คว่าเป็น type Student ไหม

Used case:

คือ เราสามารถ apply เพื่อใช้เช็คตอนรับ boolean เข้ามา บางครั้งถ้าส่งเป็น string อาจทำให้ logic ผิดพลาดได้ จึงต้องดักเอาไว้ก่อน

Ex1: ดัก request ไม่ว่าจะส่งมาเป็น true หรือ “true” ก็ยัง logic ถูกต้องEx2: อีกตัวอย่างของการเช็ค type ในการแก้โจทย์

สำหรับ

ปกติ A=65 นะ เราจะนำมาลบ 64 จะเริ่มต้นที่ค่า 1 (ทีนี้เราจะได้ A-Z ที่ได้ 1-26 โดยไม่ต้องเขียนยาวๆเลยนะ)

จาก student_code ที่มี Z อยู่ภายใน, function จะแปลงให้เป็น 26 เองเลย

student_code = '59Z10053211'

def convert_student_code_alphabet_to_number(student_code: str):

result = []  
for code in student_code:  
    if code.isdigit():  
        result.append(int(code))  
    else:  
        result.append(ord(code.upper()) - 64)  
return result
print(convert_student_code_alphabet_to_number(student_code=student_code))

output

[5, 9, 26, 1, 0, 0, 5, 3, 2, 1, 1] # z คือ 26 นะ

7 ของ

ปกติ A=65 นะ เราจะนำมาลบ 64 จะเริ่มต้นที่ค่า 1 (ทีนี้เราจะได้ A-Z ที่ได้ 1-26 โดยไม่ต้องเขียนยาวๆเลยนะ)

จาก student_code ที่มี Z อยู่ภายใน, function จะแปลงให้เป็น 26 เองเลย

student_code = '59Z10053211'

def convert_student_code_alphabet_to_number(student_code: str):

result = []  
for code in student_code:  
    if code.isdigit():  
        result.append(int(code))  
    else:  
        result.append(ord(code.upper()) - 64)  
return result
print(convert_student_code_alphabet_to_number(student_code=student_code))

output

[5, 9, 26, 1, 0, 0, 5, 3, 2, 1, 1] # z คือ 26 นะ

8คือ มันเช็คได้แค่

ปกติ A=65 นะ เราจะนำมาลบ 64 จะเริ่มต้นที่ค่า 1 (ทีนี้เราจะได้ A-Z ที่ได้ 1-26 โดยไม่ต้องเขียนยาวๆเลยนะ)

จาก student_code ที่มี Z อยู่ภายใน, function จะแปลงให้เป็น 26 เองเลย

student_code = '59Z10053211'

def convert_student_code_alphabet_to_number(student_code: str):

result = []  
for code in student_code:  
    if code.isdigit():  
        result.append(int(code))  
    else:  
        result.append(ord(code.upper()) - 64)  
return result
print(convert_student_code_alphabet_to_number(student_code=student_code))

output

[5, 9, 26, 1, 0, 0, 5, 3, 2, 1, 1] # z คือ 26 นะ

9 ไง จะใช้

> chr(97) 'a'
chr(65) 'A'
chr(66) 'B'

0 ที่เรา

> chr(97) 'a'
chr(65) 'A'
chr(66) 'B'

1 ขึ้นมาเองไม่ได้ ถ้าเราต้องการให้ได้ ต้องทำแบบนี้นะ

  • Ex3: นี้เป็นตัวอย่างของการให้เช็คว่าเป็น
    > chr(97) 'a'
    chr(65) 'A'
    chr(66) 'B' 2 ของ chr(97) 'a' chr(65) 'A' chr(66) 'B' 3 รึปาว

if not any([student_registration_code, credit_bank_registration_code]):

raise ValidationError({'detail': 'Student code is required.'})

ถ้าไม่มีค่าเข้ามาเลยทั้ง 2 ตัวแปรใน any() ก็จะทำใน raise นะ

8

วิธีใช้นะ

  • เช็คว่า
    > chr(97) 'a'
    chr(65) 'A'
    chr(66) 'B' 4 เป็น chr(97) 'a' chr(65) 'A' chr(66) 'B' 5 ของ chr(97) 'a' chr(65) 'A' chr(66) 'B' 6 ป่าว ถ้าใช่ก็ค่อยไปดึงแต่ละ index ออกมา

if not any([student_registration_code, credit_bank_registration_code]):

raise ValidationError({'detail': 'Student code is required.'})

ถ้าไม่มีค่าเข้ามาเลยทั้ง 2 ตัวแปรใน any() ก็จะทำใน raise นะ

9

40. rstrip()

สำหรับเคสนี้มันคือ

> chr(97) 'a'
chr(65) 'A'
chr(66) 'B'

7 คือ จะตัวด้านขวาออก โดยระบุสิ่งที่ต้องการ

> chr(97) 'a'
chr(65) 'A'
chr(66) 'B'

8 ด้านในได้เลย มาดู

> chr(97) 'a'
chr(65) 'A'
chr(66) 'B'

9 กันเลยดีกว่านะ