0% found this document useful (0 votes)
5 views

Program Code

The document provides a series of programming tasks involving the creation and manipulation of dictionaries in Python. It includes examples for creating a dictionary of odd numbers, inputting employee names and salaries, counting character occurrences in a string, converting numbers to words, and displaying employees with salaries above a certain threshold. Each task is accompanied by code snippets and expected outputs.

Uploaded by

abhinavchettri90
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Program Code

The document provides a series of programming tasks involving the creation and manipulation of dictionaries in Python. It includes examples for creating a dictionary of odd numbers, inputting employee names and salaries, counting character occurrences in a string, converting numbers to words, and displaying employees with salaries above a certain threshold. Each task is accompanied by code snippets and expected outputs.

Uploaded by

abhinavchettri90
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

VI.

DICTIONARY

1. Create a dictionary ‘ODD’ of odd numbers between 1 and 10, where the key is the decimal
number and the value is the corresponding number in words. Perform the following operations on
this dictionary:

(a) Display the keys

(b) Display the values

(c) Display the items

(d) Find the length of the dictionary

(e) Check if 7 is present or not

(f) Check if 2 is present or not

(g) Retrieve the value corresponding to the key 9

(h) Delete the item from the dictionary corresponding to the key 9

Ans:

# Create the dictionary


ODD = {1: 'one', 3: 'three', 5: 'five', 7: 'seven', 9: 'nine'}

# (a) Display the keys


keys = ODD.keys()
print("Keys:", keys)
# Output: Keys: dict_keys([1, 3, 5, 7, 9])

# (b) Display the values


values = ODD.values()
print("Values:", values)
# Output: Values: dict_values(['one', 'three', 'five', 'seven', 'nine'])

# (c) Display the items


items = ODD.items()
print("Items:", items)
# Output: Items: dict_items([(1, 'one'), (3, 'three'), (5, 'five'), (7, 'seven'), (9, 'nine')])

# (d) Find the length of the dictionary


length = len(ODD)
print("Length:", length)
# Output: Length: 5

# (e) Check if 7 is present or not


is_seven_present = 7 in ODD
print("Is 7 present?", is_seven_present)
# Output: Is 7 present? True

# (f) Check if 2 is present or not


is_two_present = 2 in ODD
print("Is 2 present?", is_two_present)
# Output: Is 2 present? False

# (g) Retrieve the value corresponding to the key 9


value_nine = ODD.get(9)
print("Value corresponding to key 9:", value_nine)
# Output: Value corresponding to key 9: nine

# (h) Delete the item from the dictionary corresponding to the key 9
ODD.pop(9, None)
print("Dictionary after deleting key 9:", ODD)
# Output: Dictionary after deleting key 9: {1: 'one', 3: 'three', 5: 'five', 7: 'seven'}

2. Write a program to enter names of employees and their salaries as input and store them
in a dictionary.

Ans.

program code:

# Initialize an empty dictionary


employees = {}

# Number of employees
num_employees = int(input("Enter the number of employees: "))

# Input employee names and salaries


for _ in range(num_employees):
name = input("Enter employee name: ")
salary = float(input("Enter employee salary: "))
employees[name] = salary

# Display the dictionary


print("Employee salaries:", employees)

Executable output:

Enter the number of employees: 3


Enter employee name: Aviral
Enter employee salary: 50000
Enter employee name: Abhinav
Enter employee salary: 60000
Enter employee name: Amarnath
Enter employee salary: 55000
Employee salaries: {'Aviral': 50000.0, 'Abhinav': 60000.0, 'Amarnath': 55000.0}

3. Write a program to count the number of times a character appears in a given string.

Ans:

Program Code:

# Function to count occurrences of each character in the string


def count_characters(input_string):
char_count = {}
for char in input_string:
if char in char_count:
char_count[char] += 1
else:
char_count[char] = 1
return char_count

# Input string from the user


input_string = input("Enter a string: ")

# Call the function and display the result


char_count = count_characters(input_string)
print("Character count:", char_count)

Executable Output:

Enter a string: hello world


Character count: {'h': 1, 'e': 1, 'l': 3, 'o': 2, ' ': 1, 'w': 1, 'r': 1, 'd': 1}

4. Write a function to convert a number entered by the user into its corresponding number
in words. For example, if the input is 876 then the output should be ‘Eight Seven Six’.

Ans:

Program Code:

# Function to convert a number to words


def number_to_words(number):
# Dictionary to map digits to words
digit_to_word = {'0': 'Zero', '1': 'One', '2': 'Two', '3': 'Three', '4': 'Four',
'5': 'Five', '6': 'Six', '7': 'Seven', '8': 'Eight', '9': 'Nine' }
# Convert the number to a string
number_str = str(number)

# Convert each digit to its corresponding word


words = [digit_to_word[digit] for digit in number_str]

# Join the words with spaces


result = ' '.join(words)
return result

# Input number from the user


input_number = input("Enter a number: ")

# Call the function and display the result


output = number_to_words(input_number)
print("The number in words is:", output)

Executable Output:

Enter a number: 969


The number in words is: Nine Six Nine

OR
Enter a number: 7890
The number in words is: Seven Eight Nine Zero

5. Write a program to display the name of all the employees whose salary is more than
25000 from the following dictionary.
emp={1:(“Amit”,25000),2:(“Suman”,30000),3:(“Ravi”,36000)}
Format of data is given below :{Empid : (Empname, EmpSalary)}

Ans:

Program Code:

# Employee dictionary
emp = {1: ("Amit", 25000), 2: ("Suman", 30000), 3: ("Ravi", 36000)}

# Function to display employee names with salary more than 25000


def display_high_salary_employees(employee_dict, threshold):
high_salary_employees = [name for empid, (name, salary) in employee_dict.items() if
salary > threshold]
return high_salary_employees

# Define the salary threshold


threshold = 25000

# Get the list of employees with salary more than the threshold
result = display_high_salary_employees(emp, threshold)

# Display the result


print("Employees with salary more than 25000:", result)

Output:

Employees with salary more than 25000: ['Suman', 'Ravi']

You might also like