

ตัวแปร
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
6 questions
ORIE 101 - @OrtLibrary Orientation
Presentation
•
University
11 questions
Untitled Lesson
Presentation
•
12th Grade
6 questions
Introduction to ICT
Presentation
•
11th - 12th Grade
9 questions
เทคนิคในการสร้างสรรค์ผลงานภาพพิมพ์
Presentation
•
12th Grade
7 questions
สมการความต่อเนื่อง
Presentation
•
12th Grade
7 questions
Gadgets
Presentation
•
KG
7 questions
การเล่นทีมกีฬาวอลเลย์บอล
Presentation
•
12th Grade
8 questions
Procedure Text
Presentation
•
12th Grade
Popular Resources on Wayground
10 questions
Safety Day 2026 – Student Review
Quiz
•
9th Grade
21 questions
Continents and Oceans
Quiz
•
6th Grade
20 questions
Parts of Speech
Quiz
•
5th Grade
16 questions
Subject & Predicate
Quiz
•
5th Grade
25 questions
Multiplication Facts
Quiz
•
5th Grade
12 questions
Map Skills
Quiz
•
3rd Grade
22 questions
Continents and Oceans
Quiz
•
5th Grade
15 questions
Rounding Decimals
Quiz
•
5th Grade
Discover more resources for Science
7 questions
Newton's Laws Video Quiz
Interactive video
•
9th - 12th Grade
35 questions
Cell Transport/Enzyme/Cell/Macro Review
Quiz
•
9th - 12th Grade
13 questions
Explore Enzyme Functions and Interactions
Quiz
•
9th - 12th Grade
10 questions
Fact or Fiction: Microbes
Quiz
•
9th - 12th Grade
10 questions
Understand Cell Functions and Organelles
Quiz
•
9th - 12th Grade
19 questions
Explore Macromolecules in Living Organisms
Quiz
•
9th - 12th Grade
15 questions
Layers of Atmosphere passage
Passage
•
9th - 12th Grade
22 questions
Quiz Review Day 5 Water and Macromolecules
Quiz
•
9th - 12th Grade