Skip to main content

Dictionaries in Python

Welcome to the Third Blog 👇







Dictionaries in Python:


myDict={"name":"abbas","age":24,"gender":"male"}


In Python, dictionaries are a powerful data structure that allows you to store and manipulate data in a key-value pair format. They are incredibly versatile and commonly used in various programming tasks.


 Adding and Modifying Items


To add a new item to a dictionary, we can simply assign a value to a new key. For example, we can add an email to a dictionary like this:

```

myDict["email"]="abbasjkdj@gmail.com"

```

This will add the key "email" with the corresponding value to the dictionary. Similarly, to modify an existing key's value, we can directly reassign a new value, such as changing the age like this:

```

myDict["age"]=29

```

We can then see the updated dictionary with the modified values.


Accessing Items


Accessing items in a dictionary is straightforward. By using the key, we can retrieve the corresponding value. For example, to print the name and gender from the dictionary, we can do:

```

print(myDict["name"])

print(myDict["gender"])

```

This will output the values associated with the "name" and "gender" keys in the dictionary.

Removing Items

If we want to remove an item from a dictionary, we can use the `del` keyword followed by the key we want to remove. For instance, to delete the "age" key from the dictionary, we can do this:

```

del myDict["age"]

```

After executing this command, the dictionary will no longer contain the key "age".


 Nested Dictionaries

Nested dictionaries allow us to create more complex data structures. We can have dictionaries within dictionaries to organize and represent data hierarchically. For example, we can create nested dictionaries for different persons like this:

```

nested_dict={

    "person1":{"name":"abbas","age":20},

    "person2":{"name":"rida","age":17}

}

This nested dictionary contains information about two individuals in a structured format.

 Conclusion

Dictionaries in Python are flexible and essential data structures for storing and retrieving data efficiently. By understanding how to add, modify, access, and remove items, as well as work with nested dictionaries, you can leverage the power of dictionaries in your Python programs.

Comments

Popular posts from this blog

Creating and Using Functions in Python: A Comprehensive Guide

  Introduction Functions are a fundamental building block in Python, allowing us to write reusable and modular code. This blog will guide you through the process of creating and using functions in Python, with detailed explanations and examples. We will cover basic functions, parameterized functions, and a simple calculator program that utilizes these functions. Table of Contents Introduction to Functions Basic Function Examples Parameterized Functions Advanced Function Examples Building a Simple Calculator Conclusion 1. Introduction to Functions Functions are defined using the def keyword, followed by the function name and parentheses () . Inside the parentheses, we can define parameters that the function will accept. The function body contains the code that runs when the function is called. 2. Basic Function Examples Let's start with a basic example where we define a function to sum two numbers: In this example, the function ' sum' adds two fixed numbers and prints the ...

Learn JavaScript with simple examples and small projects

I'd be happy to help you learn JavaScript with simple examples and small projects for web development. Let's start with the basics and gradually build up to more complex concepts. We'll go step by step, and I'll provide small projects along the way. Step 1: JavaScript Basics Let's start with some fundamental JavaScript concepts: Variables and Data Types Operators Conditional Statements Functions Here's a simple example that covers these basics: Small Project 1: Age Calculator Let's create a simple web page that calculates a person's age based on their birth year. This project introduces you to: Basic HTML structure JavaScript functions DOM manipulation Event handling (onclick) Step 2: Arrays and Loops Next, let's learn about arrays and loops: Small Project 2: To-Do List Let's create a simple to-do list application: This project introduces: Array manipulation Dynamic DOM updates Event handling for multiple elements Step 3: Objects and JSON Now, le...

Learning HTML and CSS: A Beginner's Guide

   INTRODUCTION TO WAD (WEB APPLICATION DEVELOPEMENT) WAD is a broad term that encompasses the process of creating web-based applications that can be accessed through a web browser.  Definition: WAD refers to the development of software applications that run on web servers and are accessed by users through web browsers over the internet or an intranet. Key Components: a) Front-end (Client-side): HTML: Structure of the web pages CSS: Styling and layout JavaScript: Client-side interactivity and functionality b) Back-end (Server-side): Server-side languages (e.g., PHP, Python, Ruby, Java, C#) Databases (e.g., MySQL, PostgreSQL, MongoDB) Web servers (e.g., Apache, Nginx) Development Process: Planning and requirements gathering Design (UI/UX) Development (coding) Testing Deployment Maintenance and updates Key Concepts: Responsive design RESTful APIs Security (authentication, authorization, data protection) Performance optimization Scalability Frameworks and Tools: Front-end: R...