![Page 1: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/1.jpg)
โครงการความรว่มมือระหว่าง CDG Code Their Dreams กบั ภาควิชาคอมพิวเตอรศ์กึษา คณะครุศาสตรอ์สุาหกรรม มหาวิทยาลยัเทคโนโลยีพระจอมเกลา้พระนครเหนือโครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรมคอมพวิเตอรเ์พือ่การศึกษา
![Page 2: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/2.jpg)
ช่องทางประชาสมัพนัธห์ลกั👍 Facebook Fanpage :
• Code Their Dreams
• ศนูยส์ง่เสรมิการเรยีนรู ้สมาคมศษิยเ์ก่าครุศาสตรอ์ุตสาหกรรม มจพ.
ชอ่งทางประชาสมัพนัธเ์พิม่เตมิ• หน่วยฝึกอบรมภาควชิาคอมพวิเตอรศ์กึษา มจพ.
![Page 3: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/3.jpg)
Coding
![Page 4: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/4.jpg)
เริ่มเรียนรู้
output
Hello Python
![Page 5: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/5.jpg)
เริ่มเรียนรู้
Code
print (“Hello Python”)
![Page 6: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/6.jpg)
เริ่มเรียนรู้Code
print (“Hello Python”)
2. print (‘Name surname’)
3. print (Hello Python)
4. print (2)
5. print ( )
1. print (“Name surname”)
![Page 7: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/7.jpg)
เริ่มเรียนรู้
Code
1. print (“Titipong Thammavisut”)
output
Titipong Thammavisut
![Page 8: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/8.jpg)
เริ่มเรียนรู้
Code
2. print (‘Titipong Thammavisut’)
output
Titipong Thammavisut
![Page 9: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/9.jpg)
เริ่มเรียนรู้
Code
3. print (Hello Python)
output
File "<ipython-input-2-9489b56b625c>", line 1
print(Hello Python)^
SyntaxError: invalid syntax
![Page 10: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/10.jpg)
เริ่มเรียนรู้
Code
4. print (1)
output
1
![Page 11: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/11.jpg)
ข้อผิดพลาดและความหมายของSyntax
![Page 12: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/12.jpg)
ข้อผิดพลาดCode
print (Hello Python)
output
File "<ipython-input-2-9489b56b625c>", line 1
print(Hello Python)^
SyntaxError: invalid syntax
Code
print (1)
output
1
![Page 13: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/13.jpg)
output
File "<ipython-input-2-9489b56b625c>", line 1
print(Hello Python)^
SyntaxError: invalid syntax
ข้อผิดพลาด
![Page 14: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/14.jpg)
output
File "<ipython-input-2-9489b56b625c>", line 1
print(Hello Python)^
SyntaxError: invalid syntax
บรรทัดที่มีปัญหา
ข้อผิดพลาด
![Page 15: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/15.jpg)
output
File "<ipython-input-2-9489b56b625c>", line 1
print(Hello Python)^
SyntaxError: invalid syntax
บรรทัดที่มีปัญหา
รูปแบบข้อผิดพลาด
ข้อผิดพลาด
![Page 16: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/16.jpg)
output
print ( )
Syntax
โครงสร้างของค าส่ัง กฏการปฏิบัติ
ชื่อค าสั่งArgument
ค่าที่ส่งให้ค าสั่งน าไปใช้
![Page 17: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/17.jpg)
output
print (“Hello Python”)
Syntax
โครงสร้างของค าส่ัง กฏการปฏิบัติ
ชื่อค าสั่งArgument
ค่าที่ส่งให้ค าสั่งน าไปใช้
![Page 18: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/18.jpg)
output
print (‘Hello Python’)
Syntax
โครงสร้างของค าส่ัง กฏการปฏิบัติ
ชื่อค าสั่งArgument
ค่าที่ส่งให้ค าสั่งน าไปใช้
![Page 19: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/19.jpg)
output
print ( 1 )
Syntax
โครงสร้างของค าส่ัง กฏการปฏิบัติ
ชื่อค าสั่งArgument
ค่าที่ส่งให้ค าสั่งน าไปใช้
![Page 20: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/20.jpg)
output
print ( valiable )
Syntax
โครงสร้างของค าส่ัง กฏการปฏิบัติ
ชื่อค าสั่งArgument
ค่าที่ส่งให้ค าสั่งน าไปใช้
![Page 21: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/21.jpg)
ตัวด าเนินการทางคณิตศาสตร์
![Page 22: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/22.jpg)
output
ตัวด าเนินการทางคณิตศาสตร์Code
print (1)
output
1
![Page 23: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/23.jpg)
ตัวด าเนินการทางคณิตศาสตร์Code
print (1+5)
output
6
![Page 24: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/24.jpg)
ตัวด าเนินการทางคณิตศาสตร์
+ บวก- ลบ* คูณ/ หาร
% มอดุโล** ยกก าลัง// หารไม่เอาเศษ
7 + 3 = 10
7 - 3 = 4
7 * 3 = 21
7 / 3 = 2.33333333
7 % 3 = 1
7 ** 3 = 343
7 // 3 = 2
![Page 25: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/25.jpg)
Question
![Page 26: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/26.jpg)
ลองหาค าตอบกันดูซ…ิว่าได้อะไรออกมา
print (20.5 - 30)
print (50 / 5)
print (50 // 5)
print (1 + 1 + 1 * 0)
![Page 27: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/27.jpg)
ล าดับความส าคัญของเครื่องหมาย
![Page 28: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/28.jpg)
ล าดับความส าคัญของเครื่องหมาย
print (20.5 - 30) = - 9.5
print (50 / 5) = 10.0
print (50 // 5) = 10
print (1 + 1 + 1 * 0) = 2
![Page 29: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/29.jpg)
print (1 + 1 + 1 * 0) = 2
print (1 + 1 + (1 * 0)) = 2
print ((1 + 1 + 1) * 0) = 0
print (1 + (1 + 1) * 0) = 1
ล าดับความส าคัญของเครื่องหมาย
![Page 30: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/30.jpg)
1. ( )
2. **
3. * , / , // , %
4. + , -
ล าดับความส าคัญของเครื่องหมาย
มากสุด
น้อยสุด
![Page 31: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/31.jpg)
print (7 // 5 * 3) = 3
print (2 * (1 - 3)) = - 4
print ((1 + 1 + 1) * 0) = 0
print (1 + (1 + 1) * 0) = 1
ล าดับความส าคัญของเครื่องหมาย
![Page 32: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/32.jpg)
Code
output
7 // 5 = 1
Code
output
1 – 3 = - 2
ล าดับความส าคัญของเครื่องหมาย
print (7 // 5 * 3) print (2 * (1 - 3))
1 * 3 = 3 - 2 * 2 = - 4
- 43
![Page 33: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/33.jpg)
Question
![Page 34: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/34.jpg)
Code
output
7 % 3 = 1
print ((4 + 7 % 3) – 1 * 3 ** 2)
4 + 1 = 5
- 4
ลองหาค าตอบกันดูซ…ิว่าได้อะไรออกมา
3 ^ 3 = 9
9 * - 1 = - 9
5 + - 9 = - 4
![Page 35: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/35.jpg)
ตัวแปร
![Page 36: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/36.jpg)
ตัวแปรโปรแกรมแสดงค่าเส้นรอบวงและพื้นที่ของวงกลมสูตรการหาเส้นรอบวง = 2𝝅𝒓 และพื้นที่ = 𝝅𝒓𝟐𝝅 = 𝟑. 𝟏𝟒 𝒓 = 𝟑
Code output
print (2 * 3.14 * 3)
print (3.14 * (3 ** 2))
18.84
28.26
![Page 37: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/37.jpg)
ตัวแปรโปรแกรมแสดงค่าเส้นรอบวงและพื้นที่ของวงกลมสูตรการหาเส้นรอบวง = 2𝝅𝒓 และพื้นที่ = 𝝅𝒓𝟐𝝅 = 𝟑. 𝟏𝟒 𝒓 = 𝟒𝟓
Code output
print (2 * 3.14 * 3)
print (3.14 * (3 ** 2))
18.84
28.26
![Page 38: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/38.jpg)
ตัวแปรโปรแกรมแสดงค่าเส้นรอบวงและพื้นที่ของวงกลมสูตรการหาเส้นรอบวง = 2𝝅𝒓 และพื้นที่ = 𝝅𝒓𝟐𝝅 = 𝟑. 𝟏𝟒 𝒓 = 𝟒𝟓
Code output
print (2 * 3.14 * 45)
print (3.14 * (45 ** 2))
282.6
6358.5
![Page 39: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/39.jpg)
ตัวแปรโปรแกรมแสดงค่าเส้นรอบวงและพื้นที่ของวงกลมสูตรการหาเส้นรอบวง = 2𝝅𝒓 และพื้นที่ = 𝝅𝒓𝟐𝝅 = 𝟑. 𝟏𝟒𝟏𝟓𝟗 𝒓 = 𝟒𝟓
Code output
print (2 * 3.14159 * 45)
print (3.14159 * (45 ** 2))
282.7430999999997
6361.71975
![Page 40: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/40.jpg)
ตัวแปรโปรแกรมแสดงค่าเส้นรอบวงและพื้นที่ของวงกลมสูตรการหาเส้นรอบวง = 2𝝅𝒓 และพื้นที่ = 𝝅𝒓𝟐𝝅 = 𝟑. 𝟏𝟒 𝒓 = 𝟑
Code output
print (2 * py * r)
print (py * (r ** 2))
pi = 3.14
r = 3
18.84
28.26
![Page 41: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/41.jpg)
ตัวแปรโปรแกรมแสดงค่าเส้นรอบวงและพื้นที่ของวงกลมสูตรการหาเส้นรอบวง = 2𝝅𝒓 และพื้นที่ = 𝝅𝒓𝟐𝝅 = 𝟑. 𝟏𝟒𝟏𝟓𝟗 𝒓 = 𝟒𝟓
Code output
print (2 * pi * r)
print (pi * (r ** 2))
pi = 3.14159
r = 45
282.7430999999997
6361.71975
![Page 42: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/42.jpg)
การตั้งชื่อตัวแปร
![Page 43: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/43.jpg)
การตั้งชื่อตัวแปร1. ขึ้นต้นด้วยตัวอักษร A-Z หรือ a-z หรือ _ 2. ตามด้วยตัวอักษร ตัวเลข (0-9) หรือ _ กี่ตัวก็ได้3. ห้ามตั้งตรงกับค าที่สงวนไว้
![Page 44: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/44.jpg)
การก าหนดค่าให้กับตัวแปรการก าหนดค่าโดยใช้เครื่องหมาย =
pi = 3.14
radius = 3
area = pi * (radius ** 2)
การเรียกใช้ค่าตัวแปร
print (area)
print (pi * (radius ** 2))
![Page 45: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/45.jpg)
ตัวแปรสามารถเปลี่ยนค่าได้
radius = 3
print (radius)
Code output
3
![Page 46: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/46.jpg)
ตัวแปรสามารถเปลี่ยนค่าได้
radius = 3
print (radius)
Code output
5
radius = 5
![Page 47: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/47.jpg)
ตัวแปรสามารถเปลี่ยนค่าได้
radius = 3
print (radius)
Code output
6
radius = 5radius = radius + 1
![Page 48: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/48.jpg)
ตัวแปรสามารถเปลี่ยนค่าได้และอ้างอิงค่าเก่า
radius = 3
print (radius)
Code output
7
radius = 5radius = radius + 1
![Page 49: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/49.jpg)
ตัวแปรสามารถเปลี่ยนค่าได้และอ้างอิงค่าเก่า
radius = 3
print (radius)
Code output
2
radius = 5radius = radius + 1
radius += 1
radius -= 5
![Page 50: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/50.jpg)
Question
![Page 51: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/51.jpg)
Code output
year = 2
ลองหาค าตอบกันดูซ…ิว่าได้อะไรออกมา
month = year * 12
print (month)
year = year + 1
print (month)
24
24
![Page 52: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/52.jpg)
ชนิดของข้อมูล
![Page 53: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/53.jpg)
Print (“Hello Python”)
สตริง หรือ ข้อความ
ใช้เก็บข้อมูลประเภทตัวอักษร จะอยู่ในเครื่องหมาย “ ” หรือ ‘ ’
Hello Python
Print (“3”) 3
Print (“3.14 + 2.5”) 3.14 + 2.5
![Page 54: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/54.jpg)
สตริง หรือ ข้อความ
ความแตกต่างของการมีกับไม่มี “ ”
5.64
Print (“3.14 + 2.5”) 3.14 + 2.5
Print (3.14 + 2.5)
![Page 55: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/55.jpg)
สตริง หรือ ข้อความ
ความแตกต่างของการมีกับไม่มี “ ”
Print (“Hello”)
Hello
Print (Hello)
NameError: name 'Hello' is not defined
![Page 56: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/56.jpg)
สตริง หรือ ข้อความ
ความแตกต่างของการมีกับไม่มี “ ”
Print (“Hello”)
Hello
Print (Hello)
Python
Hello = “Python”
![Page 57: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/57.jpg)
สตริง หรือ ข้อความ
a = 3b = 5Print (a + b)
8 35
a = “3”b = “5”print (a + b)
สตริง ตัวเลข และการกระท า
![Page 58: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/58.jpg)
สตริง หรือ ข้อความ
a = 3b = 5Print (a - b)
- 2 TypeError: unsupported operand type(s) for -: 'str' and 'str'
a = “3”b = “5”print (a - b)
สตริง ตัวเลข และการกระท า
![Page 59: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/59.jpg)
สตริง หรือ ข้อความ
TypeError: can only concatenate str (not "int") to str
a = “3”b = 5print (a + b)
สตริง ตัวเลข และการกระท า
![Page 60: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/60.jpg)
สตริง หรือ ข้อความ
35
a = “3”b = 5print (a + str(b))
สตริง ตัวเลข และการกระท า
![Page 61: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/61.jpg)
สตริง หรือ ข้อความ
8
a = “3”b = 5print(type( a ))print (int(a) + b)print(type( a ))
สตริง ตัวเลข และการกระท า
![Page 62: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/62.jpg)
สตริง หรือ ข้อความ
This is 3 and 5
a = “3”b = 5print (“This is ” + a + “ and ” + str(b))
สตริง ตัวเลข และการกระท า + ต่อข้อความ
![Page 63: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/63.jpg)
สตริง หรือ ข้อความ
a = “3”b = 5print (“This is %s and %d” % (a, b) )
สตริง ตัวเลข และการกระท า + ต่อข้อความ
![Page 64: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/64.jpg)
สตริง หรือ ข้อความ
This is 3 and 5
a = “3”b = 5print (“This is %s and %d” % (a, b) )
สตริง ตัวเลข และการกระท า + ต่อข้อความ
![Page 65: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/65.jpg)
คุณสมบัติของสตริง
![Page 66: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/66.jpg)
อินเด็กซ์ (Index) ของสตริง
s = “Hello, world”
สตริง ตัวเลข และการกระท า + ต่อข้อความ
0 1 2 3 4 5 6 7 8 9 10 11
H e l l o , w o r l d
print (s[0])
H
![Page 67: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/67.jpg)
อินเด็กซ์ (Index) ของสตริง
s = “Hello, world”
สตริง ตัวเลข และการกระท า + ต่อข้อความ
0 1 2 3 4 5 6 7 8 9 10 11
H e l l o , w o r l d
print (s[7])
w
![Page 68: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/68.jpg)
Question
![Page 69: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/69.jpg)
s = “Hello, world”
0 1 2 3 4 5 6 7 8 9 10 11
H e l l o , w o r l d
print (s[12]) IndexError: string index out of range
ลองหาค าตอบกันดูซ…ิว่าได้อะไรออกมา
![Page 70: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/70.jpg)
s = “Hello, world”
0 1 2 3 4 5 6 7 8 9 10 11
H e l l o , w o r l d
dprint (s[-1])
ลองหาค าตอบกันดูซ…ิว่าได้อะไรออกมา
![Page 71: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/71.jpg)
s = “Hello, world”
0 1 2 3 4 5 6 7 8 9 10 11
H e l l o , w o r l d
,print (s[-7])
ลองหาค าตอบกันดูซ…ิว่าได้อะไรออกมา
![Page 72: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/72.jpg)
การเข้าถึงบางส่วนของสตริง
![Page 73: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/73.jpg)
s = “Hello, world”
0 1 2 3 4 5 6 7 8 9 10 11
H e l l o , w o r l d
print (s[2:5]) llo
การเข้าถึงบางส่วนของสตริง
![Page 74: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/74.jpg)
s = “Hello, world”
0 1 2 3 4 5 6 7 8 9 10 11
H e l l o , w o r l d
print (s[2:5]) llo
print (s[0:6])
การเข้าถึงบางส่วนของสตริง
![Page 75: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/75.jpg)
s = “Hello, world”
0 1 2 3 4 5 6 7 8 9 10 11
H e l l o , w o r l d
print (s[2:5]) llo
print (s[0:6]) Hello,
การเข้าถึงบางส่วนของสตริง
![Page 76: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/76.jpg)
s = “Hello, world”
0 1 2 3 4 5 6 7 8 9 10 11
H e l l o , w o r l d
print (s[2:5]) llo
print (s[0:6]) Hello,
print (s[:6])
การเข้าถึงบางส่วนของสตริง
![Page 77: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/77.jpg)
s = “Hello, world”
0 1 2 3 4 5 6 7 8 9 10 11
H e l l o , w o r l d
print (s[2:5]) llo
print (s[0:6]) Hello,
print (s[:6]) Hello,
การเข้าถึงบางส่วนของสตริง
![Page 78: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/78.jpg)
s = “Hello, world”
0 1 2 3 4 5 6 7 8 9 10 11
H e l l o , w o r l d
ello, worlprint (s[1:11])
การเข้าถึงบางส่วนของสตริง
![Page 79: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/79.jpg)
s = “Hello, world”
0 1 2 3 4 5 6 7 8 9 10 11
H e l l o , w o r l d
ello, worlprint (s[1:11])
print (s[1:])
การเข้าถึงบางส่วนของสตริง
![Page 80: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/80.jpg)
s = “Hello, world”
0 1 2 3 4 5 6 7 8 9 10 11
H e l l o , w o r l d
ello, worlprint (s[1:11])
print (s[1:]) ello, world
การเข้าถึงบางส่วนของสตริง
![Page 81: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/81.jpg)
s = “Hello, world”
0 1 2 3 4 5 6 7 8 9 10 11
H e l l o , w o r l d
ello, worlprint (s[1:11])
print (s[1:]) ello, world
print (s[7:-2])
การเข้าถึงบางส่วนของสตริง
![Page 82: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/82.jpg)
s = “Hello, world”
0 1 2 3 4 5 6 7 8 9 10 11
H e l l o , w o r l d
ello, worlprint (s[1:11])
print (s[1:]) ello, world
print (s[7:-2]) wor
การเข้าถึงบางส่วนของสตริง
![Page 83: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/83.jpg)
Question
![Page 84: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/84.jpg)
s = “Hello, world”
0 1 2 3 4 5 6 7 8 9 10 11
H e l l o , w o r l d
print (………..) world
ลองหาค าตอบกันดูซ…ิว่าได้อะไรออกมา
![Page 85: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/85.jpg)
s = “Hello, world”
0 1 2 3 4 5 6 7 8 9 10 11
H e l l o , w o r l d
print (s[7:]) world
ลองหาค าตอบกันดูซ…ิว่าได้อะไรออกมา
![Page 86: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/86.jpg)
s = “Hello, world”
0 1 2 3 4 5 6 7 8 9 10 11
H e l l o , w o r l d
print (s[7:]) world
print (s[7:12]) world
ลองหาค าตอบกันดูซ…ิว่าได้อะไรออกมา
![Page 87: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/87.jpg)
การหาความยาวของสตริง
![Page 88: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/88.jpg)
s = “Hello, world”
print (len(s)) 12
การหาความยาวของสตริง (lenght)
![Page 89: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/89.jpg)
การค้นหาสตริง
![Page 90: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/90.jpg)
text = “Hello, world”
print (text.find(word))7
การค้นหาสตริง (find)
word = “word”
ค้นหาอินเด็กซ์เริ่มต้นของสตริง
![Page 91: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/91.jpg)
text = “Hello, world”
print (text.find(word))-1
การค้นหาสตริง (find)
word = “man”
ค้นหาอินเด็กซ์เริ่มต้นของสตริง
![Page 92: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/92.jpg)
text = “Hello, world. This is my world.”
print (text.find(word, 14))25
การค้นหาสตริง (find)
word = “world”
ค้นหาอินเด็กซ์เริ่มต้นและจุดสิ้นสุด
print (text.find(word, 14, len(text)))
![Page 93: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/93.jpg)
การค้นหาสตริงที่อยู่ติดกัน
![Page 94: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/94.jpg)
text = "Today is Sunday. I stay at home and keep coding. "
text = text [ text.find(word) + len (word) + 1 : ]
การค้นหาสตริงที่อยู่ติดกันในค าถัดไป (find)
word = “keep”
next_word = text [ : text.find(“ ”) ]
print (next_word)
coding.
![Page 95: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/95.jpg)
text = "Today is Sunday. I stay at home and keep coding. "
text = text [ text.find(word) + len (word) + 1 : ]
การค้นหาสตริงที่อยู่ติดกันในค าถัดไป (find)
word = “at”
next_word = text [ : text.find(“ ”) ]
print (next_word)
home
![Page 96: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/96.jpg)
text = "Today is Sunday. I stay at home and keep coding. Today is Monday. I stay at office and keep cleaning but still keep coding. "
การค้นหาสตริงที่อยู่ติดกันในค าถัดไปหลายค า (find)
coding.
word = “keep”start_pos = 0
text = text [ text.find ( word, start_pos) + len (word) + 1 : ]
next_word = text [ : text.find(“ ”) ]
print (next_word)
start_pos = text.find (“ ”) + 1
![Page 97: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/97.jpg)
text = "Today is Sunday. I stay at home and keep coding. Today is Monday. I stay at office and keep cleaning but still keep coding. "
cleaning
word = “keep”
text = text [ text.find ( word, start_pos) + len (word) + 1 : ]
next_word = text [ : text.find(“ ”) ]
print (next_word)
start_pos = text.find (“ ”) + 1
การค้นหาสตริงที่อยู่ติดกันในค าถัดไปหลายค า (find)
![Page 98: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/98.jpg)
text = "Today is Sunday. I stay at home and keep coding. Today is Monday. I stay at office and keep cleaning but still keep coding. "
coding.
word = “keep”
text = text [ text.find ( word, start_pos) + len (word) + 1 : ]
next_word = text [ : text.find(“ ”) ]
print (next_word)
การค้นหาสตริงที่อยู่ติดกันในค าถัดไปหลายค า (find)
![Page 99: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/99.jpg)
ฟังก์ชัน
![Page 100: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/100.jpg)
ฟังก์ชัน
print (“world”)
word = “keep”
Word_length = len (word)
![Page 101: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/101.jpg)
ฟังก์ชัน
radius = 3
pi = 3.14
area = pi * (radius ** 2)
print (new_area)
new_pi = 3.1416
new_radius = 5
new_area = new_pi * (new_radius ** 2 )
print (area)
![Page 102: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/102.jpg)
ฟังก์ชัน
radius = 3
pi = 3.14
area = pi * (radius ** 2)
print (new_area)
new_pi = 3.1416
new_radius = 5
new_area = new_pi * (new_radius ** 2 )
print (area)
![Page 103: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/103.jpg)
ฟังก์ชัน
< code >
def function_name ( < parameters >) :
return result
การประกาศฟังก์ชัน
การเรียกใช้ฟังก์ชันfunction_name ( < parameters >) :
![Page 104: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/104.jpg)
ฟังก์ชัน
radius = 3
pi = 3.14
area = get_area ( pi, radius)
print (new_area)
new_pi = 3.1416
new_radius = 5
new_area = get_area (new_pi, new_radius)
print (area)
def get_area ( pi, radius) :
return pi * (radius ** 2)
![Page 105: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/105.jpg)
Question
![Page 106: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/106.jpg)
ฟังก์ชัน
ให้เขียนฟังก์ชันค านวณพื้นที่สามเหลี่ยมก าหนดชื่อฟังก์ชันเป็น get_triangle_area
รับพารามิเตอร์ 2 ตัว คือ height, base_width
ค่าที่ต้องส่งคืน คือ ค่าของพื้นที่สามเหลี่ยมตามสูตร 0.5 * ฐาน * สูงแสดงค่าผลลัพธืที่หาได้
![Page 107: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/107.jpg)
ฟังก์ชัน
base_width = 5
height = 10
def get_tringle_area ( height, base_width) :
return 0.5 * base_width * height
print(get_tringle_area(height, base_width))
25.0
![Page 108: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/108.jpg)
text = “Hello, world”
print (text.find(word))7
การค้นหาสตริง (find)
word = “world”
ค้นหาอินเด็กซ์เริ่มต้นของสตริง
![Page 109: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/109.jpg)
text = “Hello, world”
print (text.find(word))-1
การค้นหาสตริง (find)
word = “man”
ค้นหาอินเด็กซ์เริ่มต้นของสตริง
![Page 110: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/110.jpg)
text = “Hello, world. This is my world.”
print (text.find(word, 14))2525
การค้นหาสตริง (find)
word = “world”
ค้นหาอินเด็กซ์เริ่มต้นและจุดสิ้นสุด
print (text.find(word, 14, len(text)))
![Page 111: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/111.jpg)
การค้นหาสตริงที่อยู่ติดกัน
![Page 112: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/112.jpg)
text = "Today is Sunday. I stay at home and keep coding. "
text = text [ text.find(word) + len (word) + 1 : ]
การค้นหาสตริงที่อยู่ติดกันในค าถัดไป (find)
word = “keep”
next_word = text [ 0: text.find(“ ”) ]
print (next_word)
coding.
![Page 113: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/113.jpg)
text = "Today is Sunday. I stay at home and keep coding. "
text = text [ text.find(word) + len (word) + 1 : ]
การค้นหาสตริงที่อยู่ติดกันในค าถัดไป (find)
word = “at”
next_word = text [ : text.find(“ ”) ]
print (next_word)
home
![Page 114: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/114.jpg)
text = "Today is Sunday. I stay at home and keep coding. Today is Monday. I stay at office and keep cleaning but still keep coding. "
การค้นหาสตริงที่อยู่ติดกันในค าถัดไปหลายค า (find)
coding.
word = “keep”start_pos = 0
text = text [ text.find ( word, start_pos) + len (word) + 1 : ]
next_word = text [ : text.find(“ ”) ]
print (next_word)
start_pos = text.find (“ ”) + 1
![Page 115: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/115.jpg)
text = "Today is Sunday. I stay at home and keep coding. Today is Monday. I stay at office and keep cleaning but still keep coding. "
cleaning
word = “keep”
text = text [ text.find ( word, start_pos) + len (word) + 1 : ]
next_word = text [ : text.find(“ ”) ]
print (next_word)
start_pos = text.find (“ ”) + 1
การค้นหาสตริงที่อยู่ติดกันในค าถัดไปหลายค า (find)
![Page 116: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/116.jpg)
text = "Today is Sunday. I stay at home and keep coding. Today is Monday. I stay at office and keep cleaning but still keep coding. "
coding.
word = “keep”
text = text [ text.find ( word, start_pos) + len (word) + 1 : ]
next_word = text [ : text.find(“ ”) ]
print (next_word)
การค้นหาสตริงที่อยู่ติดกันในค าถัดไปหลายค า (find)
![Page 117: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/117.jpg)
ฟังก์ชัน
![Page 118: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/118.jpg)
ฟังก์ชัน
print (“world”)
word = “keep”
Word_length = len (word)
![Page 119: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/119.jpg)
ฟังก์ชัน
radius = 3
pi = 3.14
area = pi * (radius ** 2)
print (new_area)
new_pi = 3.1416
new_radius = 5
new_area = new_pi * (new_radius ** 2 )
print (area)
![Page 120: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/120.jpg)
ฟังก์ชัน
radius = 3
pi = 3.14
area = pi * (radius ** 2)
print (new_area)
new_pi = 3.1416
new_radius = 5
new_area = new_pi * (new_radius ** 2 )
print (area)
![Page 121: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/121.jpg)
ฟังก์ชัน
< code >
def function_name ( < parameters >) :
return result
การประกาศฟังก์ชัน
การเรียกใช้ฟังก์ชันfunction_name ( < parameters >) :
![Page 122: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/122.jpg)
ฟังก์ชัน
radius = 3
pi = 3.14
area = get_area ( pi, radius)
print (new_area)
new_pi = 3.1416
new_radius = 5
new_area = get_area (new_pi, new_radius)
print (area)
def get_area ( pi, radius) :
return pi * (radius ** 2)
![Page 123: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/123.jpg)
Question
![Page 124: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/124.jpg)
ฟังก์ชัน
ให้เขียนฟังก์ชันค านวณพื้นที่สามเหลี่ยมก าหนดชื่อฟังก์ชันเป็น get_triangle_area
รับพารามิเตอร์ 2 ตัว คือ height, base_width
ค่าที่ต้องส่งคืน คือ ค่าของพื้นที่สามเหลี่ยมตามสูตร 0.5 * ฐาน * สูงแสดงค่าผลลัพธืที่หาได้
![Page 125: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/125.jpg)
ฟังก์ชัน
base_width = 5
height = 10
def get_tringle_area ( height, base_width) :
return 0.5 * base_width * height
print(get_tringle_area(height, base_width))
25.0
![Page 126: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/126.jpg)
ขอบเขตตัวแปร
![Page 127: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/127.jpg)
ขอบเขตของตัวแปร (การโยนค่าพารามิเตอร)์
def get_area(pi, radius):return pi * (radius ** 2)
pi = 3.14 radius = 3 area = get_area(3.1416, 5)print(area)
![Page 128: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/128.jpg)
ขอบเขตของตัวแปร (การโยนค่าพารามิเตอร)์
def get_area(pi, radius):return pi * (radius ** 2)
pi = 3.14 radius = 3 area = get_area(3.1416, 5)print(area)
![Page 129: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/129.jpg)
def get_area(pi, radius):print("in function") print("pi = %0.4f, radius = %d" % (pi, radius))return pi * (radius ** 2)
pi = 3.14 radius = 3 area = get_area(3.1416, 5)
ขอบเขตของตัวแปร (การโยนค่าพารามิเตอร)์
![Page 130: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/130.jpg)
def get_area(pi, radius):print("in function")print("pi = %0.4f, radius = %d" % (pi, radius))return pi * (radius ** 2)
pi = 3.14radius = 3area = get_area(3.1416, 5) print("after function") print("pi = %0.4f, radius = %d" % (pi, radius))
ขอบเขตของตัวแปร (การโยนค่าพารามิเตอร)์
![Page 131: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/131.jpg)
def get_area(pi, radius):print("in function")print("pi = %0.4f, radius = %d" % (pi, radius))return pi * (radius ** 2)
pi = 3.14 radius = 3 area = get_area(3.1416, 5) print("after function") print("pi = %0.4f, radius = %d" % (pi, radius))
in functionpi = 3.1416, radius = 5after functionpi = 3.1400, radius = 3
ขอบเขตของตัวแปร (การโยนค่าพารามิเตอร)์
![Page 132: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/132.jpg)
def get_area(pi, radius):radius += 1print("in function")print("pi = %0.4f, radius = %d" % (pi, radius))return pi * (radius ** 2)
pi = 3.14 radius = 3 area = get_area(3.1416, 5) print("after function") print("pi = %0.4f, radius = %d" % (pi, radius))
in functionpi = 3.1416, radius = 6after functionpi = 3.1400, radius = 3
ขอบเขตของตัวแปร (การโยนค่าพารามิเตอร)์
![Page 133: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/133.jpg)
def get_area(pi):print("in function") print("radius = %d" % (radius))return pi * (radius ** 2)
pi = 3.14radius = 3area = get_area(3.1416)print("after function")print("radius = %d" % (radius))
in functionradius = 3after functionradius = 3
ขอบเขตของตัวแปร (การโยนค่าพารามิเตอร)์
![Page 134: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/134.jpg)
def get_area(pi):print("in function") print("radius = %d" % (radius))return pi * (radius ** 2)
pi = 3.14radius = 3area = get_area(3.1416)print("after function")print("radius = %d" % (radius))
in functionradius = 3after functionradius = 3
ขอบเขตของตัวแปร (การโยนค่าพารามิเตอร)์
การอ้างอิงตัวแปร1. มองหาค่าจาก Local variable ก่อน2. ถ้าไม่เจอ ไปมองหาค่าจาก Global variable
Local variable
Global variable
![Page 135: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/135.jpg)
def find_next_word(text, word, start_pos):text = text[text.find(word, start_pos) + len(word) + 1:]next_word = text[: text.find(“ ”)]print(next_word)start_pos = text.find(“ ”) + 1return text, start_pos
text = "Today is Sunday. I stay at home and keep coding. Today is Monday. I stay, at office and keep cleaning but still keep coding. "
word = "keep"start_pos= 0text, start_pos = find_next_word(text, word, start_pos)text, start_pos = find_next_word(text, word, start_pos)text, start_pos = find_next_word(text, word, start_pos)
สร้างฟังก์ชันหาค าถัดไป
![Page 136: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/136.jpg)
เงื่อนไข
![Page 137: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/137.jpg)
text = "I keep coding "word = "keep"text = text[text.find(word) + len(word) + 1:]next_word = text[:text.find(“ ”)]print(next_word)
text = “coding ”text.find(“ ”) = 6
เงื่อนไข ความเป็นจริงไม่มีเว้นวรรค
coding
เจอ “ ” จะส่ง -1 กลับมา
![Page 138: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/138.jpg)
text = "I keep coding"word = "keep"text = text[text.find(word) + len(word) + 1:]next_word = text[:text.find(" ")]print(next_word)
text = "coding" text.find(“ ”) = -1
เงื่อนไข ความเป็นจริงไม่มีเว้นวรรค
codin
ไม่เจอ “ ” จะส่ง -1 กลับมา
![Page 139: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/139.jpg)
if (<Condition>) :<Block 1>
เงื่อนไข if
![Page 140: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/140.jpg)
if (<Condition>) :<Block 1>
else:<Block 2>
เงื่อนไข if
![Page 141: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/141.jpg)
if (<Condition 1>):<Block 1>
elif (<Condition 2>) :<Block 2>
else:<Block 3>
เงื่อนไข if
![Page 142: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/142.jpg)
if (<Condition 1>):<Block 1>
elif (<Condition 2>):<Block 2>
elif (<Condition 3>):<Block 3>
else:<Block 4>
เงื่อนไข if
![Page 143: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/143.jpg)
if (<Condition 1>):<Block 1>
elif (<Condition 2>):<Block 2>
elif (<Condition 3>):<Block 3>
else:<Block 4>
เงื่อนไข if
![Page 144: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/144.jpg)
next_word = text[:text.find(" ")]
if (text.find(" ") != -1):
next_word = text[:text.find(" ")]
else:
next_word = text
เงื่อนไข if การหาค าสุดท้าย -1
==, !=, <, >, <=, >=
![Page 145: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/145.jpg)
if (<Condition 1> and <Condition 2> and <Condition 3>):<Block 1>
if (<Condition 1> or <Condition 2> or <Condition 3>):<Block 1>
การเชื่อมเงื่อนไข
![Page 146: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/146.jpg)
if (a >= 5 and a <= 10) :
if (a == 5 or a == 10) :
if (a == 5 or a == 10 or a == 15) :
if ((a == 5) or (a >= 10 and a <= 15)) :
การเชื่อมเงื่อนไข
![Page 147: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/147.jpg)
if (<Condition 1>): if (<Condition 2>):
<Block 1>
else (<Condition 3>):
<Block 2>
else:<Block 3>
เงื่อนไขซ้อนเงื่อนไข
![Page 148: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/148.jpg)
Question
![Page 149: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/149.jpg)
ก าหนดตัวแปร Score เก็บตัวเลขคะแนนสอบไว้ ให้เขียนโปรแกรมค านวณเกรด โดยโปรแกรมจะ พิมพ์เกรดที่ได้ออกทางหน้าจอ วิธีการค านวณเป็นดังนี้
ถ้าได้ตั้งแต่ 80 คะแนนขึ้นไป ให้เกรด "A"
ถ้าได้ตั้งแต่ 70 คะแนนขึ้นไป แต่น้อยกว่า 80 ให้เกรด "B"
ถ้าได้ตั้งแต่ 60 คะแนนขึ้นไป แต่น้อยกว่า 70 ให้เกรด "C"
ถ้าได้ตั้งแต่ 50 คะแนนขึ้นไป แต่น้อยกว่า 60 ให้เกรด "D"
ถ้าได้ต่ ากว่า 50 คะแนน ให้เกรด "F"
ลองเขียนดูซิ……
![Page 150: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/150.jpg)
score = 80if (score >= 80):
print(“A”)
elif (score >= 70 and score < 80 ):print(“B”)
elif (score >= 60 and score < 70 ):print (“C”)
elif (score >= 50 and score < 60 ):print(“D”)
else: print(“F”)
ลองเขียนดูซิ……
![Page 151: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/151.jpg)
score = 80if (score >= 80):
print(“A”) elif (70 <= score < 80 ):
print(“B”) elif (60 <= score < 70 ):
print(“C”) elif (50 <= score < 60 ):
print(“D”) else:
print(“F”)
ลองเขียนดูซิ……
![Page 152: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/152.jpg)
score = 80if (score >= 80):
print(“A”) elif (score >= 70):
print (“B”),elif (score >= 60):
print(“C”)elif (score >= 50):
print(“D”)else:
print(“F”)
ลองเขียนดูซิ……
![Page 153: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/153.jpg)
Question
![Page 154: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/154.jpg)
def find_next_word(text, word, start_pos):
ลองเขียนดูซิ……
1. แสดงค่าค าที่อยู่ถัดจาก word ใน text โดยเริ่มค้นหาจากต าแหน่ง start_pos
และ return ค่าต าแหน่งของจุดเริ่มหาถดัไปแต่ถ้าสิ้นสุด text แล้วให้ return -12. ถ้าไม่มี word ปรากฏใน text ให้แสดงสตริงว่าง “ ” แล้ว return -1
![Page 155: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/155.jpg)
def find_next_word(text, word, start_pos):text = text[text.find(word, start_pos) + len(word) + 1:]next_word = text[: text.find(' ')]print(next_word)start_pos = text.find(' ') + 1return text, start_pos
ลองเขียนดูซิ…… สร้างตัวแปรใหม่
![Page 156: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/156.jpg)
def find_next_word(text, word, start_pos):word_pos = text.find(word, start_pos)text = text[word_pos + len(word) + 1:]next_word = text[: text.find(' ')]print(next_word)start_pos = text.find(' ') + 1return text, start_pos
ลองเขียนดูซิ…… สร้างตัวแปรใหม่
![Page 157: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/157.jpg)
def find_next_word(text, word, start_pos):word_pos = text.find(word, start_pos)if (word_pos != -1):
text = text[word_pos + len(word) + 1:]next_word = text[: text.find(' ')]print(next_word)start_pos = text.find(' ') + 1return text, start_pos
else:print(“ ")return “ ", -1
ลองเขียนดูซิ……เช็คเงื่อนไขว่าต าแหน่งที่หาไม่เท่ากับ -1
ถ้าเจอ
ถ้าไม่เจอ
![Page 158: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/158.jpg)
def find_next_word(text, word, start_pos):word_pos = text.find(word, start_pos)if (word_pos != -1):
text = text[word_pos + len(word) + 1:]if (text.find(‘ ') != -1):
next_word = text[: text.find(' ')]print(next_word)start_pos = text.find(' ') + 1return text, start_pos
else:print(“ ")
return -1
ลองเขียนดูซิ……ยังมีช่องว่างอยู่หรือไม่
ถ้ายังมีอยู่ยังไม่ใช่ค าสุดท้าย
![Page 159: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/159.jpg)
def find_next_word(text, word, start_pos):word_pos = text.find(word, start_pos)if (word_pos != -1):
text = text[word_pos + len(word) + 1:]if (text.find(‘ ') != -1):
next_word = text[: text.find(' ')]print(next_word)start_pos = text.find(' ') + 1return text, start_pos
else:next_word = textprint(next_word)start_pos= -1return text, start_pos
else:print(“ ")
return -1
ลองเขียนดูซิ……ยังมีช่องว่างอยู่หรือไม่
ถ้ายังมีอยู่ยังไม่ใช่ค าสุดท้าย
ถ้าเท่ากับ -1
![Page 160: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/160.jpg)
while Loop
![Page 161: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/161.jpg)
while (<Condition>) :<Block>
next_command
การวน loop while ตราบที่เงื่อนไขเป็นจริง
![Page 162: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/162.jpg)
a = 2
b = 1 while (a <= 12):
print(str(a) + "X" + str(b) + "=" + str(a * b))b = b + 1if (b > 12):
a = a + 1b = 1
การวน loop while
![Page 163: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/163.jpg)
การ break while
![Page 164: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/164.jpg)
a = 2b = 1while (True) :
print(str(a) + "X" + str(b) + "=" + str(a * b))b = b + 1if (b > 12) :
a = a + 1b = 1
if (a > 12) :break
การ break while
![Page 165: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/165.jpg)
ข้อมูล List
![Page 166: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/166.jpg)
s = "Hello World"
โครงสร้างข้อมูล List
l = [1, 2.5, "a" , "hello"]
สตริง – เก็บล าดับของตัวอักษร (sequence of characters)
ลิสต์ - เก็บล าดับของอะไรก็ได้
![Page 167: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/167.jpg)
s = "Hello World"print(s[2])
โครงสร้างข้อมูล List
l = [1, 2.5, "a", "hello"]print (l[2])
สตริง – เก็บล าดับของตัวอักษร (sequence of characters)
ลิสต์ - เก็บล าดับของอะไรก็ได้
l
a
![Page 168: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/168.jpg)
s = "Hello World"print(s[2])print(s[1:3])
โครงสร้างข้อมูล List
l = [1, 2.5, "a" , "hello"]print(l[2])print (l[1:3])
สตริง – เก็บล าดับของตัวอักษร (sequence of characters)
ลิสต์ - เก็บล าดับของอะไรก็ได้
lel
a[2.5, “a”]
![Page 169: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/169.jpg)
Question
![Page 170: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/170.jpg)
month = [ 'Jan' , 'Feb' , 'Mar' , 'Apr' , 'May', 'Jun' , 'Jul', 'Aug' , 'Sep' , 'Oct' , 'Nov' , 'Dec' ]
โครงสร้างข้อมูล List
ให้สร้างตัวแปรประเภทลิสต์ชื่อ month เพื่อเก็บชื่อย่อเดือนทั้ง 12 เดือน
'Jan', 'Feb', 'Mar', 'Apr', 'May, 'Jun', 'Jul', 'Aug', 'Sep',
'Oct', 'Nov, 'Dec
![Page 171: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/171.jpg)
แก้ไขข้อมูลใน List
![Page 172: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/172.jpg)
s = “Hello World” s[0] = ‘M’ print(s)
แก้ไขข้อมูลใน List
การแก้ไขข้อมูลในสตรงิ
Traceback (most recent call last): File “.....”, line 2, in <module>s[0] = ‘M’TypeError: ‘str’ object does not support item assignment
![Page 173: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/173.jpg)
l = [1, 2.5] l[0] = ‘M’ print (l)
แก้ไขข้อมูลใน List
การแก้ไขข้อมูลใน List
[ ‘M’ , 2.5]
![Page 174: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/174.jpg)
การต่อ List
![Page 175: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/175.jpg)
l = [1, 2]l.append (3) print(l)
การต่อ List
<name list>.append(<element list>)
[1,2,3]
![Page 176: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/176.jpg)
ความยาว List
![Page 177: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/177.jpg)
l = [1, 2] print(len(l))
ความยาว List
len(<name list>)
2
![Page 178: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/178.jpg)
l = [ ] print(len(l))
ความยาว List
len(<name list>)
0
![Page 179: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/179.jpg)
For loop
![Page 180: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/180.jpg)
weekdays = [ ‘Monday’, ‘Tuesday’, ‘Wednesday’, ‘Thursday’, ‘Friday’]
for day in weekdays:print(“Today is “ + day)
For loop
การวนลูป for ในลิสต์
ชื่อตัวแปร ชื่อ list
Today is Monday Today is Tuesday……………………
![Page 181: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/181.jpg)
text = “Today is Sunday. I stay at home and keep coding. Today is Monday. I stay at office and keep cleaning.”
For loop
การวนลูปค าใน String
![Page 182: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/182.jpg)
text = “Today is Sunday. I stay at home and keep coding. Today is Monday. I stay at office and keep cleaning.”splitted_text = text.split() print(splitted_text)
For loop
การวนลูปค าใน String
[‘Today’, ‘is’, ’Sunday.’, ‘I’, ‘stay’, ‘at’, ‘home’,‘and’, ‘keep’, ‘coding.’, ‘Today’, ‘is’, ‘Monday.’, ‘I’, ‘stay’, ‘at’, ‘office’, ‘and’, ‘keep’, ‘cleaning.’]
![Page 183: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/183.jpg)
Question
![Page 184: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/184.jpg)
text = “Today is Sunday. I stay at home and keep coding. Today is Monday. I stay at office and keep cleaning.” splitted_text = text.split() print(splitted_text) print(splitted_text[2])print(splitted_text[300])
For loop
การวนลูปค าใน String
![Page 185: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/185.jpg)
For loop
การวนลูปค าใน String
[‘Today’, ‘is’, ‘Sunday.’, ‘I’, ‘stay’, ‘at’, ‘home’, ‘and’, ‘keep’, ‘coding.’, ‘Today’, ‘is’, ‘Monday.’,‘I’, ‘stay’, ‘at’, ‘office’, ‘and’, ‘keep’, ‘cleaning.’] Sunday Traceback (most recent call last):File ..., line 5, in <module>
![Page 186: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/186.jpg)
text = .Today is Sunday. I stay at home and keep coding. Today is Monday. I stay at office and keep cleaning.” splitted_text = text.split() for word in splitted_text:
print(word)
For loop
การวนลูปค าใน String
![Page 187: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/187.jpg)
For loop
การวนลูปค าใน String
Today is Sunday......keep cleaning.
![Page 188: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/188.jpg)
for i in range(5) :print(i)
For loop
การวนลูป for กับ range()
01234
![Page 189: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/189.jpg)
Question
![Page 190: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/190.jpg)
จงแสดงเลขคู่ในช่วง 0 ถึง 10 (รวม 10 ด้วย)
ลองดูซิ…….
![Page 191: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/191.jpg)
for i in range(11): If (i % 2 == 0) :
print(i)
ลองดูซิ…….
การวนลูป for กับ range()
02….….10
![Page 192: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/192.jpg)
input
![Page 193: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/193.jpg)
name = input(“Please enter your name: “) print(“Your name is “ + name)
Input String
Please enter your name:
![Page 194: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/194.jpg)
num = input (“Please enter a number: ") print(num + 5)
Input Integer
Please enter a number: 10<enter> Traceback (most recent call last): File ..., line 2, in <module>print(num + 5) TypeError: Can't convert 'int' object to str implicitly
![Page 195: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/195.jpg)
num = input("Please enter a number: ") print(int(num) + 5)
Input Integer
Please enter a number: 10<enter> 15
![Page 196: โครงการ Public Training หลักสูตรอบรมการเขียนโปรแกรม ... · 6. ตัวด าเนินการทางคณิตศาสตร์](https://reader035.vdocuments.us/reader035/viewer/2022071000/5fbc4dcf462cc9782a208d6b/html5/thumbnails/196.jpg)
num = int(input("Please enter a number: ")) print(num + 5)
Input Integer
Please enter a number: 10<enter>15