Tipe Data Python

Tipe Data Bawaan

Dalam pemrograman, tipe data adalah konsep penting.

Variabel dapat menyimpan berbagai jenis data, dan jenis yang berbeda dapat melaksanakan berbagai operasi.

Dalam kategori ini, Python secara default memiliki tipe data bawaan berikut:

Tipe data teks: str
Tipe data numerik: int, float, complex
Tipe data urutan: list, tuple, range
Tipe data peta: dict
Tipe data koleksi: set, frozenset
Tipe data boolean: bool
Tipe data biner: bytes, bytearray, memoryview

Dapatkan Tipe Data

Anda dapat menggunakan fungsi type() untuk mendapatkan tipe data obyek apapun:

Instan

Cetak tipe data variabel x:

x = 10
print(type(x))

Jalankan Instan

Pengaturan Tipe Data

Dalam Python, ketika Anda menetapkan nilai variabel, Anda mengatur tipe data:

Contoh Tipe Data Coba
x = "Hello World" str Coba
x = 29 int Coba
x = 29.5 float Coba
x = 1j complex Coba
x = ["apple", "banana", "cherry"] list Coba
x = ("apple", "banana", "cherry") tuple Coba
x = range(6) range Coba
x = {"name" : "Bill", "age" : 63} dict Coba
x = {"apple", "banana", "cherry"} set Coba
x = frozenset({"apple", "banana", "cherry"}) frozenset Coba
x = True bool Coba
x = b"Hello" bytes Coba
x = bytearray(5) bytearray Coba
x = memoryview(bytes(5)) memoryview Coba

Tentukan Tipe Data Khusus

Jika ingin menentukan tipe data, Anda dapat menggunakan konstruktur berikut:

Contoh Tipe Data Coba
x = str("Hello World") str Coba
x = int(29) int Coba
x = float(29.5) float Coba
x = complex(1j) complex Coba
x = list(("apple", "banana", "cherry")) list Coba
x = tuple(("apple", "banana", "cherry")) tuple Coba
x = range(6) range Coba
x = dict(name="Bill", age=36) dict Coba
x = set(("apple", "banana", "cherry")) set Coba
x = frozenset(("apple", "banana", "cherry")) frozenset Coba
x = bool(5) bool Coba
x = bytes(5) bytes Coba
x = bytearray(5) bytearray Coba
x = memoryview(bytes(5)) memoryview Coba