Python Dictionary

A Dictionary is a unordered , indexed collection of items. Each item is given a Key and a value to store. For this example, the dictionary will store Users information such as Name, Date of Birth and phone number.

userDict = {
  "Name": "Joe",
  "DoB": "01/12/1901",
  "Phone": 8001234567
}
print(userDict)

Add Item

Items can be added by creating a new Key and giving it a value

userDict = {
  "Name": "Joe",
  "DoB": "01/12/1901",
  "Phone": 8001234567
}

userDict["Last Name"] = "Smith"

print(userDict)

Remove Item

pop.() can be used to remove specific items from a dictionary

userDict = {
  "Name": "Joe",
  "DoB": "01/12/1901",
  "Phone": 8001234567
}

userDict.pop("DoB")

print(userDict)

Edit Values

A Key’s values can be changed or edited by referencing it’s name

userDict = {
  "Name": "Joe",
  "DoB": "01/12/1901",
  "Phone": 8001234567
}
userDict["DoB"] = "03/07/2020"

print(userDict)

Retrieve Item

Objects in dictionary can be retrieved by using brackets and the name of the Key

userDict = {
  "Name": "Joe",
  "DoB": "01/12/1901",
  "Phone": 8001234567
}

user = userDict["Name"]

print(userDict)

Get Length of Dictionary

Print number of items in dictionary

print(len(userDict))

Loop Through Dictionary

This will loop through the dictionary one by one.

for x in thisdict:
  print(thisdict[x])

Loop through both Keys and Values

for x, y in thisdict.items():
  print(x, y)

Android Studio Loading Animation Between Activities

Progress Dialog is dialog showing a progress indicator and an optional text message or view. The methods of Progress Dialog being used in this tutorial are: ProgressDialog.setTitle() – Used to set title of dialog box ProgressDialog.setMessage() – Used to set dialog message being displayed ProgressDialog.setProgressStyle() – Choose the style of indicator ProgressDialog.dismiss() – Dismiss the…

Android Studio Tutorial SeekBar

SeekBar is an extension of ProgressBar that adds a draggable thumb. The user can touch thumb and drag left or right to set the current progress level or various other task. In this example, the seekbar will be used to display a percentage. As the user moves the SeekBar left to right the percentage value…

Python Ethereum Block Chain Interaction with Web3

This tutorial will show how to interact with the Ethereum blockchain using Python. To start, the Python library Web3 will need to be installed. The Web3 library allows you to interact with a local or remote Ethereum node, using a HTTP or IPC connection. Using Web3 you will be able to create smart contracts on…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s