1 of 19

Builtin FunctionPython

https://docs.python.org/3/library/functions.html

2 of 19

Python Functions You Should Know

reduce() split() enumerate() eval() round()

max() min() map() getattr() append()

range() slice() format() strip() abs()

upper() lower() sorted() join() replace()

zip() lambda filter open ord

any all

https://www.makeuseof.com/python-built-in-functions-methods-you-should-know/

https://medium.com/pythoneers/10-must-known-built-in-functions-in-python-2f196b9c0359

3 of 19

การนำเข้า library

แบบที่ 1 นำเข้ามาทั้ง module

import ModuleName วิธีใช้ function

ModuleName.function1()

เช่น import math

x = math.factorial(15)

y = math.gcd(24, 40)

เพื่อความคล่องตัวเราสามารถ import เข้ามาแล้วเปลี่ยนชื่อโดยใช้ as ก็ได้ (เรียกว่าใช้ alias นามแฝง)

import math as m

x = m.factorial(15)

y = m.gcd(24, 40)

https://docs.python.org/3/library/math.html

4 of 19

การนำเข้า library

แบบที่ 2 นำเข้ามาเฉพาะบาง function

from ModuleName import function1

เช่น from math import factorial, gcd

x = factorial(15)

y = gcd(24, 40)

เพื่อความคล่องตัวเราสามารถ import เข้ามาแล้วเปลี่ยนชื่อโดยใช้ as ก็ได้ (เรียกว่าใช้ alias นามแฝง)

from math import factorial as fac, gcd as g

x = fac(15)

y = g(24, 40)

https://docs.python.org/3/library/math.html

เราสามารถนำเข้ามาทุกฟังก์ชันก็ได้

from ModuleName import *

แต่รูปแบบนี้ไม่แนะนำให้ใช้

5 of 19

Python Libraries and Modules Every Developer Should Know

Collections – Container datatypes

CSV – file Handling

Random – generation

Tkinter – GUI applications

Requests – HTTP requests

BeautifulSoup4 – web scraping

Numpy

Pandas

Matplotlib

Django

Flask

https://geekflare.com/popular-python-libraries-modules/

6 of 19

Python Random Libraries

Initialize the random number generator

Returns the current internal state of the random number generator

Restores the internal state of the random number generator

Returns a number representing the random bits

Returns a random number between the given range

Returns a random number between the given range

Returns a random element from the given sequence

Returns a list with a random selection from the given sequence

Takes a sequence and returns the sequence in a random order

Returns a given sample of a sequence

Returns a random float number between 0 and 1

Returns a random float number between two given parameters

Returns a random float number between two given parameters, you can also set a mode parameter to specify the midpoint between the two other parameters

https://www.w3schools.com/python/module_random.asp

7 of 19

Python Random Libraries

import random��random.seed(10)print(random.random()) # 0.5714025946899135

print(random.randrange(3, 9)) # 6

print(random.randint(3, 9))

mylist = ["apple", "banana", "cherry"]

random.shuffle(mylist)

print(mylist)

random.randrange(startstopstep)

random.randint(startstop)

8 of 19

Exercise

  1. ให้เขียนโปรแกรมเพื่อสร้างรหัสผ่านซึ่งประกอบด้วยตัวอักษร (ตัวใหญ่ + ตัวเล็ก) ตัวเลข และสัญลักษณ์ โดยให้ผู้ใช้เลือกว่าต้องการรหัสผ่านกี่ตัวอักษร แล้วแสดงรหัสผ่านที่สุ่มได้ (เพื่อความสะดวก ให้ใช้ string.ascii_letters, string.digits และ string.punctuation)
  2. ให้เขียนโปรแกรมเพื่อสุ่ม Code ในรูปแบบ XXXX-0000 (ตัวใหญ่ - ตัวเลข) แล้วแสดงผล
  3. ให้เขียนโปรแกรมหัวก้อย โดยให้ผู้ใช้กรอกตัวเลือก (H หรือ T) แล้วโปรแกรมสุ่มแล้วแจ้งผลว่าถูกหรือผิด
  4. ให้เขียนโปรแกรมกรรไกร ค้อน กระดาษ (S, H, P) โดยให้ผู้ใช้กรอกตัวเลือก (S, H หรือ P) แล้วโปรแกรมสุ่ม SHP แล้วแจ้งผลว่าแพ้ ชนะหรือเสมอ

