Showing posts with label python dict(). Show all posts
Showing posts with label python dict(). Show all posts

Python Accessing elements of dictionary-all keys() all values() all items()

Python Accessing elements of dictionary-all keys() all values() all items() 

#Accessing elements of dictionary

emp={"empname":"krishna","jobdesc":"software programmer","salary":34000}

print(emp['empname'])    #access value using name

print(emp.get('salary')) #get key value using key name in get function

#get all keys and values

#keys(): return the list of all keys in present dictionary

#ex:

emp={"empname":"krishna","jobdesc":"software programmer","salary":34000}

print(emp.keys())

print(type(emp.keys()))


#values():return the list of all values in present dictionary

#ex:

emp={"empname":"krishna","jobdesc":"software programmer","salary":34000}

print(emp.values())

print(type(emp.values()))


#items():return all the items present in the dictionary. each item will be inside a tuple as a      key-value pair.

emp={"empname":"krishna","jobdesc":"software programmer","salary":34000}

print(emp.items())

print(type(emp.items()))


#Iterating a dictionary:

emp={"empname":"krishna","jobdesc":"software programmer","salary":34000}

for x in emp:

    print(x)

note:it will display only keys

emp={"empname":"krishna","jobdesc":"software programmer","salary":34000}

print('key', ':', 'value')

for key in emp:

    print(key, ':', emp[key])

# using items() method

print('key', ':', 'value')

for key_value in emp.items():

    print(key_value[0], key_value[1])


#find the length of the dictionary

emp={"empname":"krishna","jobdesc":"software programmer","salary":34000}

print(len(emp))


#adding new keys in dictionary 

emp={"empname":"krishna","jobdesc":"software programmer","salary":34000}

emp["empno"]=101 #single item adding

print(emp)

emp.update({"doj":"12/09/2022","company":"google"})

print(emp)




#python dictionary,#dictionary keys(),#dictionary values(),#dictionary items(),
#python datatypes,#python dict(),#dictionary accessing elements,#all keys(),#all vlaues(), #all items()

How to Creating a dictionary in Python datatypes-Python Dictionary and Dict

How to Creating a dictionary in Python datatypes 

three ways to create a dictionary.


Using curly brackets: The dictionaries are created by enclosing the comma-separated Key: Value pairs inside the {} curly brackets. The colon ‘:‘ is used to separate the key and value in a pair.

Creating a dictionary in Python

ex:

employee = {"empname": "raemsh", "jobdesc": "Software Developer", "salary":88000}

print(employee)


Using dict() constructor:  Create a dictionary by passing the comma-separated key: value pairs inside the dict().

ex:

emp=dict({"empname":"krishna","jobdesc":"Python Developer","salaru":120000})

print(emp)


Using sequence having each item as a pair (key-value)

ex:

emp=dict([("empname","ravikrishna"),("jobdesc","software developer"),("salary",130000)])

print(emp)


# create dictionary with value as a list

ex:

emp = {"empname": "mohan rao", "empnumber": [987, 654,3210]}

print(emp)




#Creating a dictionary in Python, #creating python dictionary, #python dict(), #python dict constructor, #python jobs, #python programming, #python development





Latest Post

Don’t Sweat It – Chill with Crompton Air Cooler Deals

 ðŸŒž Don’t Sweat It – Chill with Crompton Air Cooler Deals  More Details : https://www.amazon.in/s?k=laptops+under+70000&crid=29QIFPXQYB3...