Skip to main content

GRADE XI - PYTHON DICTIONARY - 06-11-2024

 GRADE XI 

PYTHON DICTIONARY

Python Dictionary is collection of elements where each element is a key-value pair.

#CREATE DICTIONARY

d={}    #EMPTY DICTIONARY

print(d)

#print(type(d))     #type of data


#METHOD TO CREATE DICTIONARY

dict= {} or d1 = dict()


dict1={'mihir':'gurgaon','Agastya':'delhi','Palak':'GGN'}

print(dict1)


#insert data in Empty Dictionary

D1={}

D1[1]="ONE"

D1[2]="TWO"

D1[3]="THREE"

D1[4]="FOUR"

print(D1)

#APENDING VALUES IN DICT

dict1={'R':'Rain','B':'Ball','C':'Cat','T':'Tree','Age':15}

dict1['Name']='Preeti'

print(dict1)


#UPDATING ELEMENTS IN A DICTIONARY

dict1={'R':'Rain','B':'Ball','C':'Cat','T':'Tree','Age':15}

dict1['Name']="RIYA"

print(dict1)

#TRAVERSING A DICTIONARY

dict1={'R':'Rain','B':'Ball','C':'Cat','T':'Tree','Age':15}

for i in dict1:

    print(i,':',dict1[i])

# COMMON DICTIONARY FUNCTIONS AND METHODS

A={"Mon":"Monday","Tue":"Tuesday","Wed":"Wednesday","Thu":"Thursday"}

res=len(A)

print(res)


#clear function - it removes all the items from the dictionary

A={"Mon":"Monday","Tue":"Tuesday","Wed":"Wednesday","Thu":"Thursday"}

A.clear()

print(A)


#get() - Returns the value for given key

A={"Mon":"Monday","Tue":"Tuesday","Wed":"Wednesday","Thu":"Thursday"}

res=A.get('Wed')

print(res)


#items() - returns the content of dictionary

A={"Mon":"Monday","Tue":"Tuesday","Wed":"Wednesday","Thu":"Thursday"}

res=A.items()

print(res)


#key()- It return list of key values in a dictionary

A={"Mon":"Monday","Tue":"Tuesday","Wed":"Wednesday","Thu":"Thursday"}

res=A.keys()

print(res)


#Values() - returns a list of values from key-value pairs in a dictionary

A={"Mon":"Monday","Tue":"Tuesday","Wed":"Wednesday","Thu":"Thursday"}

res=A.values()

print(res)


#REMOVING AN ITEM FROM DICTIOARY

A={"Mon":"Monday","Tue":"Tuesday","Wed":"Wednesday","Thu":"Thursday"}

del A['Wed']

print(A)


#If Particular Key is not there , it will result in error

#A={"Mon":"Monday","Tue":"Tuesday","Wed":"Wednesday","Thu":"Thursday"}

#del A['Fri']

#print(A)


#Remove Item using pop(key)

A={"Mon":"Monday","Tue":"Tuesday","Wed":"Wednesday","Thu":"Thursday"}

A.pop('Wed')

print(A)


#IN AND NOT IN MEMBERSHIP OPERATOR

A={"Mon":"Monday","Tue":"Tuesday","Wed":"Wednesday","Thu":"Thursday"}

res='Thu' in A

print(res)


A={"Mon":"Monday","Tue":"Tuesday","Wed":"Wednesday","Thu":"Thursday"}

res='Mon' not in A

print(res)

print("😎\n" *5)

#Try to copy any emoji you wish

#(https://tools.picsart.com/text/emojis/) and build your own pattern

print(''.join("😉" *5))

print('\n'.join("😉" *5))

n = 5

print('\n'.join("😉" * i for i in range(1, n + 1)))

print("😉\n" +"💢" )

PRACTICE QUESTIONS

1. #CREATE A DICT WHICH STORES NAME AND PH NO OF YOUR FRIENDS(ANY 5)

#AND DISPLAY THE NAMES AND PH_NO IN THE DICTIONARY.

Sol:

phdict={"Kavya":2345,"Agastya":5667,"Mihir":7890,"Palak":6544,"Sanya":555}

for name in phdict:

    print(name,":",phdict[name])

2.#WAP to create a dictionary of two workers and their names as keys and other details in the form 

of values

Sol:

employee={'Riya':{'age':25,'Salary':20000},'Divya':{'age':35,'Salary':50000}} 

for key in employee:

    print("Employee",key,":")

    print("Age:",employee[key]['age'])

    print("Salary:",employee[key]['Salary'])


3.WAP to create a dictionary containing names of competition winner students as KEY and number of their wins as values

Sol:

n=int(input("How Many Students ?")) 

compwinner={ }

for i in range(n):

    key=input("Enter Name of the Student:")

    value=int(input("Enter No of Wins:"))

    compwinner[key]=value

print("The Dictionary is:", compwinner)


4.#WAP TO INPUT TOTAL NO OF SECTIONS AND STREAM IN 11TH CLASS AND DISPLAY ALL

Sol:

5.WAP to create a dictionary containing roll number of students and percentage as values for n number of students display the total number of students scoring more than 80

Sol:

stud={ }# Empty dictionary created

n=int(input("enter number of students - "))

for i in range(n):

       rno = input("enter Roll No - ")

      stud[rno] = float(input("Enter percentage –"))


ctr=0

for rno in stud:

    if stud[rno]>80:

        ctr+=1

print("No. of students scoring percentage above 80 –" ,ctr)

6.#Program to create a dictionary which stores names(key) of employees and their salary(value)


7.Write a program to create a dictionary a key:valuepair for “n” number of elements . Check and display whether the dictionary with different keys have same values or not. Display appropriate message.

Sol:

d1 = {}

n = int(input("Enter number of elements: "))


# Input keys and values

for i in range(n):

    key = int(input("Enter key: "))

    d1[key] = eval(input("Enter value: "))


# Check for duplicate values

found_duplicate = False

for k in d1: #iterates over each key in the dictionary

    cnt = 0

    for k1 in d1:            # iterates over all the keys in the dictionary again, checking if the value     

                                       associated with key k  

        if d1[k] == d1[k1]:

            cnt += 1

        if cnt > 1:

            break

    if cnt > 1:

        found_duplicate = True

        print("Keys have the same value")

        break

if not found_duplicate:

    print("No keys have the same value")

8.WAP TO INPUT NAMES(KEY) OF 'N' COUNTRIES AND THEIR CAPITAL AND CURRENCY(VALUES) STORE IT IN A DICTIONARY. ALSO SEARCH AND DISPLAY A PARTICULAR COUNTRY

dict={}    

n=int(int(input("Enter Number of countries")))

for i in range(n):

    cname=input("Enter country name")

    cap=input("Enter Capital name")

    curr=input("Enter Currency")

    dict[cname]=[cap,curr]   #Value as list assigned

sname=input("Enter country name to search")

for cn in dict:

    if cn==sname:

        print("Name found in dictionary")

        print("details of",cn,"-",dict[cn])

        break

    else:

        print("Country name not found")

Comments

Popular posts from this blog

PYTHON - MYSQL CONNECTIVITY CODE

  #INSERTION OF DATA import mysql.connector mydb = mysql.connector.connect( host="localhost", user="root", passwd="root", database="school" ) print("Successfully Connected") #print(mydb) mycursor=mydb.cursor()   v1=int(input("enter ID:")) v2=input("enter name:") v3=input("enter Gender:") v4=int(input("enter age:")) sql='insert into TEACH values("%d","%s","%s","%s")'%(v1,v2,v3,v4) print(sql) mycursor.execute(sql) mydb.commit() print("record added") #MYSQL Connection code – Deletion on database SOURCE CODE: s=int(input("enter id of TEACHER to be deleted:")) r=(s,) v="delete from TEACH where id=%s" mycursor.execute(v,r) mydb.commit() print("record deleted") MYSQL Connection code – Updation on database SOURCE CODE: import mysql.connector mydb = mysql.connector.c...

GRADE XII CS - VIVA QUESTIONS

  VIVA QUESTIONS GRADE XII CS Dear All Be thorough with your project and practical files, as the viva can be asked from anywhere. Stay calm, don’t get nervous, and be confident in front of the examiner. 1. Tell me about your project. 2. Which concepts you have used for your project? 3. What do you mean by front end and back end? How they are important in developing any such projects? 4  Mention the modules and built-in functions you have used in your project. 5. Which real world problems are solved by your project? 6. Explain the most important feature of your project. 7. Name a few mutable data types of python. Lists, Sets, and Dictionaries 8. Name a few immutable data types of python. Strings, Tuples, Numeric 9. Name ordered and unordered data type of python. Ordered – String, List, Tuples Unordred – Set, Dictionaries 10. What is the significance of a pass statement in python? pass is no operation python statement. This is used where python requires syntax but logic requires...