9 of 19

lambda function

ในการสร้างฟังก์ชันที่มีการทำงานเพียง Statement เดียว หรือมีการใช้งานเพียงครั้งเดียว การสร้างฟังก์ชันโดยใช้ def จำนวนมาก ๆ จะทำให้โปรแกรมดูยุ่ง/วุ่นวายเกินไป

Python จึงมีการสร้างฟังก์ชันแบบพิเศษเรียกว่า lambda function มีรูปแบบดังนี้

lambda arguments : expression

def RectangleArea(width, height): RectangleArea = lambda w, h : w*h

return width * height

def CircleArea(radius): CircleArea = lambda r : 3.1415 * r**2

return 3.1415 * radius**2

10 of 19

lambda function

การใช้ lambda แบบ anonymous

RectangleArea = lambda w, h : w*h กำหนดชื่อฟังก์ชัน RectangleArea

area = RectangleArea(4, 7)

print(area)

area = (lambda w, h : w*h) (4 , 7 ) สร้าง lambda function แบบไม่กำหนดชื่อ

print(area)

11 of 19

Exercise lambda function

  1. สร้าง lambda function แล้วกำหนดชื่อ CubeVol ซึ่งจะคำนวณปริมาตรลูกบาศก์
  2. สร้าง lambda function แล้วกำหนดชื่อ CubeArea ซึ่งจะคำนวณพื้นที่ผิวลูกบาศก์
  3. สร้าง lambda function แบบไม่กำหนดชื่อเพื่อคำนวณความยาวของเส้นขอบทั้ง 12 ขอบของกล่องทรงสี่เหลี่ยมที่มีความกว้างยาวสูง w, l, h แล้วเรียกใช้งาน lambda function นี้

12 of 19

Python Functions You Should Know

reduce() split() enumerate() eval() round()

max() min() map() getattr() append()

range() slice() format() strip() abs()

upper() lower() sorted() join() replace()

zip() lambda filter() open ord

any() all()

https://www.makeuseof.com/python-built-in-functions-methods-you-should-know/

https://medium.com/pythoneers/10-must-known-built-in-functions-in-python-2f196b9c0359

13 of 19

Python map Functions

map()

map(function, listobject)

my_ingredient = ['หมู', 'ไก่','ปลา','กุ้ง']

def sausage(ingredient): -> ฟังก์ชันทำไส้กรอก

my_food = map(sausage, my_ingredient)

** result จากการ map จะเป็น map object ซึ่งสามารถใช้วน loop ได้ / เปลี่ยนเป็น list ได้

14 of 19

Python map exercise

  1. ให้สร้างฟังก์ชันยกกำลังสอง แล้ว map ฟังก์ชัน เพื่อยกกำลังสอง -> my_num = [1, 2, 3, 4,…, 10]
  2. ให้สร้างฟังก์ชันผัดกะเพรา xxx ไข่ดาวแล้ว map ฟังก์ชั่น my_ingredient = ['หมู', 'ไก่', 'ปลาหมึก', 'กุ้ง', 'หมูกรอบ'] เพื่อสร้างรายการอาหาร กะเพราหมูไข่ดาว ... แล้วแสดงผล
  3. ให้ใช้ map เพื่อแปลง list ของข้อความตัวเลข เป็น list ของตัวเลข (แปลงจาก ['23', '54', '13', '87'] --> [23, 54, 13, 87] )

15 of 19

Python map Functions

filter()

filter(function, listobject)

my_ingredient = ['หมูสามชั้น', 'อกไก่','ไข่ต้ม','กุ้ง','น้ำเปล่า', 'ชานมไข่มุก']

def suit_for_fat(ingredient): -> ฟังก์ชันตรวจเหมาะกับคนอ้วนหรือเปล่า

