Important Announcement
PubHTML5 Scheduled Server Maintenance on (GMT) Sunday, June 26th, 2:00 am - 8:00 am.
PubHTML5 site will be inoperative during the times indicated!

Home Explore 1. Python Variables

1. Python Variables

Published by Krit Th. -- FsThai, 2020-05-29 03:00:32

Description: 22042107_lecture_notes_1

Keywords: python

Search

Read the Text Version

1 หลักการเขยี นโปรแกรม 13 Nov 2018 www.swk.asia © 2018 Sriworakarn College, Aj Krit Th. All Rights Reserved.

2 วศิ วกรรมซอฟท์ แวร์ SE swk.asia www.swk.asia © 2018 Sriworakarn College, Aj Krit Th. All Rights Reserved.

3 1 2 ปั ญ ห า ออกแบบ ค ว า ม ต้ อ ง ก า ร ร ะ บ บ ระบบ วิ เ ค ร า ะ ห์ วงจร 6 คู่ แ ข่ ง พั ฒ น า ซ อ ฟ์ ท แ ว ร์ 3 พั ฒ น า ดู แ ล รั ก ษ า ทดสอบระบบ ระบบ 4 5 www.swk.asia © 2018 Sriworakarn College, Aj Krit Th. All Rights Reserved.

4 ตวั แปรและตวั ดําเนนิ การ Python swk.asia www.swk.asia © 2018 Sriworakarn College, Aj Krit Th. All Rights Reserved.

High-Level Computer Programming Language 5 เขียนชดุ คําสั่ง ไพทนั อนิ เตอร์พ โปรแกรม รเี ตอร์ ทํางาน • เขยี นโปรแกรม • ทําหนา้ ทีแ่ ปลง • โปรแกรมทํางาน ภาษาไพทัน ตาม ชุดคาํ ส่งั เปน็ วัตถปุ ระสงค์ • ภาษาองั กฤษ คําสง่ั ท่ี ของผเู้ ขยี น • ชุดคําสั่ง คอมพิวเตอร์ เข้าใจ • นาํ ออกเป็น โปรแกรมเด่ียว Exe www.swk.asia © 2018 Sriworakarn College, Aj Krit Th. All Rights Reserved.

ตัวแปร (Variables) 6 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.

ตวั แปร (Variables) 7 After you define the variable userAge, your program will allocate a certain area of your computer's storage space to store this data. เคร่ืองคอมพิวเตอร์ไดท้ าํ การบนั ทกึ ตัวแปรใหมช่ ่อื userAge ลงในหน่วยความจาํ & เกบ็ ข้อมลู เรยี บรอ้ ยแล้ว You can then access and modify this data by referring to it by its name, userAge. www.swk.asia © 2018 Sriworakarn College, Aj Krit Th. All Rights Reserved.

ตวั แปร (Variables) 8 Every time you declare a new variable, you need to give it an initial value. In this example, we gave it the value 0. We can always change this value in our program later. userAge = 0 We can also define multiple variables at one go. To do that simply write userAge, userName = 30, 'peter' มีคา่ เทา่ กับ userAge = 30 userName = ‘peter’ www.swk.asia © 2018 Sriworakarn College, Aj Krit Th. All Rights Reserved.

การต้งั ชอื่ ตวั แปร (Naming) 9 A variable name in Python can only contain letters (a - z, A - B), numbers or underscores (_). However, the first character cannot be a number. Hence, you can name your variables userName, user_name or userName2 but not 2userName. username 2userName user_name userName2 www.swk.asia © 2018 Sriworakarn College, Aj Krit Th. All Rights Reserved.

การตัง้ ชอ่ื ตัวแปร (Naming) 10 In addition, there are some reserved words that you cannot use as a variable name because they already have preassigned meanings in Python. These reserved words include words like print, input, if, while etc. We’ll learn about each of them in subsequent chapters. Finally, variable names are case sensitive. username is not the ! same as userName. username ไม่เทา่ กบั userName www.swk.asia © 2018 Sriworakarn College, Aj Krit Th. All Rights Reserved.

เคร่ืองหมายเทา่ กบั และการเขยี นโปรแกรม 11 Note that the = sign in the statement userAge = 0 has a different meaning from the = sign we learned in Math. In programming, the = sign is known as an assignment operator. It means we are assigning the value on the right side of the = sign to the variable on the left. A good way to understand the statement userAge = 0 is to think of it as userAge <- 0. The statements x = y and y = x have very different meanings in programming. www.swk.asia © 2018 Sriworakarn College, Aj Krit Th. All Rights Reserved.

เครอ่ื งหมายเท่ากบั และการเขียนโปรแกรม 12 Type the following code into your IDLE editor and save it. x=5 y = 10 x=y print (\"x = \", x) print (\"y = \", y) Now run the program. You should get this output: x = 10 y = 10 www.swk.asia © 2018 Sriworakarn College, Aj Krit Th. All Rights Reserved.

เคร่ืองหมายเทา่ กับและการเขียนโปรแกรม 13 Next, modify the program by changing ONLY ONE statement: x=5 y = 10 y=x print (\"x = \", x) print (\"y = \", y) Now run the program. You should get this output: x=5 y=5 www.swk.asia © 2018 Sriworakarn College, Aj Krit Th. All Rights Reserved.

ตวั ดําเนนิ การทางคณิตศาสตร์ (Operator) 14 Besides assigning a variable an initial value, we can also perform the usual mathematical operations on variables. Basic operators in Python include +, -, *, /, //, % and ** which represent addition, subtraction, multiplication, division, floor division, modulus and exponent respectively. Example: www.swk.asia © 2018 Sriworakarn College, Aj Krit Th. All Rights Reserved.

ตวั ดําเนินการทางคณติ ศาสตร์ (Operator) 15 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.

Workshop/Quiz I 17 o ให้นักศึกษาเข้ามาทํา Workshop 1 บนระบบ E-Learning o วันพฤหสั บดีท่ี 15 พ.ย. คาบเรียน 6 o ระบบปิด Workshop เวลา 15.50 น. www.swk.asia © 2018 Sriworakarn College, Aj Krit Th. All Rights Reserved.

18 THANK YOU www.swk.asia © 2018 Sriworakarn College, Aj Krit Th. All Rights Reserved.


Like this book? You can publish your book online for free in a few minutes!
Create your own flipbook