python - functions cs101 introduction to computer. computer science, cmu type meaning %d signed...
TRANSCRIPT
![Page 1: Python - Functions CS101 Introduction to computer. Computer Science, CMU Type Meaning %d Signed integer decimal. %f Floating point decimal format. %a.bf Floating point decimal format](https://reader035.vdocuments.net/reader035/viewer/2022062409/6051f0dd9ef692205947570b/html5/thumbnails/1.jpg)
Computer Science, CMU
Python - Functions
CS101
CS101 Introduction to computer
![Page 2: Python - Functions CS101 Introduction to computer. Computer Science, CMU Type Meaning %d Signed integer decimal. %f Floating point decimal format. %a.bf Floating point decimal format](https://reader035.vdocuments.net/reader035/viewer/2022062409/6051f0dd9ef692205947570b/html5/thumbnails/2.jpg)
Computer Science, CMU
ถงตอนน การเขยนโคดของเรามลกษณะเปนล าดบของค าสงเรยงกนอยในไฟลเดยว เปรยบเทยบกบการเดนทาง เหมอนเราออกเดนทางจากจดเรมตนตรงดงไปยงจดหมายปลายทางในการเขยนโปรแกรม การเรยกใชฟงกชนจะเหมอนการแวะระหวางทาง
เชนเราอาจจะแวะเตมน ามน แวะกนขาว แวะเยยมญาตและเมอเราท าภารกจเหลานนเสรจเรากจะกลบเขาสทางหลก ณ จดทเราออกจากทางหลกไป
Motivation
CS101 Introduction to computer
![Page 3: Python - Functions CS101 Introduction to computer. Computer Science, CMU Type Meaning %d Signed integer decimal. %f Floating point decimal format. %a.bf Floating point decimal format](https://reader035.vdocuments.net/reader035/viewer/2022062409/6051f0dd9ef692205947570b/html5/thumbnails/3.jpg)
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
![Page 4: Python - Functions CS101 Introduction to computer. Computer Science, CMU Type Meaning %d Signed integer decimal. %f Floating point decimal format. %a.bf Floating point decimal format](https://reader035.vdocuments.net/reader035/viewer/2022062409/6051f0dd9ef692205947570b/html5/thumbnails/4.jpg)
Computer Science, CMU
Abstractionในการเรยกใชฟงกชนใดๆเราตองรเพยงวา• ฟงกชนนนมชอเรยกวาอะไร• ฟงกชนนนมหนาทอะไร• ฟงกชนนนตองการ arguments อะไรบาง• ฟงกชนนนจะสงผลลพธอะไรกลบมา
เหมอนกบการขบรถยนต คณไมจ าเปนตองรวาเครองยนต ระบบขบเคลอน หรอระบบชวงลางท างานอยางไร ถาสงทคณตองการคอใชงานรถแนนอน วาหากเราตองการแตงรถ ปรบปรง เปลยนแปลง หรอสรางรถยหอของตวเอง เราจ าเปนจะตองรการท างานของระบบตางๆเหลานน (เราจะเรยนการสรางฟงกชนเองในบทตอๆไป)
Why use functions? (1/2)
CS101 Introduction to computer
![Page 5: Python - Functions CS101 Introduction to computer. Computer Science, CMU Type Meaning %d Signed integer decimal. %f Floating point decimal format. %a.bf Floating point decimal format](https://reader035.vdocuments.net/reader035/viewer/2022062409/6051f0dd9ef692205947570b/html5/thumbnails/5.jpg)
Computer Science, CMU
Reusabilityเรยกใชการค านวณเดมๆหลายครงในโปรแกรมเดยว
เรยกใชการค านวณเดมๆหลายครงในหลายๆโปรแกรม
Easy maintenance, Readability
Why use functions? (2/2)
CS101 Introduction to computer
![Page 6: Python - Functions CS101 Introduction to computer. Computer Science, CMU Type Meaning %d Signed integer decimal. %f Floating point decimal format. %a.bf Floating point decimal format](https://reader035.vdocuments.net/reader035/viewer/2022062409/6051f0dd9ef692205947570b/html5/thumbnails/6.jpg)
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
![Page 7: Python - Functions CS101 Introduction to computer. Computer Science, CMU Type Meaning %d Signed integer decimal. %f Floating point decimal format. %a.bf Floating point decimal format](https://reader035.vdocuments.net/reader035/viewer/2022062409/6051f0dd9ef692205947570b/html5/thumbnails/7.jpg)
Computer Science, CMU
Python built-in functions
CS101 Introduction to computer
![Page 8: Python - Functions CS101 Introduction to computer. Computer Science, CMU Type Meaning %d Signed integer decimal. %f Floating point decimal format. %a.bf Floating point decimal format](https://reader035.vdocuments.net/reader035/viewer/2022062409/6051f0dd9ef692205947570b/html5/thumbnails/8.jpg)
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
![Page 9: Python - Functions CS101 Introduction to computer. Computer Science, CMU Type Meaning %d Signed integer decimal. %f Floating point decimal format. %a.bf Floating point decimal format](https://reader035.vdocuments.net/reader035/viewer/2022062409/6051f0dd9ef692205947570b/html5/thumbnails/9.jpg)
Computer Science, CMU
ก าหนด input requirements ของฟงกชนThe requirements are:• จ านวน argument ทตองสงใหฟงกชน
• ล าดบของ argument ทตองสงใหฟงกชน
len() ตองการ 1 argument. จากนนมนจะค านวณความยาวของ argument น แลวสงผลลพธกลบมา
Function specification
>>> len(“Python 3”)8
CS101 Introduction to computer
![Page 10: Python - Functions CS101 Introduction to computer. Computer Science, CMU Type Meaning %d Signed integer decimal. %f Floating point decimal format. %a.bf Floating point decimal format](https://reader035.vdocuments.net/reader035/viewer/2022062409/6051f0dd9ef692205947570b/html5/thumbnails/10.jpg)
Computer Science, CMU
เราสามารถเปดดค าแนะน าการใชงานฟงกชนเบองตนโดยเรยกใชฟงกชน help()
Getting some help
CS101 Introduction to computer
![Page 11: Python - Functions CS101 Introduction to computer. Computer Science, CMU Type Meaning %d Signed integer decimal. %f Floating point decimal format. %a.bf Floating point decimal format](https://reader035.vdocuments.net/reader035/viewer/2022062409/6051f0dd9ef692205947570b/html5/thumbnails/11.jpg)
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
![Page 12: Python - Functions CS101 Introduction to computer. Computer Science, CMU Type Meaning %d Signed integer decimal. %f Floating point decimal format. %a.bf Floating point decimal format](https://reader035.vdocuments.net/reader035/viewer/2022062409/6051f0dd9ef692205947570b/html5/thumbnails/12.jpg)
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
![Page 13: Python - Functions CS101 Introduction to computer. Computer Science, CMU Type Meaning %d Signed integer decimal. %f Floating point decimal format. %a.bf Floating point decimal format](https://reader035.vdocuments.net/reader035/viewer/2022062409/6051f0dd9ef692205947570b/html5/thumbnails/13.jpg)
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
![Page 14: Python - Functions CS101 Introduction to computer. Computer Science, CMU Type Meaning %d Signed integer decimal. %f Floating point decimal format. %a.bf Floating point decimal format](https://reader035.vdocuments.net/reader035/viewer/2022062409/6051f0dd9ef692205947570b/html5/thumbnails/14.jpg)
Computer Science, CMU
หากตองการ Import และเปลยนชอ function จากโมดลเปนชอทเราตองการ สามารถใช keyword as
Using a module [2/2]
>>> from math import log as mylog>>> mylog(2)
CS101 Introduction to computer
![Page 15: Python - Functions CS101 Introduction to computer. Computer Science, CMU Type Meaning %d Signed integer decimal. %f Floating point decimal format. %a.bf Floating point decimal format](https://reader035.vdocuments.net/reader035/viewer/2022062409/6051f0dd9ef692205947570b/html5/thumbnails/15.jpg)
Computer Science, CMU
Storingเรยกฟงกชนแลวเกบผลลพธทไดไวในตวแปรกอน
Example
On-the-flyไมเกบไวในตวแปร แตน ามาใชเลยทนท
Example
Making use of the return values
CS101 Introduction to computer
![Page 16: Python - Functions CS101 Introduction to computer. Computer Science, CMU Type Meaning %d Signed integer decimal. %f Floating point decimal format. %a.bf Floating point decimal format](https://reader035.vdocuments.net/reader035/viewer/2022062409/6051f0dd9ef692205947570b/html5/thumbnails/16.jpg)
Computer Science, CMU
เราสามารถเรยกฟงกชนซอนกนไดดวย
Computing
𝑥 = log(10)
Functions composition
CS101 Introduction to computer
![Page 17: Python - Functions CS101 Introduction to computer. Computer Science, CMU Type Meaning %d Signed integer decimal. %f Floating point decimal format. %a.bf Floating point decimal format](https://reader035.vdocuments.net/reader035/viewer/2022062409/6051f0dd9ef692205947570b/html5/thumbnails/17.jpg)
Computer Science, CMU
Inputการใช input() function
Outputการใช string modulo
Useful Functions
CS101 Introduction to computer
![Page 18: Python - Functions CS101 Introduction to computer. Computer Science, CMU Type Meaning %d Signed integer decimal. %f Floating point decimal format. %a.bf Floating point decimal format](https://reader035.vdocuments.net/reader035/viewer/2022062409/6051f0dd9ef692205947570b/html5/thumbnails/18.jpg)
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
![Page 19: Python - Functions CS101 Introduction to computer. Computer Science, CMU Type Meaning %d Signed integer decimal. %f Floating point decimal format. %a.bf Floating point decimal format](https://reader035.vdocuments.net/reader035/viewer/2022062409/6051f0dd9ef692205947570b/html5/thumbnails/19.jpg)
Computer Science, CMU
หากสงทเราตองการแสดงผลเปน string เราสามารถใชเครองหมาย modulo (%) มาสราง string ทซบซอนขนได
โดยการ แทรก placeholder ไวใน string ตามดวย เครองหมาย modulo และตามดวย คาทจะน ามาเตมใน placeholder
คาทจะน ามาเตมจะอยภายใตวงเลบ และม คอมมาขนไว
String modulo
CS101 Introduction to computer
![Page 20: Python - Functions CS101 Introduction to computer. Computer Science, CMU Type Meaning %d Signed integer decimal. %f Floating point decimal format. %a.bf Floating point decimal format](https://reader035.vdocuments.net/reader035/viewer/2022062409/6051f0dd9ef692205947570b/html5/thumbnails/20.jpg)
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, จะตองเปนไปตามขอก าหนดเทานน คอ ตามความละเอยดทก าหนด ในทนคอสองจด
![Page 21: Python - Functions CS101 Introduction to computer. Computer Science, CMU Type Meaning %d Signed integer decimal. %f Floating point decimal format. %a.bf Floating point decimal format](https://reader035.vdocuments.net/reader035/viewer/2022062409/6051f0dd9ef692205947570b/html5/thumbnails/21.jpg)
Computer Science, CMU
Examples
CS101 Introduction to computer
![Page 22: Python - Functions CS101 Introduction to computer. Computer Science, CMU Type Meaning %d Signed integer decimal. %f Floating point decimal format. %a.bf Floating point decimal format](https://reader035.vdocuments.net/reader035/viewer/2022062409/6051f0dd9ef692205947570b/html5/thumbnails/22.jpg)
Computer Science, CMU
String modulo on-the-fly
CS101 Introduction to computer
![Page 23: Python - Functions CS101 Introduction to computer. Computer Science, CMU Type Meaning %d Signed integer decimal. %f Floating point decimal format. %a.bf Floating point decimal format](https://reader035.vdocuments.net/reader035/viewer/2022062409/6051f0dd9ef692205947570b/html5/thumbnails/23.jpg)
Computer Science, CMU
Using format() : a python way
CS101 Introduction to computer
เปลยนจาก % เปน {x: } เลข x แสดง ล าดบของ argument ทจะเอาคามาแสดงเชน เดมเปน %5d เปลยนเปน {0:5d}
![Page 24: Python - Functions CS101 Introduction to computer. Computer Science, CMU Type Meaning %d Signed integer decimal. %f Floating point decimal format. %a.bf Floating point decimal format](https://reader035.vdocuments.net/reader035/viewer/2022062409/6051f0dd9ef692205947570b/html5/thumbnails/24.jpg)
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
![Page 25: Python - Functions CS101 Introduction to computer. Computer Science, CMU Type Meaning %d Signed integer decimal. %f Floating point decimal format. %a.bf Floating point decimal format](https://reader035.vdocuments.net/reader035/viewer/2022062409/6051f0dd9ef692205947570b/html5/thumbnails/25.jpg)
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