Home / Python Recap

Python Recap

You are not logged in.

If you are a current student, please Log In for full access to this page.

Table of Contents

ใน lab นี้จะเป็นการทบทวนความรู้ภาษา Python เบื้องต้น

1) Syntax Overview

เริ่มทบทวน syntax ในภาษา Python ด้วยการตอบผลลัพธ์ในการรันคำสั่งต่อไปนี้

print("Hello world!")

name = "Alice"
print(f"Hello {name}!")

homework = ["Math", "Com", "Bio", "Phy", "Chem"]
print(homework[0], homework[-1])

a = True
b = False
if a and b:
    print("AND")
elif a or b:
    print("OR")

homework = ["Math", "Com", "Bio", "Phy", "Chem"]
for subj in homework:
    print(subj, end=" ")

alien_0 = {
    'color': 'green',
    'food': 'fish',
}
print(alien_0['color'], alien_0['food'])

def square(x):
    return x**2
a = square(7)
print(a)

class Dog():
    def __init__(self, name):
        self.name = name
    
    def sit(self):
        print(f"{self.name} is sitting")

d = Dog("Bob")

print(f"Dog {d.name} is born", end=", ")
d.sit()

Checkoff 1:

อ.จะทำการสุ่มตัวอย่างโค้ดด้านบนให้นักเรียนอธิบายวิธีการทำงานของโค้ดนั้น ๆ

2) Reading Errors

top

  • การเขียนโปรแกรมแล้วเกิด error เป็นเรื่องปกติของทุกคน
  • คอมพิวเตอร์ต้องการความชัดเจนในตัวคำสั่ง การเกิด error แค่หมายความว่าคอมพิวเตอร์ยังไม่เข้าใจในตัวคำสั่ง
  • ให้มองว่า error เป็นการสื่อสารระหว่างคอมพิวเตอร์กับคนเขียนโปรแกรม เพื่อให้เกิดความชัดเจนในคำสั่ง
  • Error ในภาษา Python นั้นมีหลายแบบ เช่น
    • AttributeError
    • SyntaxError
    • TypeError
    • IndentationError
    • NameError
    • KeyError
    • อื่นๆ
  • การเข้าใจว่า Error แต่ละแบบนั้นระบุถึงความผิดพลาดประเภทใดจะช่วยทำให้เราแก้ไขโปรแกรมให้ทำงานตามที่เราต้องการได้ง่ายมากขึ้น

A = [1, 2, 3]
A[3]

print(max([1, 2, 3])
print(min([1, 2]))

int("1.30")

1 + "123"

A = {
    1: "one",
    2: "two",
    3: "three",
}
A.upper()

A = {
    1: "one",
    2: "two",
    3: "three",
}
A[4]

chicken = 4
print(checkin)

f = open("myfile.txt", "r")

for i in range(5):
print(i)

for i in range(5)
    print(i)

Checkoff 2:

อ.จะทำการสุ่มตัวอย่างโค้ดด้านบนให้นักเรียนแก้ไขโค้ดให้ทำงานได้อย่างถูกต้อง

top