Variabel tidak perlu dideklarasikan dengan tipe tertentu, dan bahkan dapat mengubah tipe setelah ditetapkan
Contoh
x = 4 # x bertipe int
x = "Sally" # x sekarang bertipe str
cetak(x)
Pengecoran
Jika Anda ingin menentukan tipe data dari suatu variabel, hal ini dapat dilakukan dengan casting
Contoh
x = str(3) # x akan menjadi '3'
y = int(3) # y akan menjadi 3
z = float(3) # z akan menjadi 3. 0
Belajar Memfilter Data dengan Python Seperti Analis Data
Cobalah sesi pelatihan langsung dengan panduan langkah demi langkah dari seorang ahli. Coba proyek terpandu yang dibuat bekerja sama dengan Coursera sekarang
Pada artikel ini, kita akan membahas cara melihat semua variabel yang ditentukan dengan Python. Melihat semua variabel yang ditentukan memainkan peran utama saat men-debug kode
Metode 1. Menggunakan fungsi dir()
dir() adalah fungsi bawaan untuk menyimpan semua variabel di dalam program bersama dengan fungsi dan metode variabel bawaan. Itu membuat daftar semua variabel yang dideklarasikan dan bawaan. Ada dua cara berbeda untuk melihat semua variabel yang ditentukan menggunakan dir(). Mereka dibahas di bawah ini
Ketika tidak ada variabel yang ditentukan pengguna dimulai dengan '__'
- Tentukan beberapa variabel dari berbagai jenis yang tidak dimulai dengan '__'
- Panggil dir dan simpan dalam variabel. Itu menyimpan semua nama variabel yang didefinisikan sebelumnya dalam bentuk daftar dan menyimpan nama variabel sebagai string
- Ulangi seluruh daftar tempat dir() disimpan
- Cetak item jika tidak dimulai dengan '__'
Contoh
Python3
# Define some variables of various types
# that are not starting with '__'_
var2= "Welcome to geeksforgeeks"
var3= {"1":# that are not starting with '__'0# that are not starting with '__'1# that are not starting with '__'2:# that are not starting with '__'4# that are not starting with '__'5
# that are not starting with '__'6= # that are not starting with '__'8
# that are not starting with '__'9= var21var22# that are not starting with '__'1var24# that are not starting with '__'1var26# that are not starting with '__'1_______1var28________1______1________3______0____1__3____
=2= =4=5# that are not starting with '__'1# that are not starting with '__'_3_______7=8
=_9
"Welcome to geeksforgeeks"_0
"Welcome to geeksforgeeks"_1
"Welcome to geeksforgeeks"_2
"Welcome to geeksforgeeks"_3
"Welcome to geeksforgeeks"4= "Welcome to geeksforgeeks"6"Welcome to geeksforgeeks"7
=_9
"Welcome to geeksforgeeks"_9
var30
var31 var32var33 var34
var35
var36var37
var36var39 =0 =1=2=3
=4=5= =7=8
=4{0{1{2# that are not starting with '__'1_______1{4{5{6{7
Keluaran
var2 is and is equal to Welcome to geeksforgeeks
var3 is and is equal to {‘1’: ‘a’, ‘2’: ‘b’}
var4 is and is equal to 25
var5 is and is equal to [1, 2, 3, 4, 5]
var6 is and is equal to (58, 59)
Menyimpan variabel bawaan dan mengabaikannya
- Buat variabel baru dan simpan semua fungsi bawaan di dalamnya menggunakan dir()
- Tentukan beberapa variabel dari berbagai jenis
- Sekali lagi panggil dir dan simpan dalam daftar dengan mengurangi variabel bawaan yang disimpan sebelumnya
- Ulangi seluruh daftar
- Cetak item yang diinginkan
Contoh
Python3
{_8
{_9
"1"0= "1"2=4"Welcome to geeksforgeeks"6"1"5
=_9
"1"_7
var2= "Welcome to geeksforgeeks"
var3= {"1":# that are not starting with '__'0# that are not starting with '__'1# that are not starting with '__'2:# that are not starting with '__'4# that are not starting with '__'5
# that are not starting with '__'6= # that are not starting with '__'8
# that are not starting with '__'9= var21var22# that are not starting with '__'1var24# that are not starting with '__'1var26# that are not starting with '__'1_______1var28________1______1________3______0____1__3____
=2= =4=5# that are not starting with '__'1# that are not starting with '__'_3_______7=8
=_9
# that are not starting with '__'_26
# that are not starting with '__'_27
# that are not starting with '__'_28
# that are not starting with '__'29= "1"2=4"Welcome to geeksforgeeks"6"1"5# that are not starting with '__'35 "1"0
=_9
# that are not starting with '__'_38
var31 var32var33 # that are not starting with '__'42
var35
var36# that are not starting with '__'45
var36var39 # that are not starting with '__'48= # that are not starting with '__'50:
=4# that are not starting with '__'53= =7=8
=4{0{1{2# that are not starting with '__'1# that are not starting with '__'_7_______4# that are not starting with '__'63{6# that are not starting with '__'65
Keluaran
var2 is and is equal to Welcome to geeksforgeeks
var3 is and is equal to {‘1’: ‘a’, ‘2’: ‘b’}
var6 is and is equal to (58, 59)
var4 is and is equal to 25
var5 is and is equal to [1, 2, 3, 4, 5]
Metode 2. Untuk mencetak variabel lokal dan global
Locals() adalah fungsi bawaan yang mengembalikan daftar semua variabel lokal dalam lingkup tertentu. Dan globals() melakukan hal yang sama dengan variabel global
Mendekati
- Buat daftar semua variabel global menggunakan fungsi globals() , untuk menyimpan variabel global bawaan
- Deklarasikan beberapa variabel global
- Mendeklarasikan sebuah fungsi
- Deklarasikan beberapa variabel lokal di dalamnya
- Simpan semua variabel lokal dalam daftar, menggunakan kata kunci locals
- Ulangi daftar dan cetak variabel lokal
- Simpan variabel global dalam daftar menggunakan kata kunci global dan kurangi daftar variabel global bawaan yang dibuat sebelumnya darinya
- Cetak mereka
- Panggil fungsinya
Contoh
Python3
# that are not starting with '__'_66
# that are not starting with '__'_67
# that are not starting with '__'_68
"1"0= "1"2=4# that are not starting with '__'73"1"5
=_9
# that are not starting with '__'_76
# that are not starting with '__'_77= # that are not starting with '__'79
# that are not starting with '__'80= # that are not starting with '__'82
=_9
# that are not starting with '__'_84
# that are not starting with '__'_85 # that are not starting with '__'86
var35
var36# that are not starting with '__'89
var36var2= "Welcome to geeksforgeeks"
var36var3= {"1":# that are not starting with '__'0# that are not starting with '__'1# that are not starting with '__'2:# that are not starting with '__'4# that are not starting with '__'5
var36# that are not starting with '__'6= # that are not starting with '__'8
________5______6# that are not starting with '__'9= var21var22# that are not starting with '__'1var24# that are not starting with '__'1var26# that are not starting with '__'1var28_______1_______1____0____3____1
var36=2= =4=5# that are not starting with '__'1=7=8
=_9
var36var234
var36var236
var36var238= "1"2=4var242"1"5
var2_44
var36var246
var36var248
var36{0=4var252=8
var36var31 var32var33 var258
=4# that are not starting with '__'53= =7=8
=4{0{1{2# that are not starting with '__'1# that are not starting with '__'_7_______4# that are not starting with '__'63{6# that are not starting with '__'65
=_9
var36var275
var36________2______77
var36var279
var36var281= "1"2=4# that are not starting with '__'73"1"5# that are not starting with '__'35"1"0
var2_44
var36var291
var36{0=4var295=8
var36var31 var32var33 =01
=_02
=4=04
=4=06
________3______4var39 # that are not starting with '__'48= # that are not starting with '__'50 =12 # that are not starting with '__'48= =15: