ชนิดข้อมูลของตัวแปร

Python เบื้องต้นสำหรับการเงิน

Adina Howe

Professor

ชนิดข้อมูลใน Python

ชนิดตัวแปร ตัวอย่าง
Strings 'hello world'
Integers 40
Floats 3.1417
Booleans True or False
Python เบื้องต้นสำหรับการเงิน

ชนิดของตัวแปร

ชนิดตัวแปร ตัวอย่าง ตัวย่อ
Strings 'Tuesday' str
Integers 40 int
Floats 3.1417 float
Booleans True or False bool
Python เบื้องต้นสำหรับการเงิน

ตรวจสอบชนิดข้อมูลของตัวแปรด้วย type()

ใช้ฟังก์ชัน type() เพื่อตรวจสอบชนิดของตัวแปร:

type(variable_name)
pe_ratio = 40
print(type(pe_ratio))
<class 'int'>
Python เบื้องต้นสำหรับการเงิน

การจัดการตัวแปร

x = 5
print(x * 3)
15
print(x + 3)
8
y = 'stock'
print(y * 3)
'stockstockstock'
print(y + 3)
TypeError: must be str, not int
Python เบื้องต้นสำหรับการเงิน

การแปลงชนิดของตัวแปร

pi = 3.14159
print(type(pi))
<class 'float'>
pi_string = str(pi)
print(type(pi_string))
<class 'str'>
print('I love to eat ' + pi_string + '!')
I love to eat 3.14159!
Python เบื้องต้นสำหรับการเงิน

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

Python เบื้องต้นสำหรับการเงิน

Preparing Video For Download...