Type Data#
Python memiliki tipe data bawaan dalam beberapa kategori:
Numeric types : integer, float, complex.
a = 100
b = 29.0
c = 1j
print(a, type(a))
print(b, type(b))
print(c, type(c))
100 <class 'int'>
29.0 <class 'float'>
1j <class 'complex'>
Boolean type: True, False.
t = True
print(t, type(t))
True <class 'bool'>
Sequence types: string, list, tuple.
x = "Hello World"
y = ["apple", "banana", "cherry"]
z = ("apple", "banana", "cherry")
print(x, type(x))
print(y, type(y))
print(z, type(z))
Hello World <class 'str'>
['apple', 'banana', 'cherry'] <class 'list'>
('apple', 'banana', 'cherry') <class 'tuple'>
Dictionary type: koleksi tidak berurutan yang memiliki pasangan key-value
j = {"name" : "Johny", "age" : 25}
print(j, type(j))
{'name': 'Johny', 'age': 25} <class 'dict'>
Set type: koleksi tak berurutan dari obyek yang unik
#nilai True dan 1 sama, dan tidak boleh ada duplikasi data
thisset = {"apple", "banana", "cherry", "apple",True,1,2}
print(thisset, type(thisset))
{True, 2, 'apple', 'banana', 'cherry'} <class 'set'>