Upload
ngoliem
View
264
Download
4
Embed Size (px)
Citation preview
Computer Science, CMU
Python - Functions
CS101
CS101 Introduction to computer
Computer Science, CMU
ถงตอนน การเขยนโคดของเรามลกษณะเปนล าดบของค าสงเรยงกนอยในไฟลเดยว เปรยบเทยบกบการเดนทาง เหมอนเราออกเดนทางจากจดเรมตนตรงดงไปยงจดหมายปลายทางในการเขยนโปรแกรม การเรยกใชฟงกชนจะเหมอนการแวะระหวางทาง
เชนเราอาจจะแวะเตมน ามน แวะกนขาว แวะเยยมญาตและเมอเราท าภารกจเหลานนเสรจเรากจะกลบเขาสทางหลก ณ จดทเราออกจากทางหลกไป
Motivation
CS101 Introduction to computer
Computer Science, CMU
ฟงกชนคอล าดบของค าสงทมการตงชอไวส าหรบเรยกใชค านวณ (a named sequence of statements)
ฟงกชนมการรบอนพทเขาไป และ(อาจ)สงผลลพธกลบมา
Example
We call a function len( ) which takes one argument and returns a result. The result is called the return value.
What is a function
>>> a = len(“Python 3”)>>> print(a)>>> 8
CS101 Introduction to computer
Computer Science, CMU
Abstractionในการเรยกใชฟงกชนใดๆเราตองรเพยงวา• ฟงกชนนนมชอเรยกวาอะไร• ฟงกชนนนมหนาทอะไร• ฟงกชนนนตองการ arguments อะไรบาง• ฟงกชนนนจะสงผลลพธอะไรกลบมา
เหมอนกบการขบรถยนต คณไมจ าเปนตองรวาเครองยนต ระบบขบเคลอน หรอระบบชวงลางท างานอยางไร ถาสงทคณตองการคอใชงานรถแนนอน วาหากเราตองการแตงรถ ปรบปรง เปลยนแปลง หรอสรางรถยหอของตวเอง เราจ าเปนจะตองรการท างานของระบบตางๆเหลานน (เราจะเรยนการสรางฟงกชนเองในบทตอๆไป)
Why use functions? (1/2)
CS101 Introduction to computer
Computer Science, CMU
Reusabilityเรยกใชการค านวณเดมๆหลายครงในโปรแกรมเดยว
เรยกใชการค านวณเดมๆหลายครงในหลายๆโปรแกรม
Easy maintenance, Readability
Why use functions? (2/2)
CS101 Introduction to computer
Computer Science, CMU
Built-inกลมฟงกชนทมากบ Python สามารถเรยกใชไดเลยSee a list of the functions next page.
User definedฟงกชนทผใชสรางขนเองส าหรบงานทเฉพาะเจาะจงมากขน
ในบทนเราจะเรยนรการเรยกใชฟงกชนแบบ built-in กอนPay attention to!• Function specification: ฟงกชนนนตองการ argument กตว และมล าดบการสง argument อยางไร
• Return value: เมอฟงกชนท างานเสรจสนจะสงผลลพธอะไรกลบมา
Functions in Python
CS101 Introduction to computer
Computer Science, CMU
Python built-in functions
CS101 Introduction to computer
Computer Science, CMU
General functionสามารถกระท ากบตวแปรหลายๆแบบได ยกตวอยางเชน len()
len() สามารถหาความยาวของ string, หรอ list ได
มการเรยกใชแบบ function ทางคณตศาสตร len(argument)
Object-type specific functionมความจ าเพาะกบตวแปรนนๆ เชน ฟงกชนท าเปน uppercase จะใชกบ string เทานน int หรอ float ท าไมได
มการเรยกใชตางออกไป โดยการใชเครองหมาย dot ตามชอตวแปร
เชน “mystring”.upper() จะไดผลลพธเปน MYSTRING
Two types of built-in function
CS101 Introduction to computer
Computer Science, CMU
ก าหนด input requirements ของฟงกชนThe requirements are:• จ านวน argument ทตองสงใหฟงกชน
• ล าดบของ argument ทตองสงใหฟงกชน
len() ตองการ 1 argument. จากนนมนจะค านวณความยาวของ argument น แลวสงผลลพธกลบมา
Function specification
>>> len(“Python 3”)8
CS101 Introduction to computer
Computer Science, CMU
เราสามารถเปดดค าแนะน าการใชงานฟงกชนเบองตนโดยเรยกใชฟงกชน help()
Getting some help
CS101 Introduction to computer
Computer Science, CMU
ฟงกชนทมการสงผลลพธกลบ (สามารถ assign คาผลลพธใหกบตวแปรได) เรยกวา fruitful functions
ฟงกชนทไมมการสงผลลพธกลบ เรยกวา void functions
ฟงกชนบางตวอาจไม return คา
>>> a = print(‘b’)b>>> print(a)None
>>> a = log(2)>>> print(a)>>> 0.6931471805599453
CS101 Introduction to computer
Computer Science, CMU
โมดล คอไฟล .py ทเกบค าสงและนยามเพอการเรยกใช
โดยปกต โมดล เปนเสมอนกลองทเกบนยามคาคงทตางๆ Constants
ฟงกชนตางๆ
ExampleA ‘math’ module contains a set of mathematical functions.• exp(), log(), ….
It also defines some useful constants in mathematics. • 𝜋
Module - Definition
CS101 Introduction to computer
Computer Science, CMU
เราสามารถเรยกใชฟงกชนใน Module โดยใช
from module_name import function_name
หากเราอยาก Import ทกๆฟงกชนในโมดลนนใหใชเครองหมาย *
Using a module [1/2]
>>> from math import log>>> log(2)
>>> from math import *
CS101 Introduction to computer
Computer Science, CMU
หากตองการ Import และเปลยนชอ function จากโมดลเปนชอทเราตองการ สามารถใช keyword as
Using a module [2/2]
>>> from math import log as mylog>>> mylog(2)
CS101 Introduction to computer
Computer Science, CMU
Storingเรยกฟงกชนแลวเกบผลลพธทไดไวในตวแปรกอน
Example
On-the-flyไมเกบไวในตวแปร แตน ามาใชเลยทนท
Example
Making use of the return values
CS101 Introduction to computer
Computer Science, CMU
เราสามารถเรยกฟงกชนซอนกนไดดวย
Computing
𝑥 = log(10)
Functions composition
CS101 Introduction to computer
Computer Science, CMU
Inputการใช input() function
Outputการใช string modulo
Useful Functions
CS101 Introduction to computer
Computer Science, CMU
ฟงกชน print() ตองการ argument 3 ตว คอ ลสตของสงทตองการจะแสดงผล
เครองหมายทเอาไวข นระหวางการแสดงผล (default คอไมข นอะไร)
เครองหมายทเอาไวปดทายการแสดงผล (default คอการขนบรรทดใหม)
Function specification
Example
Output using print
>>> print(“Python 3”)Python 3>>> print(1,4,5, sep=‘-’, end=‘\t’)1-4-5 >>>
print(*objects, sep=' ', end='\n')
CS101 Introduction to computer
Computer Science, CMU
หากสงทเราตองการแสดงผลเปน string เราสามารถใชเครองหมาย modulo (%) มาสราง string ทซบซอนขนได
โดยการ แทรก placeholder ไวใน string ตามดวย เครองหมาย modulo และตามดวย คาทจะน ามาเตมใน placeholder
คาทจะน ามาเตมจะอยภายใตวงเลบ และม คอมมาขนไว
String modulo
CS101 Introduction to computer
Computer Science, CMU
Type Meaning
%d Signed integer decimal.
%f Floating point decimal format.
%a.bf Floating point decimal format with precision marker
%c Single character
Common placeholder types
CS101 Introduction to computer
a คอความยาวทงหมดทอยากจะใชงาน- หากตวเลข ยาวกวา a ตว Python กยงจะพมพทงหมด - หากตวเลขยาวนอยกวา a ตว Python จะพมพ space
แตจดทศนยม, b, จะตองเปนไปตามขอก าหนดเทานน คอ ตามความละเอยดทก าหนด ในทนคอสองจด
Computer Science, CMU
Examples
CS101 Introduction to computer
Computer Science, CMU
String modulo on-the-fly
CS101 Introduction to computer
Computer Science, CMU
Using format() : a python way
CS101 Introduction to computer
เปลยนจาก % เปน {x: } เลข x แสดง ล าดบของ argument ทจะเอาคามาแสดงเชน เดมเปน %5d เปลยนเปน {0:5d}
Computer Science, CMU
Examples of using format()>>> "First argument: {0}, second one: {1}".format(47,11)
'First argument: 47, second one: 11'
>>> "Second argument: {1}, first one: {0}".format(47,11)
'Second argument: 11, first one: 47'
>>> "Second argument: {1:3d}, first one: {0:7.2f}".format(47.42,11)
'Second argument: 11, first one: 47.42'
>>> "First argument: {}, second one: {}".format(47,11)
'First argument: 47, second one: 11‘
>>> # arguments can be used more than once: ...
>>> "various precisions: {0:6.2f} or {0:6.3f}".format(1.4148)
'various precisions: 1.41 or 1.415'
CS101 Introduction to computer
Computer Science, CMU
Can be done using input() function.
You’ve already mastered it.
Example
Beware of converting String to Int or Float
Getting inputs using input()
CS101 Introduction to computer