การสร้างตัวแปร

Python เบื้องต้นสำหรับ Data Science

Hillary Green-Lerman

Lead Data Scientist, Looker

การกรอกรายงานสุนัขหาย

รายงานสุนัขหาย

name = "Bayes"
height = 24
weight = 75.5
Python เบื้องต้นสำหรับ Data Science

กฎการตั้งชื่อตัวแปร

  • ต้องขึ้นต้นด้วยตัวอักษร (โดยทั่วไปเป็นตัวพิมพ์เล็ก)
  • หลังตัวอักษรแรก ใช้ตัวอักษร/ตัวเลข/ขีดล่างได้
  • ห้ามมีช่องว่างหรืออักขระพิเศษ
  • ตัวพิมพ์ใหญ่-เล็กต่างกัน (my_var ต่างจาก MY_VAR)
# Valid Variables
bayes_weight
b
bayes42
# Invalid Variables
bayes-height
bayes!
42bayes
Python เบื้องต้นสำหรับ Data Science

ข้อความแสดงข้อผิดพลาด

bayes-height = 3
File "<stdin>", line 1
    bayes-height = 3
                   ^
SyntaxError: can't assign to operator
Python เบื้องต้นสำหรับ Data Science

Float และ string

  • float: แทนจำนวนเต็มหรือทศนิยม

    height = 24
    weight = 75.5
    
  • string: แทนข้อความ ประกอบด้วยตัวอักษร ตัวเลข ช่องว่าง และอักขระพิเศษได้

    name = 'Bayes'
    breed = "Golden Retriever"
    
Python เบื้องต้นสำหรับ Data Science

ข้อผิดพลาดที่พบบ่อยเกี่ยวกับ string

  • หากไม่ใส่เครื่องหมายคำพูด จะเกิด name error
owner = DataCamp
File "<stdin>", line 1, in <module>
    owner = DataCamp
NameError: name 'DataCamp' is not defined
  • หากใช้เครื่องหมายคำพูดไม่ตรงกัน จะเกิด syntax error
fur_color = "blonde'
File "<stdin>", line 1
    fur_color = "blonde'
                       ^
SyntaxError: EOL while scanning string literal
Python เบื้องต้นสำหรับ Data Science

การแสดงผลตัวแปร

name = "Bayes"
height = 24
weight = 75

print(height)
24
Python เบื้องต้นสำหรับ Data Science

มาฝึกกันเถอะ!

Python เบื้องต้นสำหรับ Data Science

Preparing Video For Download...