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