In: Computer Science
1. Use the get() method to print the value of the "name" key and
the value of the "age" key of the stuInfo dictionary.
Use the variable given below:
stuInfo = {'name': 'John Smith', "gpa": 3.456, "age": 20}
2. Use the dict() function to make a copy of the NY dictionary
to NewYorkState dictionary.
3. Change the "name" value from "John Smith" to "James Bond" of
the stuInfo dictionary.
Use the variable given below:
stuInfo = {'name': 'John Smith', "gpa": 3.456, "age": 20}
need help with Python. Please help ASAP. Please please please.
Question 1
get() method is used to return value of an item of given key.
Syntax : dictionaryname.get(keyname)
Here, dictionary name is stuInfo.
To get value of age key, we can write
stuInfo.get("name")
If you want to print it, then write print(stuInfo.get("name"))
It will print John Smith
Same for age key, stuInfo.get("age") will return 20.
If you want to print it, then write print(stuInfo.get("age"))
It will print 20
Question 2
To copy NY directory to NewYorkState directory using dict() function
NewYorkState = dict(NY)
Question 3
To change value of name key from "John Smith" to "James Bond" of the stuInfo dictionary
stuInfo["name"] = 'James Bond'
Below is code reference,
#Question 1
#given dictionary
stuInfo = {'name': 'John Smith', "gpa": 3.456, "age": 20}
#get method to print value of "name" key
print(stuInfo.get("name"))
#get method to print value of "age" key
print(stuInfo.get("age"))
#--------------------------------------------------------------
#Qusetion 2
#NY dictionary
NY = {
"name": "New York",
"country": "US"
}
#to copy NY to NewYorkState dictionary
NewYorkState = dict(NY)
#to print NewYorkState
print(NewYorkState)
#-------------------------------------------------------------
#Qustion 3
#given directory
stuInfo = {'name': 'John Smith', "gpa": 3.456, "age": 20}
#it will change the value of name key
stuInfo["name"] = 'James Bond'
#print new stuInfo directory
print(stuInfo)
You can refer below code screenshot to understand output also.