printing python dictionary values
#print keys and values from the dictionary
for k, v in dic.items():
print(k, v)
printing python dictionary values
#print keys and values from the dictionary
for k, v in dic.items():
print(k, v)
how to use dictionaries in python
student_data = {
"name":"inderpaal",
"age":21,
"course":['Bsc', 'Computer Science']
}
#the keys are the left hand side and the values are the right hand side
#to print data you do print(name_of_dictionary['key_name'])
print(student_data['name']) # will print 'inderpaal'
print(student_data['age']) # will print 21
print(student_data['course'])[0]
#this will print 'Bsc' since that field is an array and array[0] is 'Bsc'
dictionary in python
# Dictionaries in Python are used to store set of data like Key: Value pair
# the syntax of a dictionary in Python is very simple we use {} inside that
# we define {Key: Value}, to separate multiple values we use','
programming_dictionary = {
"Bug": "An error in a program that prevents the program from running as expected.",
"Function": "A piece of code that you can easily call over and over again.",
"Loop": "The action of doing sommething again and again",
}
# to retrieve the values from a dictionary we use the Key name as an Index
# retrieving the Function's definition
print(programming_dictionary["Function"]) # this will print the definition of Function
# if you wanna print all the entries in the dictionary you can do that by for loop
for key in programming_dictionary:
print(programming_dictionary[key]) # prints all entries
# adding items to a dictionary
# the following code will add another entry to the dictionary called Variable
programming_dictionary["Variable"] = "The label to store some sort of data"
print(programming_dictionary["Variable"])
# editing the values of a key
# editing the value of variable
programming_dictionary["Variable"] = "Variables are nothing but reserved memory locations to store values. This means that when you create a variableyou reserve some space in memory"
# if you learnt something from this please upvote it
how to print a value from a dictionary in python
dictionary={
"Jeff":{
"lastname":"bobson",
"age":55,
"working":True
},
"James":{
"lastname":"Bobson",
"age":34,
"working":False
}
}
# For a good format:
for i in dictionary:
print(i, ":")
for j in dictionary[i]:
print(" ", j, ":", dictionary[i][j])
print()
# Output:
Jeff :
lastname : bobson
age : 55
working : True
James :
lastname : Bobson
age : 34
working : False
# For just a quick reading:
for k, v in dictionary.items():
print(k, v)
# Output:
Jeff {'lastname': 'bobson', 'age': 55, 'working': True}
James {'lastname': 'Bobson', 'age': 34, 'working': False}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us