1 หลักการเขยี นโปรแกรม 6th Jan 2018 www.swk.asia © 2018 Sriworakarn College, Aj Krit Th. All Rights Reserved.
ทบทวน SDLC 2 1 2 ปั ญ ห า ออกแบบ ค ว า ม ต้ อ ง ก า ร ร ะ บ บ ระบบ วิ เ ค ร า ะ ห์ วงจร 6 คู่ แ ข่ ง พั ฒ น า ซ อ ฟ์ ท แ ว ร์ 3 พั ฒ น า ดู แ ล รั ก ษ า ทดสอบระบบ ระบบ 4 5 www.swk.asia © 2018 Sriworakarn College, Aj Krit Th. All Rights Reserved.
ทบทวน High-Level Programming Language 3 เขียนชุดคาํ ส่งั ไพทันอินเตอร์พ โปรแกรม รเี ตอร์ ทํางาน • เขียนโปรแกรม • ทาํ หนา้ ทแ่ี ปลง • โปรแกรมทาํ งาน ภาษาไพทนั ตาม ชุดคาํ สั่งเปน็ วัตถุประสงค์ • ภาษาองั กฤษ คาํ ส่ังท่ี ของผเู้ ขยี น • ชดุ คาํ สง่ั คอมพวิ เตอร์ เข้าใจ • นาํ ออกเป็น โปรแกรมเดย่ี ว Exe www.swk.asia © 2018 Sriworakarn College, Aj Krit Th. All Rights Reserved.
ทบทวน ตัวแปร (Variables) 4 Variables are names given to data that we need to store and manipulate in our programs. For instance, suppose your program needs to store the age of a user. To do that, we can name this data userAge and define the variable userAge using the following statement. userAge = 0 www.swk.asia ตวั แปร ข้อมูล © 2018 Sriworakarn College, Aj Krit Th. All Rights Reserved.
ทบทวตนวั ดําเนนิ การทางคณติ ศาสตร์ (Operator) 5 Suppose x = 5, y = 2 Addition: x + y = Subtraction: x - y = Multiplication: x*y = Division: x/y = Floor Division: x//y = (rounds down the answer to the nearest whole number) Modulus: x%y = (gives the remainder when 5 is divided by 2) Exponent: x**y = (5 to the power of 2) Let’s try: x+=2, x-=2, x*=2 www.swk.asia © 2018 Sriworakarn College, Aj Krit Th. All Rights Reserved.
6 ประเภทตวั แปร (Data Types) Python swk.asia www.swk.asia © 2018 Sriworakarn College, Aj Krit Th. All Rights Reserved.
ประเภทตวั แปร 7 1. Integers – จํานวนเตม็ เช่น -5, -4, -3, 0, 5, 7 etc. userAge = 20 mobileNumber = 12398724 2. Float – จาํ นวนจริง (เช่น จํานวนท่ีมีเลขทศนยิ ม) เชน่ 1.234, -0.023, 12.01 userHeight = 1.82 userWeight = 67.2 www.swk.asia © 2018 Sriworakarn College, Aj Krit Th. All Rights Reserved.
ประเภทตัวแปร 8 3. String – ขอ้ ความ เช่น 'initial value' (single quotes) หรอื \"initial value\" (double quotes) userName = 'Peter’ userSpouseName = \"Janet” userAge = '30' www.swk.asia © 2018 Sriworakarn College, Aj Krit Th. All Rights Reserved.
Formatter - % Operator 9 นกั ศกึ ษาลองศึกษา Python Code ตอ่ ไปนี้ brand = 'Apple’ exchangeRate = 1.235235245 message = 'The price of this %s laptop is %d USD and the exchange rate is %4.2f USD to 1 EUR' %(brand, 1299, exchangeRate) print (message) www.swk.asia © 2018 Sriworakarn College, Aj Krit Th. All Rights Reserved.
Formatter - % Operator 10 วเิ คราะห์ Python Code… message = 'The price of this %s laptop is %d USD and the exchange rate is %4.2f USD to 1 EUR' %(brand, 1299, exchangeRate) %s -> brand -> ‘Apple’ %d -> 1299 -> ‘1299 %4.2f -> exchangeRate -> ‘1.24’ #สตริงยาว 4 ตวั อักษร มีจดุ ทศนิยม 2 ตําแหน่ง www.swk.asia © 2018 Sriworakarn College, Aj Krit Th. All Rights Reserved.
Formatter – format() method 11 นักศึกษาลองศึกษา Python Code ตอ่ ไปนี้ message = 'The price of this {0:s} laptop is {1:d} USD and the exchange rate is {2:4.2f} USD to 1 EUR'.format('Apple', 1299, 1.235235245) www.swk.asia © 2018 Sriworakarn College, Aj Krit Th. All Rights Reserved.
Formatter – format() method 12 วเิ คราะห์ Python Code… message = 'The price of this {0:s} laptop is {1:d} USD and the exchange rate is {2:4.2f} USD to 1 EUR’ .format('Apple', 1299, 1.235235245) {0:s} -> 0 คอื ตาํ แหนง่ ท่ี 1, s คือ formatter ระบุว่าเปน็ String {1:d} -> 1 คือตาํ แหนง่ ท่ี 2, d คอื formatter ระบุวา่ เป็น Integer {2:4.2f} -> 2 คือตาํ แหนง่ ที่ 3, 4.2f คอื formatter ระบุวา่ เปน็ Float www.swk.asia © 2018 Sriworakarn College, Aj Krit Th. All Rights Reserved.
Formatter – format() method 13 นกั ศกึ ษาลองศกึ ษา Python Code ต่อไปนี้ message1 = '{0} is easier than {1}'.format('Python', 'Java’) print (message1) #You’ll get 'Python is easier than Java’ message2 = '{1} is easier than {0}'.format('Python', 'Java’) print (message2) #You’ll get 'Java is easier than Python’ www.swk.asia © 2018 Sriworakarn College, Aj Krit Th. All Rights Reserved.
Formatter – format() method 14 นกั ศึกษาลองศึกษา Python Code ต่อไปน้ี message3 = '{:10.2f} and {:d}'.format(1.234234234, 12) print (message3) #You’ll get ' 1.23 and 12’ #You do not need to indicate the positions of the arguments. message4 = '{}'.format(1.234234234) print (message4) #You’ll get '1.234234234'. No formatting is done. www.swk.asia © 2018 Sriworakarn College, Aj Krit Th. All Rights Reserved.
List 15 4. List – ชดุ (Collection) ของขอ้ มลู ท่มี คี วามสมั พนั ธ์กัน จากตัวอยา่ งกอ่ นหน้า จะพบว่าเมอื่ เรามีความจาํ เปน็ ต้องใช้ตวั แปร เราจะ ประกาศตวั แปรแยกกนั เชน่ ต้องการเก็บขอ้ มลู อายผุ ใู้ ช้งาน 5 คน: user1Age, user2Age, user3Age, user4Age และ user5Age มอี กี วิธีการหนึง่ เราสามารถประกาศใช้ตัวแปรหลายๆ ตัวพรอ้ มกนั ได้โดย ใชว้ ิธีการที่เรยี กว่าประกาศตัวแปรแบบ List userAge = [21, 22, 23, 24, 25] #ใชต้ วั แปรชอ่ื userAge ตวั เดียว www.swk.asia © 2018 Sriworakarn College, Aj Krit Th. All Rights Reserved.
List 16 การเรียกใชง้ านตัวแปรประเภท List userAge = [21, 22, 23, 24, 25] userAge[0] = 21 #ตําแหนง่ ที่ 1 ในวงเลบ็ […] userAge[1] = 22 #ตําแหนง่ ท่ี 2 ในวงเล็บ […] userAge[2] = 23 #ตําแหนง่ ที่ 3 ในวงเลบ็ […] userAge[3] = 24 #ตําแหน่งท่ี 4 ในวงเล็บ […] userAge[4] = 25 #ตาํ แหนง่ ที่ 5 ในวงเล็บ […] www.swk.asia © 2018 Sriworakarn College, Aj Krit Th. All Rights Reserved.
List 17 สรปุ โครงสร้างของตัวแปรประเภท List ทม่ี สี มาชกิ N ตวั สมาชกิ ลําดับที่ สมาชิกลําดับท่ี สมาชกิ ลําดับที่ สมาชกิ ลําดบั ที่ … สมาชกิ ลาํ ดบั ที่ 1 2 3 4 N (member) ตาํ แหน่งท่ี ตําแหน่งท่ี ตําแหนง่ ท่ี ตําแหนง่ ที่ … ตําแหน่งที่ 0 2 3 4 N-1 (อนิ เดก็ ซ์) ü ตวั แปร List สามารถระบปุ ระเภทสมาชกิ ได้หลายแบบ www.swk.asia © 2018 Sriworakarn College, Aj Krit Th. All Rights Reserved.
List 18 นักศึกษาลองศึกษา Python Code ต่อไปนี้ userAge = [21, 22, 23, 24, 25] print(userAge[-1]) # 25 print(userAge[-2]) # 24 userAge2 = userAge # [21, 22, 23, 24, 25] userAge3 = userAge[2:4] userAge4 = userAge[1:5:2] userAge5 = userAge[ :4] userAge6 = userAge[1: ] www.swk.asia © 2018 Sriworakarn College, Aj Krit Th. All Rights Reserved.
List 19 เพิ่ม ลบ และ แกไ้ ข สมาชิกในตวั แปร List userAge = [21, 22, 23, 24, 25] userAge[1] = 5 # [21, 5, 23, 24, 25] userAge.append(99) # [21, 5, 23, 24, 25, 99] del userAge[2] # [21, 5, 24, 25, 99] www.swk.asia © 2018 Sriworakarn College, Aj Krit Th. All Rights Reserved.
Assignment 4.1 20 ใหน้ ักศกึ ษาเขียนคําส่ัง Python เพ่ือดาํ เนินการเกยี่ วกบั ตัวแปร List: • #สรา้ งตัวแปร List ชื่อ myList มีสมาชกิ 1, 2, 3, 4, 5, และ \"Hello\" • #พมิ พ์สมาชกิ ทุกตวั ในตวั แปร myList • #พิมพ์สมาชิกลําดบั ท่ี 3 • #พมิ พส์ มาชกิ ลําดับสุดท้าย • #คัดลอกข้อมลู ใน myList (index-อินเด็กซ์ 1 to 4) ไปยงั myList2 • #แกไ้ ขขอ้ มูลสมาชกิ ลําดบั ท่ี 2 ใน myList เปน็ 20 และ พิมพ์ myList • #เพม่ิ สมาชิกใหม่ (“How Are You?”) ไปยัง myList ตอ่ จากสมาชิก ลา่ สุด และ พิมพ์ myList • #ลบสมาชกิ ลาํ ดับที่ 6 ออกจาก myList และ พิมพ์ myList www.swk.asia © 2018 Sriworakarn College, Aj Krit Th. All Rights Reserved.
Tuple 21 5. Tuple – ชดุ (Collection) ของข้อมลู ที่มีความสมั พันธ์กนั ข้อมลู ประเภท Tuple มโี ครงสรา้ งและรปู แบบเหมอื นกับข้อมูลประเภท List ตา่ งกันทขี่ ้อมลู ประเภท Tuple ไมส่ ามารถแก้ไขข้อมูลได้ เหมาะที่จะนาํ ไปใชก้ บั ขอ้ มูลทีเ่ ป็นข้อมูลตน้ แบบหรอื ขอ้ มลู ท่ีตายตวั และใช้ ตลอดในโปรแกรม เชน่ monthsOfYear = (\"Jan\", \"Feb\", \"Mar\", \"Apr\", \"May\", \"Jun\", \"Jul\", \"Aug\", \"Sep\", \"Oct\", \"Nov\", \"Dec\") #การประกาศใช้ตวั แปรแบบ tuple จะใช้ (…) ในขณะ List จะ ใช้ […] www.swk.asia © 2018 Sriworakarn College, Aj Krit Th. All Rights Reserved.
Dictionary 22 6. Dictionary – ชุด (Collection) ข้อมูลท่ีสมาชิกมีความสัมพันธเ์ ป็นคู่ ดกิ ชนั นาร่ี คือ ชุดขอ้ มูลท่สี มาชิกเปน็ คู่ (คยี ์ และ ข้อมลู ) ยกตัวอยา่ งเชน่ หากตอ้ งการเกบ็ ข้อมลู ทีม่ ที งั้ ชื่อ และ อายุ หลายๆ คน พรอ้ มกนั ในตัว แปรเดียว เราสามารถใช้ตัวแปรประเภท Dictionary dictionaryName = {key1 : data1, key2 : data2, …, keyN : dataN} ข้อกําหนดสาํ หรบั ตวั แปรประเภท Dictionary สมาชิกคยี ์ทกุ ตวั จะตอ้ งไม่ ซา้ํ กัน (Unique) myDictionary = {\"Peter\":38, \"John\":51, \"Peter\":13} # {'Peter': 13, 'John': 51}www.swk.asia © 2018 Sriworakarn College, Aj Krit Th. All Rights Reserved.
Dictionary 23 วิธกี ารประกาศใช้งานตวั แปร Dictionary แบบที่ 1 userNameAndAge = {\"Peter\" : 38, \"John\" : 51, \"Alex\" : 13, \"Alvin\" : \"Not Available”} วธิ ีการประกาศใช้งานตัวแปร Dictionary แบบท่ี 2: Dict() Method userNameAndAge = dict(Peter = 38, John = 51, Alex = 13, Alvin = \"Not Available\") # Peter = 38, John = 51, Alex = 13, # Alvin = ’Not Available’ www.swk.asia © 2018 Sriworakarn College, Aj Krit Th. All Rights Reserved.
Dictionary 24 Dictionary Operations: การดําเนนิ การกบั ตวั แปร Dictionary userNameAndAge = {\"Peter\" : 38, \"John\" : 51, \"Alex\" : 13, \"Alvin\" : \"Not Available”} # {'Peter': 38, 'John': 51, 'Alex': 13, 'Alvin': 'Not Available’} พมิ พ์/แสดง ขอ้ มูล print(userNameAndAge[\"John\"]) # You’ll get the value 51 www.swk.asia © 2018 Sriworakarn College, Aj Krit Th. All Rights Reserved.
Dictionary 25 Dictionary Operations: การดําเนินการกบั ตัวแปร Dictionary userNameAndAge = {\"Peter\" : 38, \"John\" : 51, \"Alex\" : 13, \"Alvin\" : \"Not Available”} # {'Peter': 38, 'John': 51, 'Alex': 13, 'Alvin': 'Not Available’} แกไ้ ขขอ้ มลู userNameAndAge[\"John\"] = 21 # John has been modified to 21 www.swk.asia © 2018 Sriworakarn College, Aj Krit Th. All Rights Reserved.
Dictionary 26 Dictionary Operations: การดาํ เนินการกับตัวแปร Dictionary userNameAndAge = {\"Peter\" : 38, \"John\" : 51, \"Alex\" : 13, \"Alvin\" : \"Not Available”} # {'Peter': 38, 'John': 51, 'Alex': 13, 'Alvin': 'Not Available’} เพ่มิ ขอ้ มลู userNameAndAge[\"Joe\"] = 40 # Joe = 40 has been added # {'Peter': 38, 'John': 51, 'Alex': 13, 'Alvin': 'Not Available’, ‘Joe’: 40} www.swk.asia © 2018 Sriworakarn College, Aj Krit Th. All Rights Reserved.
Dictionary 27 Dictionary Operations: การดําเนินการกับตัวแปร Dictionary userNameAndAge = {\"Peter\" : 38, \"John\" : 51, \"Alex\" : 13, \"Alvin\" : \"Not Available”} # {'Peter': 38, 'John': 51, 'Alex': 13, 'Alvin': 'Not Available’} ลบขอ้ มูล del userNameAndAge[\"Alex\"] # Alex = 13 has been removed from Dictionary # {'Peter': 38, 'John': 51, 'Alex': 13, 'Alvin': 'Not Available’, ‘Joe’: 40} # {'Peter': 38, 'John': 51, 'Alvin': 'Not Available’, ‘Joe’: 40} www.swk.asia © 2018 Sriworakarn College, Aj Krit Th. All Rights Reserved.
Assignment 4.2 28 ใหน้ ักศกึ ษาเขียนคําสงั่ Python เพือ่ ดําเนนิ การเกย่ี วกับตัวแปร Dictionary: • #สรา้ งตวั แปร Dictionary ชอื่ myDict มีสมาชกิ {'One’ -> 1.35, 2.5 -> 'Two Point Five’, 3 -> '+', 7.9 -> 2} • #พิมพส์ มาชกิ ทกุ ตวั ในตวั แปร myDict • #พิมพส์ มาชกิ ท่ีชือ่ (Key) วา่ ‘one’ • #พมิ พส์ มาชิกทช่ี ือ่ (Key) วา่ 7.9 • #แก้ไขข้อมลู สมาชิก ทช่ี อ่ื (Key) ว่า 2.5 เป็น \"Two and a Half” • #เพ่มิ สมาชกิ ใหม่ myDict คยี ช์ ือ่ วา่ “New Item” และขอ้ มลู ”I’m new” • #ลบสมาชิกทม่ี ีคยี ์ชือ่ ว่า ‘One’ www.swk.asia © 2018 Sriworakarn College, Aj Krit Th. All Rights Reserved.
29 THANK YOU www.swk.asia © 2018 Sriworakarn College, Aj Krit Th. All Rights Reserved.
Search
Read the Text Version
- 1 - 29
Pages: