Python has a built-in function called type() that helps you find the class type of the variable given as input. For example, if the input is a string, you will get the output as , for the list, it will be , etc.
Dengan menggunakan perintah type(), Anda dapat melewatkan satu argumen, dan nilai yang dikembalikan akan menjadi tipe kelas dari argumen yang diberikan, misalnya. jenis (objek)
Dimungkinkan juga untuk memberikan tiga argumen ke type(), i. e. , type(name, bases, dict), dalam kasus seperti itu, ini akan mengembalikan Anda objek tipe baru
Dalam tutorial ini, Anda akan belajar
Sintaks untuk tipe()
type() dapat digunakan dalam dua cara seperti yang ditunjukkan di bawah ini
type(object) type(namr, bases, dict)Parameter. jenis (objek)
- obyek. Ini adalah parameter wajib. Jika ini hanya parameter yang diteruskan ke type(), maka itu akan mengembalikan Anda jenis parameternya
Parameter. jenis (nama, basis, dict)
- nama. nama kelas
- basis. (opsional). Ini adalah parameter opsional, dan ini adalah kelas dasar
- dikt. (opsional). Ini adalah parameter opsional, dan ini adalah ruang nama yang memiliki definisi kelas
Nilai Pengembalian
Jika objek adalah satu-satunya parameter yang diteruskan ke type() maka itu akan mengembalikan tipe objek kepada Anda
Jika params yang diteruskan ke tipe adalah tipe (objek, basis, dict), dalam kasus seperti itu, ia akan mengembalikan tipe objek baru
Contoh tipe()
Dalam contoh ini kita memiliki nilai string, angka, nilai float, angka kompleks, list, tuple, dict dan set. Kami akan menggunakan variabel dengan tipe untuk melihat output masing-masing
str_list = "Welcome to Guru99" age = 50 pi = 3.14 c_num = 3j+10 my_list = ["A", "B", "C", "D"] my_tuple = ("A", "B", "C", "D") my_dict = {"A":"a", "B":"b", "C":"c", "D":"d"} my_set = {'A', 'B', 'C', 'D'} print("The type is : ",type(str_list)) print("The type is : ",type(age)) print("The type is : ",type(pi)) print("The type is : ",type(c_num)) print("The type is : ",type(my_list)) print("The type is : ",type(my_tuple)) print("The type is : ",type(my_dict)) print("The type is : ",type(my_set)) _Keluaran
The type is :<class 'str'> The type is :<class 'int'> The type is :<class 'float'> The type is :<class 'complex'> The type is :<class 'list'> The type is :<class 'tuple'> The type is :<class 'dict'> The type is :<class 'set'>Contoh. Menggunakan type() untuk objek kelas
Saat Anda memeriksa objek yang dibuat dari kelas menggunakan type(), ia mengembalikan tipe kelas beserta nama kelasnya. Dalam contoh ini, kita akan membuat kelas dan memeriksa jenis objek yang dibuat dari pengujian kelas
class test: s = 'testing' t = test() print(type(t))Keluaran
<class '__main__.test'> _Contoh. Menggunakan nama, basis, dan dict di type()
Jenis dapat juga disebut menggunakan sintaks. jenis (nama, basis, dict)
Tiga parameter diteruskan ke type()i. e. , name, bases dan dict adalah komponen yang membentuk definisi kelas. Nama mewakili nama kelas, basis adalah kelas dasar, dan dict adalah kamus atribut kelas dasar
Dalam contoh ini, kita akan menggunakan ketiga parameter i. e name, bases, dan dict in type()
Contoh
Keluaran
<class 'type'> {'x': 'Hello World', 'y': 50, '__module__': '__main__', '__doc__': None} _Saat Anda meneruskan ketiga argumen ke type() , ada baiknya Anda menginisialisasi kelas baru dengan atribut kelas dasar
Apa itu isinstance() dengan Python?
Python isinstance adalah bagian dari fungsi bawaan python. Python isinstance() menerima dua argumen, dan mengembalikan true jika argumen pertama adalah instance dari classinfo yang diberikan sebagai argumen kedua
Sintaks isinstance()
isinstance(object, classtype)Parameter
- obyek. Objek yang instance-nya Anda bandingkan dengan classtype. Ini akan mengembalikan true jika jenisnya cocok jika tidak salah
- tipe kelas. Tipe atau kelas atau tupel tipe dan/atau kelas
Nilai pengembalian
Ini akan mengembalikan nilai true jika objek adalah instance dari classtype dan false jika tidak
Contoh isinstance()
Pada bagian ini, kita akan mempelajari berbagai contoh untuk mempelajari isinstance()
Contoh. isinstance() Pemeriksaan bilangan bulat
Kode di bawah membandingkan nilai integer 51 dengan tipe int. Itu akan mengembalikan true itu tipe 51 cocok dengan int jika tidak salah
age = isinstance(51,int) print("age is an integer:", age) _Keluaran
age is an integer: True _Contoh. isinstance() Pemeriksaan mengambang
Dalam contoh ini kita akan membandingkan nilai float dengan tipe float i. e. 3. Nilai 14 akan dibandingkan dengan tipe float
Keluaran
str_list = "Welcome to Guru99" age = 50 pi = 3.14 c_num = 3j+10 my_list = ["A", "B", "C", "D"] my_tuple = ("A", "B", "C", "D") my_dict = {"A":"a", "B":"b", "C":"c", "D":"d"} my_set = {'A', 'B', 'C', 'D'} print("The type is : ",type(str_list)) print("The type is : ",type(age)) print("The type is : ",type(pi)) print("The type is : ",type(c_num)) print("The type is : ",type(my_list)) print("The type is : ",type(my_tuple)) print("The type is : ",type(my_dict)) print("The type is : ",type(my_set)) _1Contoh. isinstance() Pemeriksaan string
str_list = "Welcome to Guru99" age = 50 pi = 3.14 c_num = 3j+10 my_list = ["A", "B", "C", "D"] my_tuple = ("A", "B", "C", "D") my_dict = {"A":"a", "B":"b", "C":"c", "D":"d"} my_set = {'A', 'B', 'C', 'D'} print("The type is : ",type(str_list)) print("The type is : ",type(age)) print("The type is : ",type(pi)) print("The type is : ",type(c_num)) print("The type is : ",type(my_list)) print("The type is : ",type(my_tuple)) print("The type is : ",type(my_dict)) print("The type is : ",type(my_set)) _2Keluaran
str_list = "Welcome to Guru99" age = 50 pi = 3.14 c_num = 3j+10 my_list = ["A", "B", "C", "D"] my_tuple = ("A", "B", "C", "D") my_dict = {"A":"a", "B":"b", "C":"c", "D":"d"} my_set = {'A', 'B', 'C', 'D'} print("The type is : ",type(str_list)) print("The type is : ",type(age)) print("The type is : ",type(pi)) print("The type is : ",type(c_num)) print("The type is : ",type(my_list)) print("The type is : ",type(my_tuple)) print("The type is : ",type(my_dict)) print("The type is : ",type(my_set)) _3Contoh. isinstance() pemeriksaan Tuple
Kode memeriksa tuple (1,2,3,4,5) dengan tipe tuple. Ini akan mengembalikan nilai true jika input yang diberikan bertipe tuple dan false jika tidak
str_list = "Welcome to Guru99" age = 50 pi = 3.14 c_num = 3j+10 my_list = ["A", "B", "C", "D"] my_tuple = ("A", "B", "C", "D") my_dict = {"A":"a", "B":"b", "C":"c", "D":"d"} my_set = {'A', 'B', 'C', 'D'} print("The type is : ",type(str_list)) print("The type is : ",type(age)) print("The type is : ",type(pi)) print("The type is : ",type(c_num)) print("The type is : ",type(my_list)) print("The type is : ",type(my_tuple)) print("The type is : ",type(my_dict)) print("The type is : ",type(my_set)) _4Keluaran
str_list = "Welcome to Guru99" age = 50 pi = 3.14 c_num = 3j+10 my_list = ["A", "B", "C", "D"] my_tuple = ("A", "B", "C", "D") my_dict = {"A":"a", "B":"b", "C":"c", "D":"d"} my_set = {'A', 'B', 'C', 'D'} print("The type is : ",type(str_list)) print("The type is : ",type(age)) print("The type is : ",type(pi)) print("The type is : ",type(c_num)) print("The type is : ",type(my_list)) print("The type is : ",type(my_tuple)) print("The type is : ",type(my_dict)) print("The type is : ",type(my_set)) _5Contoh. isinstance() Tetapkan centang
Kode memeriksa satu set ({1,2,3,4,5}, dengan tipe set. Ini akan mengembalikan nilai true jika input yang diberikan bertipe set dan false jika tidak
str_list = "Welcome to Guru99" age = 50 pi = 3.14 c_num = 3j+10 my_list = ["A", "B", "C", "D"] my_tuple = ("A", "B", "C", "D") my_dict = {"A":"a", "B":"b", "C":"c", "D":"d"} my_set = {'A', 'B', 'C', 'D'} print("The type is : ",type(str_list)) print("The type is : ",type(age)) print("The type is : ",type(pi)) print("The type is : ",type(c_num)) print("The type is : ",type(my_list)) print("The type is : ",type(my_tuple)) print("The type is : ",type(my_dict)) print("The type is : ",type(my_set)) _6Keluaran
str_list = "Welcome to Guru99" age = 50 pi = 3.14 c_num = 3j+10 my_list = ["A", "B", "C", "D"] my_tuple = ("A", "B", "C", "D") my_dict = {"A":"a", "B":"b", "C":"c", "D":"d"} my_set = {'A', 'B', 'C', 'D'} print("The type is : ",type(str_list)) print("The type is : ",type(age)) print("The type is : ",type(pi)) print("The type is : ",type(c_num)) print("The type is : ",type(my_list)) print("The type is : ",type(my_tuple)) print("The type is : ",type(my_dict)) print("The type is : ",type(my_set)) _7Contoh. pemeriksaan daftar isinstance()
Kode memeriksa daftar [1,2,3,4,5], dengan daftar tipe. Itu akan mengembalikan true jika input yang diberikan adalah tipe list dan false jika tidak
str_list = "Welcome to Guru99" age = 50 pi = 3.14 c_num = 3j+10 my_list = ["A", "B", "C", "D"] my_tuple = ("A", "B", "C", "D") my_dict = {"A":"a", "B":"b", "C":"c", "D":"d"} my_set = {'A', 'B', 'C', 'D'} print("The type is : ",type(str_list)) print("The type is : ",type(age)) print("The type is : ",type(pi)) print("The type is : ",type(c_num)) print("The type is : ",type(my_list)) print("The type is : ",type(my_tuple)) print("The type is : ",type(my_dict)) print("The type is : ",type(my_set)) _8Keluaran
str_list = "Welcome to Guru99" age = 50 pi = 3.14 c_num = 3j+10 my_list = ["A", "B", "C", "D"] my_tuple = ("A", "B", "C", "D") my_dict = {"A":"a", "B":"b", "C":"c", "D":"d"} my_set = {'A', 'B', 'C', 'D'} print("The type is : ",type(str_list)) print("The type is : ",type(age)) print("The type is : ",type(pi)) print("The type is : ",type(c_num)) print("The type is : ",type(my_list)) print("The type is : ",type(my_tuple)) print("The type is : ",type(my_dict)) print("The type is : ",type(my_set)) _9Contoh. isinstance() pemeriksaan dict
Kode memeriksa dict({“A”. "a", "B". "b", "C". "c", "D". ”d”}, dengan tipe dict. Ini akan mengembalikan true jika input yang diberikan bertipe dict dan false jika tidak
The type is :<class 'str'> The type is :<class 'int'> The type is :<class 'float'> The type is :<class 'complex'> The type is :<class 'list'> The type is :<class 'tuple'> The type is :<class 'dict'> The type is :<class 'set'> 0Keluaran
The type is :<class 'str'> The type is :<class 'int'> The type is :<class 'float'> The type is :<class 'complex'> The type is :<class 'list'> The type is :<class 'tuple'> The type is :<class 'dict'> The type is :<class 'set'> 1Contoh. isinstance() tes pada kelas
Kode menunjukkan pemeriksaan tipe kelas dengan isinstance(). Objek kelas dibandingkan dengan nama kelas di dalam isinstance(). Itu mengembalikan benar jika objek milik kelas dan salah sebaliknya