Skip to main content

What It's Dictionary In Python Programming

Know What It's Dictionary In Python Programming

At the material then, we've studied the list data structure in python program that is capable of storing a wide range of values ​​that has a variety of data types. List is usually used to store a variety of data collection.

But it turns out, the list has its drawbacks.

Lack of List: the list can not use keywords to access an item or the value. Can only be used by calling the index number only.

But do not worry ya ...

Weakness on the list now can be completed by the Dictionary.

On this matter, we will learn the basic things that should be known about Dictionary in python program.

The following important things:

What is a Dictionary in Python Programming?

Dictionary is a data structure that looks like a dictionary. There is a key word then there the value well. Keyword must be unique (not the same as the others), while the value may be filled with just about anything.

Example Code:
me {
    "name": "FajarYusuf",
    "url": "https://www.fajaryusuf.com"
}


In the example above we are coding to create a Dictionary called me and filled in the name and URL. name and url is the key word (key) that we will use to access the value in it.

This is the difference Dictionary compared with lists and tuples . Dictionary have keywords that can be text, and could also figure, while for lists and tuples only use an index is either a number in order to access its value.

In other programming languages (eg PHP), the Dictionary also known as associative arrays .

make Dictionary

Things that must exist in the manufacture Dictionary is:

  • name Dictionary,
  • Keywords,
  • Value,
  • Open the lid and Dictionary should use curly braces.
Among the key and value must be separated with a colon (:) and when there is more than one item or value, then separated by a comma (,).

Examples of coding an item:
name_dict = {
    "key": "value"
}
Examples of coding of three items:
name_dict = {
    "key1": "value",
    "key2": "value",
    "key3": "value"
}
The contents of the Dictionary can be the following data types:

  • String
  • Integer
  • objects
  • List
  • Tuple
  • Dictionary
  • etc.
Example Code:
FajarYusuf= {
    "name": "FajarYusuf",
    "age": 25,
    "hobby": ["coding", "nggame", "ngband"],
    "get married": True,
    "sosmed": {
        "facebook": "FajarYusuf",
        "twitter": "@cibinongcreative"
    }
}
Here are the contents of the Dictionary above:
  • name contains the string value "FajarYusuf"
  • age contains an integer with a value 25
  • hobby contains a list of strings
  • get married is boolean True
  • and sosmed contains Dictionary 

using Constructors

In addition to using the above, we also can make the Dictionary of constructor dict () with the parameter keys and values.

Example Code :
color_fruit = dict (orange = "orange", apple = "red", banana = "yellow")
It will generate the dictionary as follows:
{ 'Orange': 'orange', 'apple', 'red', 'banana', 'yellow'}

Accessing Item Values ​​of Dictionary

We already know how to create the Dictionary, now let us learn how to access them.

How to access the same as accessing the list. But the key words used are not numbers, but the keywords we have set in the Dictionary.

Example Code :
#CreateDictionary
fajaryusuf = {
    "name": "Fajar Yusuf",
    "age": 25,
    "hobby": ["coding", "nggame", "ngband"],
    "get married": True,
    "sosmed": {
    "Facebook": "Fajar Yusuf",
        "twitter": "@cibinongcreative"
    }
}
# Access the contents of the dictionary
print ("My name is %s"% fajaryusuf ["name"])
print ("Twitter: %s"% fajaryusuf ["sosmed"] ["twitter"])


Results output:

What It's Dictionary In Python Programming

In addition to the above manner, we can also access the value Dictionary with method get ().

Example Code:

#CreateDictionary
fajaryusuf= {
    "name": "FajarYusuf",
    "age": 25,
    "hobby": ["coding", "nggame", "ngband"],
    "get married": True,
    "sosmed": {
        "facebook": "FajarYusuf",
        "twitter": "@cibinongcreative"
    } 
}
# Access the contents of the dictionary
print(fajaryusuf.get("name"))
print(fajaryusuf.get("age"))

The result:

Know What It's Dictionary In Python Programming

using Recurrence

To display the output of all the contents of the Dictionary, we can use the loop like this:
# CreateDictionary
web = {
    "name": "FajarYusuf.Com",
    "url": "https://www.fajaryusuf.com",
    "rank": "5"
}
# Print the contents of the dictionary with repetition
for key in web:
    print(web[key])
The result:
Know What It's Dictionary In Python Programming

We can also do it like the following code:
web = {
    "name": "FajarYusuf.Com",https://www.fajaryusuf.com",
    "url": "
    "rank": "1"
}
for key, val in web.items():
    print("%s : %s" % (key, val))
The result:

Know What It's Dictionary In Python Programming

Changing Item Values ​​Dictionary

Dictionary is mutable, which means that its value can be changes as needed. To change the value of the Dictionary, we can do like this coding:

name_dic [ "key"] = "New Value"
Example:

# Making Online Dictionary 
skill = { 
    "key": "Python", 
    "other": [ "CSS", "JAVASCRIPT", "HTML"] 

# Print cartridge skill main 
print (skill [ "key"]) 
# edit any skill The main 
skill [ "key"] = "Ruby" 
# Print the contents of the main skill 
print (skill [ "key"])
The value of key 'key' will be changed from Python to Ruby, he will produce:
Python
Ruby


Removing items from the Dictionary

To remove the value of the Dictionary, we can use the command del and method pop ().

Method pop () is a method that serves to remove items from the dictionary while del function is a function to remove a variable from memory.

Coding example to remove it using del:
>>> del skill["main"]
>>> skill
{'other': ['PHP', 'Java', 'HTML']}

Sample Coding remove by using the method pop ():
>>> skill.pop("main")
'Rust'
>>> skill
{'other': ['PHP', 'Java', 'HTML']}

And if we want to remove all the dictionary at once, we can use the method clear ().

Example Code:
skill.clear()


Adding items to Dictionary

We can use the method update () to add content to Dictionary. Parameters such as Dictionary.

Also serves to add, this method also serves to change the dictionary when the key is inserted are built.

Example Code:
# Create dictionary user
user = {
    "name": "FajarYusuf"
}
# add password
user.update({"password": "P@ssw0rd"})
print(user)
# update name
user.update({"name": "CodingMania"})
print(user)
The result:

{'name': 'FajarYusuf', 'password': 'P@ssw0rd'}
{'name': 'CodingMania', 'password': 'P@ssw0rd'}

Taking the Long Dictionary

To retrieve the amount of data (count) or long-Dictionary, we can use the function len ().

Example Code:
# Make Dictonary 
books = { 
    "python": "Mastering Python within 24 hours", 
    "java": "Tutorial Learning Python for Beginners", 
    "php": "Creating a web application with a CSS" 

# print the amount of data that is in dictionary 
print ( "total book:% d"% len (books))
The result:
total book: 3

Comments

Popular posts from this blog

Contoh Program C++ : Konversi Bilangan Hexadesimal, Oktal , Biner.

Hai Pembaca FajarYusuf.Com yang pintar dan berwawasan tinggi, kali ini FajarYusuf.Com akan post Contoh Program C++ : Konversi Bilangan Hexadesimal, Oktal , Biner . Mari kita membuat program konversi bilangan yang sangat mudah di buat dengan menggunakan bahasa pemrograman C++. Yaitu dengan menggunakan type data long, untuk Hexadesimal dengan rumus : <<hex<<desimal (merubah dari bilangan Desimal ke Hexadesimal) , untuk Oktal dengan rumus : <<oct<<desimal (merubah dari bilangan Desimal ke Oktal). Dan untuk biner dilakukan pembagian 2. Contoh Coding Program C++ Konversi Bilangan: #include<iostream> using namespace std; int main() { long desimal, pembagi=1073741824, bit; cout<<"Nilai Desimal : "; cin>>desimal; cout<<"Hexadesimal = "<<hex<<desimal<<endl; cout<<"Oktal = "<<oct<<desimal<<endl; ...

Menambahkan Peraturan Komentar Pada Blog

Menambahkan Peraturan Komentar Pada Blog : Ngblog Menulis sebuah artikel dan dikomentari dengan link aktif/promosi produk dan kata kata spam lainnya memang tidak mengenakan anda mungkin akan sangat merasa kecewa sekali, anda mungkin seperti menulis sebuah artikel namun tidak dihargai karyanya oleh orang lain. Mungkin membuat aturan berkomentar di blog adalah salah 1 cara menetralisirnya, mungkin jika ada aturan tersebut orang berpikir 2 kali untuk meninggalkan komentar Spam, lalu bagaimana cara membuat aturan komentar tersebut? langsung saja kita praktekan Ikuti langkah berikut : Login ke bloger,masuk ke blog yang anda inginkan, Klik Setelan -> POS-KOMENTAR   Scroll kebawah dan cari menu "pesan formulir komentar" klik tambahkan , dan isi kolom sesuai keinginan kalian atau peraturan yang kalian inginkan.  Setelah selesai silakan klik simpan setelan dipojok kanan atas. Lihat hasilnya di blog kalian ,akan seperti ini : Jika ada pertanyaan...

Mengenal Struktur Pada Bahasa Pemrograman C++

Hai Pembaca FajarYusuf.Com yang pintar dan berwawasan tinggi, kali ini FajarYusuf.Com akan post  Mengenal Struktur Pada Bahasa Pemrograman C++ . Secara umum, struktur bahasa pemrograman C++ umumnya adalah seperti dibawah ini : // contoh program C++ #include <iostream> using namespace std; int main () { cout << "Selamat Belajar C++ di fajaryusuf.com"; return 0; } Hasil output: Klik gambar untuk memperbesar Sisi atas merupakan source code, dan sisi bawah adalah hasilnya setelah dilakukan compile dan di eksekusi. persatu Program diatas merupakan salah satu program paling sederhana dalam C++, tetapi dalam program tersebut mengandung komponen dasar yang selalu ada pada setiap pemrograman C++. Jika dilihat satu : // contoh program C++ Baris ini adalah komentar. semua baris yang diawali dengan dua garis miring (//) akan dianggap sebagai komentar dan tidak akan berpengaruh terhadap program. Dapat digunakan oleh programmer ...