Friday, 3 February 2023

Class 12 Python Programs : Dictionaries.mr indian coder.mictechblog


Class 12 Python Programs : Dictionaries 

Class 12 students learning Python can benefit greatly from using dictionaries in their programs. A dictionary in Python is a collection of key-value pairs, where each key is unique and maps to a specific value. This makes dictionaries useful for storing and organizing data in a program.


To create a dictionary in Python, use curly braces {} and separate each key-value pair with a colon. You can access values in a dictionary by using square brackets [] and the key name.


One common use for dictionaries in Python is counting the frequency of words in a given text. This can be achieved by creating an empty dictionary, iterating over the words in the text, and using each word as the key, with the value being the number of times it appears in the text.


Another use for dictionaries is creating a database-like program, where data is stored in dictionaries and can be easily retrieved and updated. This can be especially useful for students who want to create programs that store information about their friends, family, or any other data they want to keep track of.


In conclusion, dictionaries in Python are a versatile data structure that can be used in a variety of programs. Class 12 students learning Python should definitely make use of dictionaries in their PROGRAMS.


Store student information in a dictionary:

python

Copy code

student_info = {"name": "John Doe", "roll_number": 12345, "marks": 85}



Add, delete, and modify elements in a dictionary:

python

Copy code

# Add element

student_info["address"] = "123 Main Street"


# Delete element

del student_info["roll_number"]


# Modify element

student_info["marks"] = 90




Find the frequency of words in a given text using dictionaries:

python

Copy code

def word_frequency(text):

    word_count = {}

    words = text.split()

    for word in words:

        if word in word_count:

            word_count[word] += 1

        else:

            word_count[word] = 1

    return word_count


text = "the quick brown fox jumps over the lazy dog"

print(word_frequency(text))



Store a phonebook using dictionaries:

python

Copy code

phonebook = {"John Doe": "555-555-5555", "Jane Doe": "555-555-5556"}



Find the most common elements in a list using dictionaries:

python

Copy code

def most_common_element(lst):

    element_count = {}

    for element in lst:

        if element in element_count:

            element_count[element] += 1

        else:

            element_count[element] = 1

    return max(element_count, key=element_count.get)


lst = [1, 2, 3, 4, 5, 1, 2, 3, 1]

print(most_common_element(lst))




Store a history of transactions for a bank account:

python

Copy code

transactions = {"date": "2022-01-01", "type": "deposit", "amount": 1000}


Store the state capitals of India using dictionaries:

python

Copy code

indian_states = {"Andhra Pradesh": "Amaravati", "Arunachal Pradesh": "Itanagar", "Assam": "Dispur", ...}


Find the average marks of students using dictionaries:

python

Copy code

student_marks = {"John Doe": 85, "Jane Doe": 90, "Jim Smith": 80}


def average_marks(student_marks):

    total_marks = 0

    for marks in student_marks.values():

        total_marks += marks

    return total_marks / len(student_marks)


print(average_marks(student_marks))


Store the details of a company's employees using dictionaries:

python

Copy code

employee_details = {"John Doe": {"age": 30, "salary": 50000}, "Jane Doe": {"age": 35, "salary": 60000}}

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home