Upload
trinhdieu
View
233
Download
0
Embed Size (px)
Citation preview
Computer Science, CMU
ภาษาไพทอน (Python language)
และการเขยนโปรแกรมเบองตน
204101 Introduction to Computer 1
1. แนะน าเบองตนกบภาษาไพทอน (Introduction)2. ตวแปร (Variable)3. การก าหนดคาใหตวแปร (Assignment statement)4. ชนดขอมล (Data type)5. ค าสงรบและแสดงผล (Input and output statement)6. ปฏบตการ
Computer Science, CMU
1. Introduction
1.1 ประวตความเปนมา1.2 คณลกษณะของภาษาไพทอน1.3 First program
204101 Introduction to Computer 2
Computer Science, CMU
1.1 ประวตความเปนมา
ในป ค.ศ. 1989 ภาษาไพทอนถกพฒนาโดย Guido van Rossumนกวจย แหงสถาบน วจยแหงชาตทางดานคณตศาสตรและวทยาการคอมพวเตอร เมองอมสเตอรดม ประเทศเนเธอรแลนด
ภาษาไพทอนเปนภาษาทน าลกษณะทดของภาษาทมอยเดม (ABC, Modula-3, C, C++, Algol-68, SmallTalk and Unix shell and other scripting languages) และเพมคณลกษณะทดเชน คลาสและอนๆ รวมถงม interface ใหเขยนโปรแกรมไดสะดวก
204101 Introduction to Computer 3
Computer Science, CMU
1.2 คณลกษณะของภาษาไพทอน
ภาษาไพทอนเปนภาษาระดบสง มคณลกษณะ ดงน
1.เปนภาษาทจดอยในกลม Interpreter
คอแปลแลวท างานทละค าสง มการประมวลผลทนท (process at runtime)
2.มลกษณะ interactive คอ เราสามารถพมพค าสง ท างานในลกษณะinteract คอโตตอบได
3.เปนภาษาทไดรบความนยม เรยนรไดงาย เหมาะกบผเรมตนเขยนโปรแกรม
204101 Introduction to Computer 4
Computer Science, CMU
1.3 first program
1 # first program2 # you can do it3 print(“Welcome to Python!”) # print line of text
อธบายบรรทด 1 comment ภาษาไพทอน สญลกษณ # คอ commentบรรทด 2 comment comment ไดทละ 1 บรรทด
จะใชเครองหมาย # น าหนาขอความทตองการ comment ไปจนจบบรรทดนนบรรทด 3 ค าสง print () คอฟงกชนเพอแสดงผลขอความทจอภาพ ขอความอยใน
เครองหมายค าพด ” ”
ผลลพธทไดจากโปรแกรมนคอขอความ Welcome to Python! ปรากฏทจอภาพ
204101 Introduction to Computer 5
Computer Science, CMU
ภาษาไพทอน execute ได 2 mode คอ
(1) INTERACTIVE MODE PROGRAMMING: เปน mode ทเราพมพค าสง ภาษาไพทอนจะแปลและท างานทนท เชน
(2) SCRIPT MODE PROGRAMMING:
พมพค าสงหรอโปรแกรมท editor หรอ IDLE ของไพทอน (write a simple Pythonprogram in a script) จากนน save file และก าหนด file type เปน .py
เมอรนโปรแกรมจะไดผลลพธ เชน
source code in a welcome.py file
print “Welcome to Python!”
This will produce the following result:
Welcome to Python!
การท างานของภาษา python
204101 Introduction to Computer 6
Computer Science, CMU
ตวแปรจะจองเนอทในหนวยความจ า ขนาดหรอเนอททใช ขนกบชนดขอมล
ขอก าหนดในการตงชอตวแปร
1. อกษรตวแรกตองเปนตวอกษร (A-Z หรอ a-z) หรอ เครองหมาย _ (Underscore) เทานน
2. อกษรตวอนๆ ตองเปนตวอกษร (A-Z หรอ a-z) หรอ ตวเลข 0-9 หรอเครองหมาย _ เทานน
3. หามตงชอตวแปรซ ากบค าสงวน (Reserved Word)
4. หามมชองวางภายในชอ
5. ตวอกษรตวพมพเลกและตวพมพใหญถอวาตางกน
เชน NUM , Num และ num เปนตวแปรคนละตวกน
2. ตวแปร (variable)
204101 Introduction to Computer 7
Computer Science, CMU
These reserved words may not be used as constant or variable or any other identifier names. All the Python keywords contain lowercase letters only.
and exec not
assert finally or
break for pass
class from print
continue global raise
def if return
del import try
elif in while
else is with
except lambda yield
Reserved words
204101 Introduction to Computer 8
Computer Science, CMU
123MyID ผด ขนตนดวยตวเลข
_ThinkBig ถกตอง
mylife@CMU ผด หามใชเครองหมาย @
Pin number ผด หามเวนวรรค
Dorm_number ถกตอง
ตวอยางการตงชอตวแปร
204101 Introduction to Computer 9
Computer Science, CMU
ตวแปรในภาษาไพทอนเราไมตองประกาศชนดขอมลใหตวแปรกอนท างานการประกาศจะเกดขนโดยอตโนมตเมอเราก าหนดคาใหตวแปร โดยใชเครองหมาย =เชน
score = 75gpa = 2.85
หมายความวา ตวแปร score มเปนจ านวนเตม มคา 75ตวแปร gpa มเปนจ านวนจรง มคา 2.85
204101 Introduction to Computer 10
3. การก าหนดคาใหตวแปร (assignment statement)
Computer Science, CMU
นอกจากน เราสามารถก าหนดคาในลกษณะ multiple assignment ได ดง
ตวอยางท 1
a = b = c = 1
ค าอธบาย
ตวแปร a b c เปนจ านวนเตมมคา 1
ตวอยางท 2
a, b, c = 1, 2, "john" ค าอธบาย
ตวแปร a เกบขอมลจ านวนเตมทมคาเทากบ 1
bเกบขอมลจ านวนเตมทมคาเทากบ 2
c เกบขอมลทเปนขอความ john
ตวอยางการก าหนดคา
204101 Introduction to Computer 11
Computer Science, CMU
ชนดขอมล ส าหรบการเขยนโปรแกรมเบองตนทควรรจกมดงน
(1) int จ านวนเตม
(2) float จ านวนจรงหรอทศนยม
(3) boolean บลน : True, False
(4) string สตรงหรอขอความ
(5) list ลสต
4.ชนดขอมล (Data type)
204101 Introduction to Computer 12
Computer Science, CMU
(1) int เลขจ านวนเตม เชน 2 , 50 , 1009
(2) float เลขจ านวนจรงหรอ ทศนยม เชน 15.20 ,-21.9
Data type : int float
204101 Introduction to Computer 13
Computer Science, CMU
บลน (Boolean) คอคา จรง , เทจ ในภาษาไพทอน ใชค าวา True False
ในการเปรยบเทยบ จะไดผลลพธเปนบลน เชน
การเปรยบเทยบ 4 > 1 เปนจรง ผลทไดคอบลน True
6 < 7 เปนเทจผลคอไดบลน False
เมอเขยนโปรแกรม เราจะใชผลการเปรยบเทยบ เพอตรวจสอบเงอนไขในการ
ท างาน
Data type : (3) บลน
204101 Introduction to Computer 14
Computer Science, CMU
สตรงหรอขอความคอตวอกษรทเรยงตอกนในเครองหมายค าพด ภาษาไพทอนใชไดทง ' (single) " (double) ''' or """ (triple)
ตวอยางสตรงword = 'word'sentence = "This is a sentence."
นอกจากนยงสามารถใช triple (''' or """') เพอก าหนดขอความทมหลายบรรทดได เชน
paragraph = """This is a paragraph. It ismade up of multiple lines and sentences."""
นอกจากนในการท างานกบสตรงเราสามารถใช ตวด าเนนการ + * (มตวอยาง)- เครองหมาย + น าสตรงหรอขอความมาตอกน- เครองหมาย * ท าซ า
Data type : (4) string
204101 Introduction to Computer 15
Computer Science, CMU
- ซบสตรง (substring) หรอขอความยอยในสตรง- เราใชเครองหมาย [ ] หรอ [ : ] เพอท างานกบขอความยอย- ต าแหนงการจดเกบอกษรแตละตว เรมท 0 จบท end-1
การจดเกบ สตรง อธบายไดดงน- ตวอยางสตรงเกบค าวา Hello World!
จากตวอยางขอความ Hello world! มทงหมด 12 ตวอกษร ต าแหนงการจดเกบตวอกษร เรมท 0 เกบตวอกษร Hตวสดทายอยทต าแหนง 11 คอend-1 = 12-1 เกบตวอกษร !
Data type : (4) string
204101 Introduction to Computer 16
Computer Science, CMU
ตวอยางการท างานกบสตรง
204101 Introduction to Computer 17
str = 'Hello World!'
print (str) # Prints complete string
print (str[0]) # Prints first character of the string
print (str[2:5]) # Prints characters starting from 3rd to 5th (end-1 คอ 5-1 = 4)
print (str[2:]) # Prints string starting from 3rd character
print (str * 2) # Prints string two times
print (str + "TEST ") # Prints concatenated string
This will produce the following result:
Hello World!
H
llo
llo World!
Hello World!Hello World!
Hello World!TEST
Computer Science, CMU
- list เปนชนดขอมล ม item หรอสมาชก อยใน list
- item มชนดขอมลทตางกนได (compound data type)
- item อยในเครองหมาย [ ]
- แตละ item แยกกนดวยเครองหมาย , (comma)
- คาทเกบใน list เรยกมาใชงานดวยเครองหมาย [ ] [ : ]
- item หรอสมาชก เรมท 0 จบท end-1 อธบายดงน
item หรอสมาชก ใน list เรยงตอเนองกน คอ ตวแรกเรมท 0 ตวสดทายอยท end-1
ตวอยาง myList = [‘Hello’ , ’my’ , ‘student’] การจดเกบแสดงไดดงน
Data type : (5) ลสต (list)
204101 Introduction to Computer 18
จากตวอยางจ านวน item หรอขนาด ของ list คอ 3item เร มตนอยทต าแหนง 0item สดทายอยทต าแหนง 2 (คอ end-1นนคอ 3-1=2)
Computer Science, CMU
exlist1 = ['abcd', 786 , 2.23, 'john', 70.2 ]
exlist2 = ['Hello' ,'my' , 'student']
print (exlist1) # Prints complete list
print (exlist1[0]) # Prints first element of the list
print (exlist1[1:3]) # Prints elements starting from 2nd till 3rd (end-1 คอ 3-1=2)
print (exlist1[2:]) # Prints elements starting from 3rd element
print (exlist2 * 2) # Prints list two times
print (exlist1 + exlist2) # Prints concatenated lists
This will produce the following result:
['abcd', 786, 2.23, 'john', 70.2]
abcd
[786, 2.23]
[2.23, 'john', 70.2]
['Hello', 'my', 'student', 'Hello', 'my', 'student']
['abcd', 786, 2.23, 'john', 70.2, 'Hello', 'my', 'student']
ตวอยางการท างานกบ ลสต
204101 Introduction to Computer 19
Computer Science, CMU
5.ตวด าเนนการ (Operator)
• ตวด าเนนการทางคณตศาสตร (Arithmetic Operators)
• ตวด าเนนการเปรยบเทยบ (Comparison (i.e., Relational) Operators)
• ตวด าเนนการก าหนดคา (Assignment Operators)
• ตวด าเนนการตรรกะ (Logical Operators)
• Bitwise Operators
• Membership Operators
• Identity Operators
204101 Introduction to Computer 20
Computer Science, CMU
ตวด าเนนการทางคณตศาสตร (Arithmetic Operators)
204101 Introduction to Computer 21
Operator Description Example Result
+ Addition a + b 30
- Subtraction a - b -10
* Multiplication a * b 200
/ Division b / a 2
% Modulus b % a 0
** Exponent 2**3 8
/ / Floor Division 9//2 4
9.0//2.0 4.0
Assume variable a holds 10 and variable b holds 20, then:
7 / 3 # classic division returns a float 2.3333333333333335 17 // 3 # floor division discards the fractional part 5
Computer Science, CMU
ตวด าเนนการเปรยบเทยบ (Comparison Operators)
204101 Introduction to Computer 22
Operator Description Example result
== equal a == b False
!= not equal a != b True
> greater than a > b False
< less than a < b True
>= greater than or equal a >= b False
<= less than or equal a <= b True
Assume variable a holds 10 and variable b holds 20, then:
Computer Science, CMU
Operator Example Explanation
= c = a + b
+= c += a c = c + a
-= c -= a c = c - a
*= c *= a c = c * a
/= c /= a c = c / a
%= c %= a c = c % a
**= c **= a c = c ** a
//= c //= a c = c // a
ตวด าเนนการก าหนดคา (Assignment Operators)
204101 Introduction to Computer 23
Computer Science, CMU
ตวด าเนนการตรรกะ (Logical Operators)
204101 Introduction to Computer 24
Operator Example Result
and a and b True
or a or b True
not not(a and b) False
Assume variable a holds 10 and variable b holds 20, then:
ตารางคาความจรงP Q P and Q P or Q P Not P
True True True True True False
True False False True False True
False True False True
False False False False
Computer Science, CMU
นอกจากนยงม ตวด าเนนการอนๆ ซงในทนยงไมกลาวถงBitwise Operator ตวด าเนนการทางบต : >> , << , & , |
Python Membership Operators : in , not in
Python Identity Operators: is , is not
Operators
204101 Introduction to Computer 25
Computer Science, CMU
ล าดบความส าคญของเครองหมาย (Python Operators Precedence)
204101 Introduction to Computer 26
Operator Description
() parentheses
** Exponentiation (raise to the power)
~ + - Complement, unary plus and minus
* / % // Multiply, divide, modulo and floor division
+ - Addition and subtraction
>> << Right and left bitwise shift
& Bitwise 'AND'
^ | Bitwise exclusive `OR' and regular `OR'
<= < > >= Comparison operators
<> == != Equality operators
= %= /= //= -= += *= **= Assignment operators
is is not Identity operators
in not in Membership operators
not or and Logical operators
Computer Science, CMU
นพจน(expression)
เราอาจคนเคยกบรปแบบการเขยนสมการคณตศาสตร เชน xy - 5z แตเมอเขยนโปรแกรมเราตองเขยนเปนนพจนคอ รปแบบค าสงทภาษาเขาใจ
เชน สมการคณตศาสตรxy - 5z
x2 + 4y + 5
8y2 - 8z
เขยนเปนนพจนในภาษาไพทอน ไดดงนx * y – 9 * z
x**2 + 4 * y + 5
8 * y**2 - 8 * z
204101 Introduction to Computer 27
Computer Science, CMU 204101 Introduction to Computer 28
ตวอยางล าดบความส าคญของตวด าเนนการ (ตอ)
ตวอยาง 1 จงหาผลลพธทไดจาก นพจน c * d - x / y
ก าหนดใหตวแปรแตละตวมคาดงนx = 16
y = 4
c = 2.5
d = 0.25
วธคดc * d - x / y
แทนคา 2.5 * 0.25 - 16 / 4
0.625 – 4
-3.375 ตอบ
Computer Science, CMU 204101 Introduction to Computer 29
ตวอยางล าดบความส าคญของตวด าเนนการ (ตอ)ตวอยาง 2 จงหาผลลพธทไดจาก นพจน -(-5) * (x % y – x // (y+1))
ก าหนดใหตวแปรแตละตวมคาดงนx = 16
y = 4
c = 2.5
d = 0.25
วธคด
-(-5) * (x % y – x // (y+1))
แทนคา -(-5) * (16 % 4 – 16 // (4+1))
-(-5) * (16 % 4 – 16 // (5))
5 * (16 % 4 – 16 // 5)
5 * (0 – 3)
5 * (– 3)
-15 ตอบ
Computer Science, CMU 204101 Introduction to Computer
ตวอยางล าดบความส าคญของตวด าเนนการ (ตอ)
ตวอยาง 3 จงหาผลลพธทไดจาก นพจน c // d + y % x != 2*c – d
ก าหนดใหตวแปรแตละตวมคาดงนx = 16
y = 4
c = 2.5
d = 0.25
วธคด
c // d + y % x != 2*c – d
แทนคา
2.5 // 0.25 + 4 % 16 != 2*2.5 – 0.25
10 + 4 != 5 – 0.25
14 != 4.75
True ตอบ
30
Computer Science, CMU 204101 Introduction to Computer 31
6. ค าสงรบและแสดงผล
การท างานเพอตดตอกบผใช เราสามารถเรยกใชฟงกชนเพอรบ หรอแสดงผลลกษณะของฟงกชน คอ ค าสงหลายๆค าสง ทสรางไวแลว เราสามารถเรยกใชได แตตองเปนไปตามรปแบบทก าหนดในสวนน เพอรบ และแสดงผล เราจะศกษาดงน
ค าสงแสดงผล ใชฟงกชน print()ค าสงรบขอมล ใชฟงกชน input()
Computer Science, CMU 204101 Introduction to Computer 32
ค าสงแสดงผล print()
ฟงกชน print() แสดงผลทจอภาพ ในเครองหมาย ( ) จะสงขอมล : ตวแปร,ขอความ เพอแสดงผลทจอภาพ ขอมลแยกดวยเครองหมาย ,
ตวอยาง 1
>>> print ("Python is really a great language," , "isn't it? ")
Python is really a great language, isn't it?
>>> name='Wassana'
>>> ver=3.4
>>> print("Hello ! ", name, "Welcome to python", ver)
Hello ! Wassana Welcome to python 3.4
>>>
ตวอยาง 2
ตวอยาง 3
>>> print ("Welcome ")
Welcome
>>> print ("to python" )
to python
>>>
Computer Science, CMU
นอกจากน หากเราตองการแสดงผล การขนบรรทดใหม ใช \n
ตวอยางprint ("Welcome \nto \nPython")
ผลลพธ
สวนการแสดงผลในรปแบบอน เชน การแสดงจ านวนตวเลขทศนยม การจดรปแบบชองวางทสวยงาม จะกลาวถงในสวนทเกยวของตอไป
204101 Introduction to Computer 33
ค าสงแสดงผล print()
Computer Science, CMU 204101 Introduction to Computer 34
ค าสงรบขอมล input()ฟงกชน input() จะรอรบขอมลจากผใช ดงนนเราตองพมพขอมลเขาผานทางคยบอรด ปกตเราใสขอมล 1 ตว (เชน ขอความ หรอ ตวเลข) แลวเคาะenter
ขอมลทเราพมพนนจะเปน string (function return string)
เราตองสรางตวแปร เพอเกบขอมลดงกลาว
ตวอยางstr = input("Enter your input: ")
print ("Received input is : ", str)
สมมตผใชพมพขอความ Hello Python! ตวอยางการท างานและผลลพธ จะไดดงน
Enter your input: Hello Python
Received input is : Hello Python
จากตวอยาง ขอความ Hello Python ทเราพมพ จะถกเกบไวทตวแปร str
Now!!
Computer Science, CMU
ฟงกชน input จะรอใหผใชพมพขอมลจากคยบอรด ซงขอมลทรบมานนจะเปน สตรง น าไป
ค านวณไมไดถงแมเราจะพมพตวเลขกตาม หากเราตองการน าไปค านวณ เราตองแปลง
สตรงเปนตวเลขทตองการ เชน
แปลงจาก string เปน int หรอ แปลงจาก string เปน float
ตวอยาง
1 inp1=input("Input integer number : ")
2 no1=int(inp1)
3 inp2=input("Input float number : ")
4 no2=float(inp2)
204101 Introduction to Computer 35
ค าสงรบขอมล input() (cont.)
อธบายไดดงน บรรทดท 1 จะปรากฏขอความ Input integer number ทจอภาพ เคอรเซอรกระพรบ รอใหผใชพมพ
สมมตผใชพมพ 55 เคาะ enter ผลคอ string 55 จะถกเกบทตวแปร inp1บรรทดท 2 จะแปลง string 55 ซงเกบท ตวแปร inp1 ใหเปนตวเลขจ านวนเตมเกบไวทตวแปร no1
จากนเราสามารถน า no1 ไปค านวณได บรรทดท 3 และ 4 กเชนเดยวกนกบบรรทดท 1 และ 2 เพยงแตแปลง จาก string เปน float
Computer Science, CMU
ขอควรระวง เราตองทราบวาโปรแกรมเรามขอมลเขาอะไรบาง เพอทวาเราจะไดออกแบบ ออกแบบชนดขอมล และน าไปเขยนโปรแกรมไดถกตอง
เชน inp=input("Input number : ")no=int(inp)เมอโปรแกรมท างานเราใส 1.2 เกบทตวแปร string ok ไมมปญหาแตตอนแปลงใช int เกด error
204101 Introduction to Computer 36
ค าสงรบขอมล input() (cont.)
Computer Science, CMU
(1) เรยนรการใชโปรแกรม Python
(2) เรมตนเขยนโปรแกรมอยางงาย
(3) แบบฝกหด
6.ปฏบตการ
204101 Introduction to Computer 37
Computer Science, CMU
ภาษาไพทอน execute ได 2 mode คอ
(1.1) INTERACTIVE MODE PROGRAMMING: เปน mode ทเราพมพค าสง ภาษาไพทอนจะแปลและท างานทนท เชน
(1.2) SCRIPT MODE PROGRAMMING:
พมพค าสงหรอโปรแกรมท editor หรอ IDLE ของไพทอน (write a simple Python program in a script) จากนน save file และก าหนด file type เปน .py
เมอรนโปรแกรมจะไดผลลพธ เชน
source code in a welcome.py file
print “Welcome to Python!”
This will produce the following result:
Welcome to Python!
(1) เรยนรการใชโปรแกรม Python
204101 Introduction to Computer 38
Computer Science, CMU
ตวอยาง ใชค าสง print() แสดงผลทจอภาพ save file, run module
(2) เรมตนเขยนโปรแกรมอยางงาย
204101 Introduction to Computer 39
print ("Welcome ")
print ("to python" )
print ("Python is really a great language," , "isn't it? “)
name='Wassana'
ver=3.4
print ("Hello ! " , name , "Welcome to python“ , ver)
print ("Welcome \nto \nPython")
# first program
# you can do it
print(“Welcome to Python!”) # print line of text
Computer Science, CMU
ตวอยาง ใชค าสง input()
204101 Introduction to Computer 40
str = input("Enter your input: ")
print ("Received input is : ", str)
inp1=input("Input integer number : ")
no1=int(inp1)
inp2=input("Input float number : ")
no2=float(inp2)
ทตองระวงคอinp1=input("Input number : ")
no1=int(inp1)
เมอโปรแกรมท างานเราใส 1.2 เกบทตวแปร string ok ไมมปญหาแตตอนแปลงใช int เกด error
Computer Science, CMU
ตวอยาง
204101 Introduction to Computer 41
inp1=input("Input integer number : ")
no1=int(inp1)
inp2=input("Input float number : ")
no2=float(inp2)
print(‘result is = ‘ no1+no2)
จงเขยนโปรแกรมรบ ตวเลข 2 จ ำนวนเตม น ำตวเลขมำบวกกน และแสดงผลลพธทจอภำพ
save file ชอ pr01.py
run module
ดผลการท างาน
Computer Science, CMU
(3) แบบฝกหด ขอ 1
204101 Introduction to Computer 42
จงแสดงผลขอควำมทจอภำพ ตำมรปแบบทแสดงดำนลำง
Computer Science, CMU
แบบฝกหด ขอ 2
จงเขยนโปรแกรมรบ ชอ นำมสกล และอำย แลวแสดงผลทจอภำพ ตำมท
ก ำหนด โดยอำยทรบใหบวก อก 4 เพอบอกวำจะส ำเรจกำรศกษำเมออำย
เทำไร
เชน
Input name: Chalee
Input surname: Buddee
Input age: 18
ผลลพธ
My name is Chalee
And surname Buddee
Now I’m 18 years old.
I’ll finish undergraduate in 22 years old.
204101 Introduction to Computer 43