ตัวแปรและประเภทข้อมูล

Python เบื้องต้น

Hugo Bowne-Anderson

Data Scientist at DataCamp

ตัวแปร

  • ชื่อเฉพาะ แยกตัวพิมพ์ใหญ่-เล็ก

  • เรียกใช้ค่าผ่านชื่อตัวแปร

  • 1.79 ม. - 68.7 กก.

height = 1.79
weight = 68.7

height
1.79
Python เบื้องต้น

คำนวณ BMI

height = 1.79
weight = 68.7

height
1.79

$$ \text{BMI} = \frac{\text{weight}}{\text{height}^2} $$

68.7 / 1.79 ** 2
21.4413
weight / height ** 2
21.4413
bmi = weight / height ** 2
bmi
21.4413
Python เบื้องต้น

การทำซ้ำได้

height = 1.79
weight = 68.7
bmi = weight / height ** 2
print(bmi)
21.4413
Python เบื้องต้น

การทำซ้ำได้

height = 1.79
weight = 74.2 # <-
bmi = weight / height ** 2
print(bmi)
23.1578
Python เบื้องต้น

ประเภทข้อมูลใน Python

type(bmi)
float
day_of_week = 5
type(day_of_week)
int
Python เบื้องต้น

ประเภทข้อมูลใน Python (2)

x = "body mass index"
y = 'this works too'

type(y)
str
z = True
type(z)
bool
Python เบื้องต้น

ประเภทข้อมูลใน Python (3)

2 + 3
5
'ab' + 'cd'
'abcd'
  • ประเภทต่างกัน = พฤติกรรมต่างกัน!
Python เบื้องต้น

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

Python เบื้องต้น

Preparing Video For Download...