Builtin FunctionPython
https://docs.python.org/3/library/functions.html
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
การนำเข้า 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
การนำเข้า 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 *
แต่รูปแบบนี้ไม่แนะนำให้ใช้
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/
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
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(start, stop, step)
random.randint(start, stop)
Exercise
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
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)
Exercise lambda function
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
Python map Functions
map()
map(function, listobject)
my_ingredient = ['หมู', 'ไก่','ปลา','กุ้ง']
def sausage(ingredient): -> ฟังก์ชันทำไส้กรอก
my_food = map(sausage, my_ingredient)
** result จากการ map จะเป็น map object ซึ่งสามารถใช้วน loop ได้ / เปลี่ยนเป็น list ได้
Python map exercise
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 ได้
Python filter exercise
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
Python all any Functions
all() กับ any()
all(listobject) --> ตรวจสอบว่าทุกสมาชิกมีค่าความจริงเป็น True หรือ เทียบเท่า
all(listobject) --> ตรวจสอบว่ามีอย่างน้อย 1 สมาชิกมีค่าความจริงเป็น True
โดยสามารถใช้ร่วมกับ map หรือ filter ก็ได้
เช่นตรวจสอบว่าใน list มีเฉพาะจำนวนคู่เท่านั้น all(map(is_even, my_number))
การบ้าน