good_food = filter(suit_for_fat, my_ingredient)

--> good_food = ['อกไก่', ' ไข่ต้ม', 'น้ำเปล่า']

** result จากการ filter จะเป็น filter object ซึ่งสามารถใช้วน loop ได้ / เปลี่ยนเป็น list ได้

16 of 19

Python filter exercise

  1. ให้สร้าง list ของจำนวนเต็ม แล้วใช้ filter ฟังก์ชัน ร่วมกับ is_even เพื่อกรองเอาเฉพาะจำนวนคู่แล้วแสดงผล
  2. ให้สร้าง list ของจำนวนเต็ม แล้วใช้ filter ฟังก์ชัน ร่วมกับ is_prime เพื่อกรองเอาเฉพาะจำนวนที่เป็นจำนวนเฉพาะแล้วแสดงผล
  3. ให้สร้าง list ของรายการอาหาร ['กระเพราหมู', 'ไก่ทอด', 'ปลาหมึกผัดไข่เค็ม', 'กระเพรากุ้ง', 'ผัดผักบุ้งหมูกรอบ'] แล้วสร้างฟังก์ชัน is_not_pork รับรายการอาหารแล้วตรวจสอบว่าไม่มีหมูใช่ไหม แล้วใช้ filter เพื่อกรองเอาเฉพาะเมนูที่ไม่มีหมู (ใช้ "หมู" in dish เพื่อตรวจสอบว่ามีหมู)
  4. ทำซ้ำข้อ 1 แต่ให้ใช้ lambda function แทนฟังก์ชัน is_even

17 of 19

Python ค่าความจริงของฟังก์ชัน

The following values are considered false in Python:

None, False,

Zero of any numeric type. For example, 0, 0.0, 0j

Empty sequence. For example, (), [], ''.

Empty mapping. For example, {}

objects of Classes which has __bool__() or __len()__ method which returns 0 or False

All other values except these values are considered True.

https://www.programiz.com/python-programming/methods/built-in/bool

18 of 19

Python all any Functions

all() กับ any()

all(listobject) --> ตรวจสอบว่าทุกสมาชิกมีค่าความจริงเป็น True หรือ เทียบเท่า

all(listobject) --> ตรวจสอบว่ามีอย่างน้อย 1 สมาชิกมีค่าความจริงเป็น True

โดยสามารถใช้ร่วมกับ map หรือ filter ก็ได้

เช่นตรวจสอบว่าใน list มีเฉพาะจำนวนคู่เท่านั้น all(map(is_even, my_number))

19 of 19

การบ้าน

  1. ให้เขียนโปรแกรมหัวก้อย โดยให้ผู้ใช้กรอกตัวเลือก (H หรือ T) แล้วโปรแกรมสุ่มแล้วแจ้งผลว่าถูกหรือผิด
  2. ให้เขียนโปรแกรมกรรไกร ค้อน กระดาษ (S, H, P) โดยให้ผู้ใช้กรอกตัวเลือก (S, H หรือ P) แล้วโปรแกรมสุ่ม SHP แล้วแจ้งผลว่าแพ้ ชนะหรือเสมอ
  3. ให้เขียนโปรแกรมพนันหัวก้อย โดยกำหนดเงินเริ่มต้น 100 บาท แล้วให้ลงจำนวนเงินที่ต้องการเล่น แล้วทายหัวก้อย โดยเล่นไปเรื่อย ๆ จนกว่าเงินจะหมดหรือสะสมเงินทุนครบ 200 บาท
  4. ให้สร้างฟังก์ชันตัดเกรดเมื่อรับค่าคะแนน โดยถ้าคะแนนมากกว่า 80 ได้ G, คะแนนมากกว่า 40 ได้ P นอกนั้นได้ F แล้วรับค่าคะแนนทางแป้นพิมพ์ (คั่นด้วยลูกน้ำ) แล้วใช้ map เพื่อสร้าง list ที่เป็นเกรดของแต่ละคะแนนตามลำดับ
  5. จากข้อ 4 นับจำนวนผู้ได้เกรด G, P และ F แล้วแสดงผล