
ภาษาPython
Presentation
•
Computers
•
6th - 8th Grade
•
Practice Problem
•
Hard
DAO Moon
FREE Resource
44 Slides • 0 Questions
1
Computer Science, CMU
ภาษาไพทอน (Python language)
และการเขียนโปรแกรมเบื้องต้น
204101 Introduction to Computer
1
1.
แนะนำเบื้องต้นกับภาษาไพทอน (Introduction)
2.
ตัวแปร (Variable)
3.
การก าหนดค่าให้ตัวแปร (Assignment statement)
4.
ชนิดข้อมูล (Data type)
5.
ตัวดำเนินการ (Operator)
6.
ค าสั่งรับและแสดงผล (Input and output statement)
7.
ปฏิบัติการ
2
Computer Science, CMU
1. Introduction
1.1 ประวัติความเป็นมา
1.2 คุณลักษณะของภาษาไพทอน
1.3 First program
204101 Introduction to Computer
2
3
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
4
Computer Science, CMU
1.2 คุณลักษณะของภาษาไพทอน
ภาษาไพทอนเป็นภาษาระดับสูง มีคุณลักษณะ ดังนี้
1.เป็นภาษาที่จัดอยู่ในกลุ่ม Interpreter
คือแปลแล้วท างานทีละค าสั่ง มีการประมวลผลทันที (process at runtime)
2.มีลักษณะ interactive คือ เราสามารถพิมพ์ค าสั่ง ท างานในลักษณะ
interact คือโต้ตอบได้
3.เป็นภาษาที่ได้รับความนิยม เรียนรู้ได้ง่าย เหมาะกับผู้เริ่มต้นเขียน
โปรแกรม
204101 Introduction to Computer
4
5
Computer Science, CMU
1.3 first program
1 # first program
2 # you can do it
3 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
6
Computer Science, CMU
ภาษาไพทอน execute ได ้ 2 mode คือ
(1) INTERACTIVE MODE PROGRAMMING: เป็น mode ที่เราพิมพ์ค าสั่ง ภาษาไพ
ทอนจะแปลและท างานทันที เช่น
(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!
การท างานของภาษา python
204101 Introduction to Computer
6
7
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
8
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
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
9
Computer Science, CMU
123MyID
ผิด ขึ้นต้นด้วยตัวเลข
_ThinkBig
ถูกต้อง
mylife@CMU
ผิด ห้ามใช้เครื่องหมาย @
Pin number
ผิด ห้ามเว้นวรรค
Dorm_number
ถูกต้อง
ตัวอย่างการตั้งชื่อตัวแปร
204101 Introduction to Computer
9
10
Computer Science, CMU
ตัวแปรในภาษาไพทอนเราไม่ต้องประกาศชนิดข้อมูลให้ตัวแปร
ก่อนท างานการประกาศจะเกิดขึ้นโดยอัตโนมัติเมื่อเราก าหนดค่า
ให้ตัวแปร โดยใช้เครื่องหมาย =
เช่น
score = 75
gpa = 2.85
หมายความว่า ตัวแปร score มีเป็นจ านวนเต็ม มีค่า 75
ตัวแปร gpa มีเป็นจ านวนจริง มีค่า 2.85
204101 Introduction to Computer
10
3. การก าหนดค่าให้ตัวแปร (assignment statement)
11
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
12
Computer Science, CMU
ชนิดข้อมูล ส าหรับการเขียนโปรแกรมเบื้องต้นที่ควรรู้จักมีดังนี้
(1) int
จ านวนเต็ม
(2) float
จ านวนจริงหรือทศนิยม
(3) boolean
บูลีน : True, False
(4) string
สตริงหรือข้อความ
(5) list
ลิสต์
4.ชนิดข้อมูล (Data type)
204101 Introduction to Computer
12
13
Computer Science, CMU
(1) int เลขจ านวนเต็ม เช่น 2 , 50 , 1009
(2) float เลขจ านวนจริงหรือ ทศนิยม เช่น 15.20 ,-21.9
Data type : int float
204101 Introduction to Computer
13
14
Computer Science, CMU
บูลีน (Boolean) คือค่า จริง , เท็จ ในภาษาไพทอน ใช้ค าว่า True False
ในการเปรียบเทียบ จะได้ผลลัพธ์เป็นบูลีน เช่น
การเปรียบเทียบ
4 > 1 เป็นจริง ผลที่ได้คือบูลีน True
6 > 7 เป็นเท็จ
ผลคือได้บูลีน False
เมื่อเขียนโปรแกรม เราจะใช้ผลการเปรียบเทียบ เพื่อตรวจสอบเงื่อนไขในการ
ท างาน
Data type : (3) บูลีน
204101 Introduction to Computer
14
15
Computer Science, CMU
สตริงหรือข ้อความคือตัวอักษรที่เรียงต่อกันในเครื่องหมายค าพูด ภาษาไพทอนใช ้ได ้ทั้ง
'
(single)
"
(double)
''' or """
(triple)
ตัวอย่างสตริง
word = 'word'
sentence = "This is a sentence."
นอกจากนี้ยังสามารถใช ้ triple (''' or """') เพื่อก าหนดข ้อความที่มีหลายบรรทัดได ้
เช่น
paragraph = """This is a paragraph. It is
made up of multiple lines and sentences."""
นอกจากนี้ในการท างานกับสตริงเราสามารถใช ้ ตัวด าเนินการ + * (มีตัวอย่าง)
-
เครื่องหมาย + น าสตริงหรือข ้อความมาต่อกัน
-
เครื่องหมาย * ท าซ ้า
Data type : (4) string
204101 Introduction to Computer
15
16
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
17
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 3rdto 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
18
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 คือ 3
item เริ่มต้นอยู่ที่ต าแหน่ง 0
item สุดท้ายอยู่ที่ต าแหน่ง 2 (คือ end-1นั่นคือ 3-
1=2)
19
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
20
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
21
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
22
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:
23
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
24
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
25
Computer Science, CMU
นอกจากนี้ยังมี ตัวด าเนินการอื่นๆ ซึ่งในที่นี้ยังไม่กล่าวถึง
Bitwise Operator ตัวด าเนินการทางบิต : >> , << , & , |
Python Membership Operators : in , not in
Python Identity Operators: is , is not
Operators
204101 Introduction to Computer
25
26
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
27
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
28
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 ตอบ
29
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 ตอบ
30
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
31
Computer Science, CMU
204101 Introduction to Computer
31
6. ค าสั่งรับและแสดงผล
การท างานเพื่อติดต่อกับผู้ใช้ เราสามารถเรียกใช้ฟังก์ชันเพื่อรับ หรือแสดงผล
ลักษณะของฟังก์ชัน คือ ค าสั่งหลายๆค าสั่ง ที่สร้างไว้แล้ว เราสามารถเรียกใช้ได้
แต่ต้องเป็นไปตามรูปแบบที่ก าหนด
ในส่วนนี้ เพื่อรับ และแสดงผล เราจะศึกษาดังนี้
ค าสั่งแสดงผล ใช้ฟังก์ชัน print()
ค าสั่งรับข้อมูล ใช้ฟังก์ชัน input()
32
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
>>>
33
Computer Science, CMU
นอกจากนี้ หากเราต้องการแสดงผล การขึ้นบรรทัดใหม่ ใช้ \n
ตัวอย่าง
print ("Welcome \nto \nPython")
ผลลัพธ์
ส่วนการแสดงผลในรูปแบบอื่น เช่น การแสดงจ านวนตัวเลขทศนิยม การจัดรูปแบบ
ช่องว่างที่สวยงาม จะกล่าวถึงในส่วนที่เกี่ยวข้องต่อไป
204101 Introduction to Computer
33
ค าสั่งแสดงผล print()
34
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!!
35
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
36
Computer Science, CMU
ข้อควรระวัง เราต้องทราบว่าโปรแกรมเรามีข้อมูลเข้าอะไรบ้าง
เพื่อที่ว่าเราจะได้ออกแบบ ออกแบบชนิดข้อมูล และน าไป
เขียนโปรแกรมได้ถูกต้อง
เช่น
inp=input("Input number : ")
no=int(inp)
เมื่อโปรแกรมท างาน
เราใส่ 1.2 เก็บที่ตัวแปร string ok ไม่มีปัญหา
แต่ตอนแปลงใช้ int เกิด error
204101 Introduction to Computer
36
ค าสั่งรับข้อมูล input() (cont.)
37
Computer Science, CMU
(1) เรียนรู้การใช้โปรแกรม Python
(2) เริ่มต้นเขียนโปรแกรมอย่างง่าย
(3) แบบฝึกหัด
7.ปฏิบัติการ
204101 Introduction to Computer
37
38
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
39
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
40
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
41
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
ดูผลการท างาน
42
Computer Science, CMU
(3) แบบฝึกหัด
ข้อ 1
204101 Introduction to Computer
42
จงแสดงผลข้อควำมที่จอภำพ ตำมรูปแบบที่แสดงด้ำนล่ำง
43
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
44
Computer Science, CMU
Reference
204101 Introduction to computer
44
Deitel ,”Python How to program” , ”Prentice-Hall,Inc.”, 2002.
Matt Telles , ”Python Power !” , “Thomson Course Teachnology”,2008.
python 3.4.3 help documentation
PYTHON TUTORIAL Simply Easy Learning by tutorialspoint.com
Computer Science, CMU
ภาษาไพทอน (Python language)
และการเขียนโปรแกรมเบื้องต้น
204101 Introduction to Computer
1
1.
แนะนำเบื้องต้นกับภาษาไพทอน (Introduction)
2.
ตัวแปร (Variable)
3.
การก าหนดค่าให้ตัวแปร (Assignment statement)
4.
ชนิดข้อมูล (Data type)
5.
ตัวดำเนินการ (Operator)
6.
ค าสั่งรับและแสดงผล (Input and output statement)
7.
ปฏิบัติการ
Show answer
Auto Play
Slide 1 / 44
SLIDE
Similar Resources on Wayground
42 questions
Spreadsheets: Creating charts REMEDIATION
Lesson
•
7th Grade
40 questions
อุปกรณ์เครือข่าย
Lesson
•
6th Grade
38 questions
Week 6 Install operating system and drivers for peripherals/devi
Lesson
•
7th - 9th Grade
39 questions
ระบบคอมพิวเตอร์ ม.2
Lesson
•
7th Grade
34 questions
Introduction to Microsoft Excel
Lesson
•
7th Grade
39 questions
Ch 7 Networks
Lesson
•
7th Grade
38 questions
PET - Conjunctions & Connectives
Lesson
•
6th - 8th Grade
40 questions
it fun
Lesson
•
KG
Popular Resources on Wayground
15 questions
Fractions on a Number Line
Quiz
•
3rd Grade
10 questions
Probability Practice
Quiz
•
4th Grade
15 questions
Probability on Number LIne
Quiz
•
4th Grade
20 questions
Equivalent Fractions
Quiz
•
3rd Grade
25 questions
Multiplication Facts
Quiz
•
5th Grade
22 questions
fractions
Quiz
•
3rd Grade
6 questions
Appropriate Chromebook Usage
Lesson
•
7th Grade
10 questions
Greek Bases tele and phon
Quiz
•
6th - 8th Grade