

ตัวแปร
Presentation
•
Science
•
12th Grade
•
Practice Problem
•
Easy
สุภวรรณ ธิวงศ์ษา
Used 6+ times
FREE Resource
7 Slides • 2 Questions
1
Computer Science, CMU
ตัวแปรจะจองเนื้อที่ในหน่วยความจำ ขนาดหรือเนื้อที่ที่ใช ้ ขึ้นกับชนิดข้อมูล
ข้อก าหนดในการตั้งชื่อตัวแปร
1. อักษรตัวแรกต ้องเป็นตัวอักษร (A-Z หรือ a-z) หรือ เครื่องหมาย _
(Underscore) เท่านั้น
2. อักษรตัวอื่นๆ ต ้องเป็นตัวอักษร (A-Z หรือ a-z) หรือ ตัวเลข 0-9 หรือเครื่องหมาย
_ เท่านั้น
3. ห ้ามตั้งชื่อตัวแปรซ้ำกับคำสงวน (Reserved Word)
4. ห้ามมีช่องว่างภายในชื่อ
5. ตัวอักษรตัวพิมพ์เล็กและตัวพิมพ์ใหญ่ถือว่าต่างกัน
เช่น NUM , Num และ num เป็นตัวแปรคนละตัวกัน
2. ตัวแปร (variable)
204101 Introduction to Computer
7
2
Computer Science, CMU
and
exec
not
assert
finally
or
break
for
pass
class
from
continue
global
raise
def
if
return
del
import
try
elif
in
while
else
is
with
except
lambda
yield
Reserved words
204101 Introduction to Computer
8
3
Computer Science, CMU
123MyID
ผิด ขึ้นต้นด้วยตัวเลข
_ThinkBig
ถูกต้อง
mylife@CM
ผิด ห้ามใช้เครื่องหมาย @
Pin number
ผิด ห้ามเว้นวรรค
Dorm_number
ตัวที่ถูกต้อง√√
ตัวอย่างการตั้งชื่อตัวแปร
204101 Introduction to Computer
9
4
Computer Science, CMU
ตัวแปรในภาษาไพทอนเราไม่ต้องประกาศชนิดข้อมูลให้ตัวแปร
ก่อนท างานการประกาศจะเกิดขึ้นโดยอัตโนมัติเมื่อเราก าหนดค่า
ให้ตัวแปร โดยใช้เครื่องหมาย =
เช่น
score = 75
gpa = 2.85
หมายความว่า ตัวแปร score มีเป็นจ านวนเต็ม มีค่า 75
ตัวแปร gpa มีเป็นจ านวนจริง มีค่า 2.85
204101 Introduction to Computer
10
3. การก าหนดค่าให้ตัวแปร (assignment statement)
5
Computer Science, CMU
นอกจากนี้ เราสามารถกำหนดค่าในลักษณะ multiple assignment ได้ ดัง
ตัวอย่างที่ 1
a = b = c = 1
คำอธิบาย
ตัวแปร a b c เป็นจำนวนเต็มมีค่า 1
ตัวอย่างที่ 2
a, b, c = 1, 2, "john"
ค าอธิบาย
ตัวแปร
a เก็บข้อมูลจ านวนเต็มที่มีค่าเท่ากับ 1
b เก็บข้อมูลจ านวนเต็มที่มีค่าเท่ากับ 2
c เก็บข้อมูลที่เป็นข้อความ john
ตัวอย่างการกำหนดค่า
204101 Introduction to Computer
11
6
Computer Science, CMU
ชนิดข้อมูล ส าหรับการเขียนโปรแกรมเบื้องต้นที่ควรรู้จักมีดังนี้
(1) int
จำนวนเต็ม
(2) float
จำนวนจริงหรือทศนิยม
(3) boolean
บูลีน : True, False
(4) string
สตริงหรือข้อความ
(5) list
ลิสต์
4.ชนิดข้อมูล (Data type)
204101 Introduction to Computer
12
7
Computer Science, CMU
(1) int เลขจำนวนเต็ม เช่น 2 , 50 , 1009
(2) float เลขจำนวนจริงหรือ ทศนิยม เช่น 15.20 ,-21.9
(3) string คือข้อความ "สวัสดี"
Data type : int float
204101 Introduction to Computer
13
8
Multiple Choice
ตัวแปรนี้คือชนิด ?
Temp="Hello"
Int
Float
String
Boolean
9
Multiple Choice
ข้อใดต้ังชื่อตัวแปรได้ถูกต้อง
my_school
my school
my@school
123my school
Computer Science, CMU
ตัวแปรจะจองเนื้อที่ในหน่วยความจำ ขนาดหรือเนื้อที่ที่ใช ้ ขึ้นกับชนิดข้อมูล
ข้อก าหนดในการตั้งชื่อตัวแปร
1. อักษรตัวแรกต ้องเป็นตัวอักษร (A-Z หรือ a-z) หรือ เครื่องหมาย _
(Underscore) เท่านั้น
2. อักษรตัวอื่นๆ ต ้องเป็นตัวอักษร (A-Z หรือ a-z) หรือ ตัวเลข 0-9 หรือเครื่องหมาย
_ เท่านั้น
3. ห ้ามตั้งชื่อตัวแปรซ้ำกับคำสงวน (Reserved Word)
4. ห้ามมีช่องว่างภายในชื่อ
5. ตัวอักษรตัวพิมพ์เล็กและตัวพิมพ์ใหญ่ถือว่าต่างกัน
เช่น NUM , Num และ num เป็นตัวแปรคนละตัวกัน
2. ตัวแปร (variable)
204101 Introduction to Computer
7
Show answer
Auto Play
Slide 1 / 9
SLIDE
Similar Resources on Wayground
7 questions
Is handwriting obsolete?
Presentation
•
12th Grade
8 questions
checking and savings
Presentation
•
12th Grade
10 questions
THE SECOND CONDITIONAL
Presentation
•
12th Grade
5 questions
การอนุกรักษ์ทรัพยากรและสิ่งแวดล้อม
Presentation
•
12th Grade
10 questions
Review & Summary Topic 1.1
Presentation
•
12th Grade
6 questions
Warm up: Noun phrases
Presentation
•
University
5 questions
คำราชาศัพท์
Presentation
•
12th Grade
8 questions
Intro to Materials Science
Presentation
•
University
Popular Resources on Wayground
24 questions
PBIS-HGMS Day 10
Quiz
•
6th - 8th Grade
10 questions
HCS SCI 03 Summer School Review 3
Quiz
•
3rd Grade
11 questions
Home Scope
Quiz
•
7th - 8th Grade
15 questions
HCS SCI 05 Summer School Assessment 3 Review
Quiz
•
5th Grade
35 questions
Lufkin Road Middle School Student Handbook & Policies Assessment
Quiz
•
7th Grade
18 questions
Geo 11.3 Area of Circles and Sectors
Quiz
•
9th - 11th Grade