Dictionary in Python

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 16:- Dictionary in Python

In Python, a dictionary is a versatile and powerful data structure that allows you to store a collection of key-value pairs. Dictionaries are also known as associative arrays or hash maps in other programming languages. Dictionaries are defined by enclosing a comma-separated list of key-value pairs within curly braces { }.

Here's a brief introduction to dictionaries in Python:

  1. Creating a Dictionary: You can create a dictionary by defining it with curly braces and specifying key-value pairs separated by colons.

Example:

pythonCopy code

# Dictionary of key-value pairs with strings as keys person = {    "name": "John",    "age": 30,    "city": "New York" } # Dictionary of key-value pairs with integers as keys grades = {    101: "A",    102: "B",    103: "C" } # Mixed data types as values in a dictionary data = {    "name": "Alice",    "age": 25,    "is_student": True,    "marks": [90, 95, 85] }

  1. Accessing Values: You can access the values in a dictionary by using the corresponding key.

Example:

pythonCopy code

person = {    "name": "John",    "age": 30,    "city": "New York" } print(person["name"])   # Output: John print(person["age"])    # Output: 30

  1. Modifying and Adding Elements: Dictionaries are mutable, so you can change the value associated with a key or add new key-value pairs.

Example:

pythonCopy code

person = {    "name": "John",    "age": 30,    "city": "New York" } person["age"] = 35    # Modify the value for the key "age" print(person)         # Output: {'name': 'John', 'age': 35, 'city': 'New York'} person["occupation"] = "Engineer"   # Add a new key-value pair print(person)                       # Output: {'name': 'John', 'age': 35, 'city': 'New York', 'occupation': 'Engineer'}

  1. Dictionary Methods: Dictionaries come with various built-in methods to perform common operations, such as keys(), values(), items(), get(), pop(), update(), etc. These methods provide convenient ways to manipulate dictionaries.

Example:

pythonCopy code

person = {    "name": "John",    "age": 30,    "city": "New York" } print(person.keys())      # Output: dict_keys(['name', 'age', 'city']) print(person.values())    # Output: dict_values(['John', 30, 'New York']) print(person.items())     # Output: dict_items([('name', 'John'), ('age', 30), ('city', 'New York')]) print(person.get("name"))     # Output: John print(person.get("gender"))   # Output: None

Dictionaries are widely used for various purposes, such as representing data in key-value pairs, mapping values to unique identifiers, and organizing data for efficient access and retrieval. They are an essential part of Python and are commonly used in many real-world applications.

2. Variables Data Types

Comments: 0

Frequently Asked Questions (FAQs)

How do I register on Sciaku.com?
How can I enroll in a course on Sciaku.com?
Are there free courses available on Sciaku.com?
How do I purchase a paid course on Sciaku.com?
What payment methods are accepted on Sciaku.com?
How will I access the course content after purchasing a course?
How long do I have access to a purchased course on Sciaku.com?
How do I contact the admin for assistance or support?
Can I get a refund for a course I've purchased?
How does the admin grant access to a course after payment?