Search Header Logo
การเขียนโปรแกรมไพทอน

การเขียนโปรแกรมไพทอน

Assessment

Presentation

Computers

7th - 12th Grade

Practice Problem

Hard

Created by

ครูวีรวัฒน์ ศักดิ์เพชรพลอย

Used 131+ times

FREE Resource

11 Slides • 10 Questions

1

Python List & Dict


Slide image

2

List (ลิสต์)

  • ใช้เก็บข้อมูลจำนวนมากได้

  • ใช้ตัวเลขตำแหน่ง (index) ในการระบุตำแหน่ง

  • ตำแหน่งแรกของลิสต์คือ 0

  • สามารถเก็บได้ทั้ง ตัวเลข ตัวอักษร ค่าความจริง ลิสต์ และ ดิกต์

  • ใช้กฏการกำหนดชื่อเหมือนตัวแปรอื่น

  • ใช้เครื่องหมาย [ ] ในการสร้างลิสต์

3

ตัวอย่างการใช้ List

Code >>

name = ['Penny','Ellie','Koby','Olive','Ran','Andy']

print('คนที่ 1 คือ ',name[0])

print('คนที่ 3 คือ ',name[2])

print('คนที่ 5 คือ ',name[4])


Output >>

คนที่ 1 คือ Penny

คนที่ 3 คือ Koby

คนที่ 5 คือ Ran

4

Multiple Choice

name = ['Penny','Ellie','Koby','Olive','Ran','Andy']

คำถาม ถ้าต้องการให้โปรแกรมแสดงผลว่า Ran จะต้องใช้คำสั่งในข้อใด

1

print(5)

2

print(4)

3

print(name[5])

4

print(name[4])

5

Dict (ดิกต์)

  • ใช้เก็บข้อมูลจำนวนมากได้

  • ใช้ key ในการระบุตำแหน่ง

  • สามารถแทน key เป็น ตัวเลข ตัวอักษร ค่าความจริง ได้

  • สามารถเก็บได้ทั้ง ตัวเลข ตัวอักษร ค่าความจริง ลิสต์ และ ดิกต์

  • ใช้กฏการกำหนดชื่อเหมือนตัวแปรอื่น

  • ใช้เครื่องหมาย { } ในการสร้างดิกต์

6

ตัวอย่างการใช้ Dict

Code >>

student1 = {'id' : 9 , 'name' : 'Misty Lake'}

student2 = {'id' : 8 , 'name' : 'Scarlet Devil'}

print('รหัสนักเรียน', student1['id'], ' ชื่อ ', student9['name'])

print('รหัสนักเรียน', student2['id'], ' ชื่อ ', student8['name'])


Output >>

รหัสนักเรียน 9 ชื่อ Misty Lake

รหัสนักเรียน 8 ชื่อ Scarlet Devil

7

Multiple Choice

ถ้านักเรียนต้องการสร้าง Dict นักเรียนควรใช้สัญลักษณ์ใด

1

[ ]

2

( )

3

{ }

4

" "

8

Multiple Choice

ข้อใดต่อไปนี้ไม่สามารถใช้เป็น key ของ dict ได้

1

"Bob"

2

20

3

[20]

4

False

9

ตัวอย่างการใช้ List & Dict

Code >>

student = []

student.append( {'id' : 9 , 'name' : 'Misty Lake'} )

student.append( {'id' : 8 , 'name' : 'Scarlet Devil'} )

print('รหัสนักเรียน', student[0]['id'], ' ชื่อ ', student[0]['name'])

print('รหัสนักเรียน', student[1]['id'], ' ชื่อ ', student[1]['name'])


Output >>

รหัสนักเรียน 9 ชื่อ Misty Lake

รหัสนักเรียน 8 ชื่อ Scarlet Devil

10

Multiple Choice

snack = [{'id':1,'name':Lays},{'id':2,'name':Kitkat},{'id':3,'name':Pocky}]

ในลิสต์สินค้ามีขนมอยู่ 3 ชนิด ถ้าต้องการสั่ง Kitkat จะต้องสั่งสินค้าลำดับที่เท่าไร

1

0

2

1

3

2

11

Multiple Choice

snack = [{'id':1,'name':Lays},{'id':2,'name':Kitkat},{'id':3,'name':Pocky}]

จากข้อที่แล้วถ้าอยากได้ ชื่อสินค้า จะต้องเขียนโค๊ดตามข้อใด

1

snake[1]{'name'}

2

snack[1]['name']

3

snack[2]{'name'}

4

snack[2]{'name'}

12

การใช้ append & sort

Code >>

number = []

number.append(90)

number.append(65)

number.append(70)

number.sort()

print(number)

Output >>

[65,70,90]

13

การกำหนดค่าหลายตัวแปรโดยใช้ List

Code >>

number = []

number.append(90)

number.append(65)

number.append(70)

x,y,z = number

print('x = ',x,' y = ',y,' z = ',z)

Output >>

x = 90 y = 65 z = 70

14

การใช้ pop

Code >>

number = [60,70,90,100]

number.pop(2)

x,y,z = number

print('x = ',x,' y = ',y,' z = ',z)


Output >>

x = 65 y = 70 z = 100

15

การใช้ pop

Code >>

number = [60,70,90,100]

number.pop(2)

x,y,z = number

print(number.pop(2))

print(number)


Output >>

100

[60, 70]

16

การใช้ insert

Code >>

number = [60,90]

number.insert(1,70)

x,y,z = number

print(number)


Output >>

[60, 70 ,90]

17

Multiple Choice

2 + 3 + 5 x 0 + 5 x 2 + 10 x 0 = ?

1

5

2

10

3

15

4

0

18

Multiple Select

คำสั่งใดเป็นคำสั่งสำหรับเพิ่มค่าใน list (ตอบได้หลายตัวเลือก)

1

append

2

pop

3

insert

4

sort

19

Fill in the Blanks

Type answer...

20

Multiple Select

li = []

li.append(1,"Hello")

print("Hi")

print(li[1])

จากโค๊ดจะเกิด error บรรทัดใดบ้าง (เลือกตอบได้หลายบรรทัด)

1

1

2

2

3

3

4

4

5

ไม่มี Error

21

Multiple Choice

วันนี้เป็นวัน

1

ปีใหม่

2

สงกรานต์

3

พุธ

4

พุทธ

Python List & Dict


Slide image

Show answer

Auto Play

Slide 1 / 21

SLIDE