2.1
2.1
วิชา การเขียนโปรแกรม Python กลุ่มสาระการเรียนรู้ วิทยาศาสตร์และเทคโนโลยี
เรื่อง ตัวแปรและชนิดข้อมูล (Variables and Data types)
ตัวแปร (Variables) คือ ชื่อที่ถูกกำหนดขึ้นเพื่อใช้เก็บค่าในหน่วยความจำสำหรับนำไปใช้งานในโปรแกรม ซึ่งอาจจะถูกใช้จากการรับข้อมูล เก็บค่าคงที่ ข้อความ หรือผลลัพธ์การทำงาน ในการกำหนดชื่อตัวแปรต้องเป็นชื่อที่ไม่ตรงกับคำสงวน
ในภาษา Python นั้นสนับสนุนการกำหนดค่าให้กับตัวแปรจะใช้เครื่องหมาย = ในการกำหนดค่าของตัวตัวแปร และนอกจากนั้นเรายังสามารถกำหนดค่าใหม่ให้กับตัวแปรได้ตลอดเวลา
# Example2101
x = 20 #กำหนดให้ x เป็นจำนวนเต็ม
y = 1.25 #กำหนดให้ y เป็นทศนิยม
z = 'Hello World' #กำหนดให้ z เป็นข้อความ string
z = 25 #กำหนดให้ z เป็นจำนวนเต็ม (เปลี่ยนจากเดิมที่เคยเป็น string)
นอกจากการกำหนดค่าแบบปกติแล้ว เรายังสามารถกำหนดค่าตัวแปรหลายตัวได้ในคำสั่งเดียว
การกำหนดค่าตัวแปลหลายตัวได้ในคำสั่งเดียว
# Example2102
a, b, c = 25, 7.21, 'Python'
d = e = 'Hello'
คำสั่งแสดงค่าที่อยู่ในแต่ละตัวแปร
print("a = ",a)
print("b = ",b)
print("c = ",c)
print("d = ",d)
print("e = ",e)
ชนิดของข้อมูล (Data types) พื้นฐานในภาษาไพธอน แบ่งออกเป็น 7 ชนิดใหญ่ ๆ ได้แก่ Number, Boolean, String, List, Tuple, Set, dictionary ซึ่งเป็นตัวแปรทั่ว ๆ ไปแต่ภาษาไพธอนยอมให้มีตัวแปร list, tuple, dictionary ที่ผสมกันได้ เรียกว่าชนิด complex ถ้าหากต้องการทราบว่าตัวแปรที่ประกาศใช้นั้นเป็นชนิดใด เราสามารถสอบถามชนิดตัวแปรได้จากคำสั่ง type(var)
Number ข้อมูลที่เป็นตัวเลขตัวเลข ซึ่งจะแบ่งออกเป็น Integer Float และ Complex
num1=5
num2=2.5
num3=2+5j
Boolean เป็นข้อมูลที่ประกอบไปด้วย 2 ค่า คือ True และ False
y=True
String เป็นข้อมูลตัวอักขระหลายตัวที่เรียงต่อกันอยู่ และจะอยู่ในเครื่องหมาย double quote หรือ single quote
Create String
str1=''
str1='Happy'
str2='Hello World'
Read String
str2='Hello World'
str2[3]
str2[1:5]
str2[2:]
str2[-6:-1]
String + replace
str2='Hello World'
str2.replace('l','Y')
String + len
str2='Hello World'
len(str2)
String + in
str2='Hello World'
'H' in str2
String + split
str2='Hello World'
str2.split()
String + Concatenation
str2='Hello World'
str2='Hello Thailand'
str1+str2
การแปลงชนิดของข้อมูล (data type conversion)
เราสามารถแปลงชนิดข้อมูล เป็นการแปลงชนิดของข้อมูลได้จากฟังก์ชันที่มีชื่อเดียวกันกับชนิดขอข้อมูลที่ต้องการ เช่น ฟังก์ชัน str() ใช้แปลงข้อมูลเป็น string หรือ int() ใช้แปลงข้อมูลเป็นเลขจำนวนเต็ม หรือ float() ให้แปลงข้อมูลเป็นเลขจำนวนจริง
# Example2103
>>> a = 10
>>> str(a)
'10'
>>> b = '257'
>>> int(b)
257
>>> c = 12
>>> float(c)
12.0
>>> d = 8.56
>>> int(d)
8
List เป็นข้อมูลที่มีรูปแบบการเก็บข้อมูลเป็นชุดที่เรียงต่อกันข้อมูลทั้งหมดจะอยู่ในเครื่องหมาย [] สามารถได้หลายค่าในตัวแปรเดียวกันไม่ว่าจะเป็นข้อมูลชนิดเดียวกันหรือต่างกัน
Create List
list1=[]
list2=[1, 2, 3, 'a', 'b', 'c']
Read List
list1=[1, 2, 3, 'a', 'b', 'c']
list1[3]
list1[:5]
list1[1:4]
list1[-5:]
Update List
list1=[1, 2, 3, 'a', 'b', 'c']
list1.append('d')
list1=[1, 2, 3, 'a', 'b', 'c']
list1[0]=5 # 0 -> index
list1=[1, 2, 3, 'a', 'b', 'c']
list2=[4, 5, 6, 'D', 'E', 'F']
list1.extend(list2)
list1=[1, 2, 3, 'a', 'b', 'c']
list1.insert(1,'yotin') # 1 -> index
list1.insert(3,'mono') # 3 -> index
Delete List
list1=[1, 2, 3, 'a', 'b', 'c']
del list1[2] # 2 -> index
del list1[-2] # -2 -> index
list1.remove('a')
list1.pop() # ลบข้อมูลตัวสุดท้าย List
list1.clear()
List + sort
list1=[1, 5, 3,7, 8, 6, 2, 4]
list1.sort()
list1.sort(reverse=True)
nlist=list1.sorted()
nlist=sorted(list1, reverse=True)
List + len
list1=[1, 2, 3, 'a', 'b', 'c']
len(list1)
num=len(list1)
List + in
list1=[1, 2, 3, 'a', 'b', 'c']
3 in list1
'd' in list1
Tuple เป็นข้อมูลที่มีรูปแบบการเก็บข้อมูลเหมือนกับ List ซึ่งข้อมูลทั้งหมดจะอยู่ในเครื่องหมาย () แต่จะแตกต่างจาก List คือไม่เราสามารถเปลี่ยนแปลงข้อมูลใน Tuple ได้
Create Tuple
tuple1=(1, 2, 3, 'a', 'b', 'c')
Read Tuple
tuple1=(1, 2, 3, 'a', 'b', 'c')
tuple[2]
tuple[1:4]
tuple[-5:-1]
Tuple + len
tuple1=(1, 2, 3, 'a', 'b', 'c')
len(tuple1)
num=len(tuple)
Tuple + in
tuple1=(1, 2, 3, 'a', 'b', 'c')
'b' in tuple1
Set เป็นข้อมูลที่ถูกเก็บเป็นชุดข้อมูลที่ไม่ซ้ำกัน โดยข้อมูลทั้งหมดจะอยู่ในเครื่องหมาย { }
Create Set
set1=set()
set2={1, 2, 3, 4, 5, 6, 7, 8, 9}
set_a = {1, 2, 3, 4, 5, 'a', 'b', 'c','f'}
Read Set
set_a = {1, 2, 3, 4, 5, 'a', 'b', 'c', 'f'}
for x in set_a:
print(x)
Update Set
set_a = {1, 2, 3, 4, 5, 'a', 'b', 'c', 'f'}
set_a.add(8)
set_a.add('z')
set_a.update({9,10,'y'})
Delete Set
set_a = {1, 2, 3, 4, 5, 'a', 'b', 'c', 'f'}
set_a.remove('a')
set_a.clear()
Set + len
set_a = {1, 2, 3, 4, 5, 'a', 'b', 'c', 'f'}
len(set_a)
Set + in
set_a = {1, 2, 3, 4, 5, 'a', 'b', 'c', 'f'}
1 in set_a
Set + Union
set_a = {1, 2, 3, 4, 5}
set_b = {3, 4, 5, 6, 7, 8}
set_a | set_b
(สมาชิกทั้งหมดที่ set_a กับ set_b)
Set + Intersection
set_a = {1, 2, 3, 4, 5}
set_b = {3, 4, 5, 6, 7, 8}
set_a & set_b
(สมาชิกที่มีทั้งใน set_a และ set_b )
Set + Difference
set_a = {1, 2, 3, 4, 5}
set_b = {3, 4, 5, 6, 7, 8}
set_a - set_b
(สมาชิกที่มีใน set_a แต่ไม่มีใน set_b)
Set + Symmetric Difference
set_a = {1, 2, 3, 4, 5}
set_b = {3, 4, 5, 6, 7, 8}
set_a ^ set_b
(สมาชิกทั้งหมดของทั้ง 2 set - สมาชิกที่มีในทั้ง 2 set)
Dictionary เป็นข้อมูลที่ถูกเก็บไว้เป็นรายการโดยแต่ละรายการจะมีคู่ของค่า key และ value ซึ่งในแต่ละรายการข้อมูล key จะต้องไม่ซ้ำกัน ข้อมูลแบบ dictionary จะในเครื่องหมาย { }
Create Dictionary
a={}
dic1 = {1 : 'Hello', 2 : 'Python', 3 : 'tutorial', 4 : 'Never Give Up'}
dic2 = {'firstname' : 'Yotin', 'lastname' : 'Siriaey', 'age' : 45, 'food' : 'rice'}
Read Dictionary
dic1 = {'firstname' : 'Yotin', 'lastname' : 'Siriaey', 'age' : 45, 'food' : 'rice'}
dic1['firstname']
dic1['age']
Update Dictionary
dic1 = {'firstname' : 'Yotin', 'lastname' : 'Siriaey', 'age' : 45, 'food' : 'rice'}
dic1['age']=47
dic1['blood_group']='AB'
dic1.update({'age':46, 'love':'Python'})
Delete Dictionary
dic1 = {'firstname' : 'Yotin', 'lastname' : 'Siriaey', 'age' : 45, 'food' : 'rice'}
del dic1['firstname']
dic1.clear()
Dictionary + len
dic1 = {'firstname' : 'Yotin', 'lastname' : 'Siriaey', 'age' : 45, 'food' : 'rice'}
len(dic1)
len(dic1.keys())
len(dic1.values())
Dictionary + in
dic1 = {'firstname' : 'Yotin', 'lastname' : 'Siriaey', 'age' : 45, 'food' : 'rice'}
'firstname' in dic1
'firstname' in dic1.keys()
'Yotin' in dic1.values()
'Mono' in dic1.values()
ฟังก์ชันการแปลงชนิดข้อมูลชนิดอื่นๆ