Tipe Data Variabel

Pengantar Python untuk Keuangan

Adina Howe

Instructor

Tipe Data Python

Tipe Variabel Contoh
String 'hello world'
Integer 40
Float 3.1417
Boolean True atau False
Pengantar Python untuk Keuangan

Tipe Variabel

Tipe Variabel Contoh Singkatan
String 'Tuesday' str
Integer 40 int
Float 3.1417 float
Boolean True atau False bool
Pengantar Python untuk Keuangan

Tipe data variabel: type()

Untuk mengetahui tipe, gunakan fungsi type():

type(variable_name)
pe_ratio = 40
print(type(pe_ratio))
<class 'int'>
Pengantar Python untuk Keuangan

Manipulasi variabel

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
Pengantar Python untuk Keuangan

Mengubah tipe variabel

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!
Pengantar Python untuk Keuangan

Ayo berlatih!

Pengantar Python untuk Keuangan

Preparing Video For Download...