Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
# Python Tutorial for Beginners 5: Dictionaries - Working with Key-Value Pairs
student = {'name':'John','age':25,'courses':['Math','CompSci']}
print(student)
print(student['name'])
print(student['age'])
print(student['courses'])
# print(student['phone']) # error, does not exist
# get method (cleaner)
print(student.get('name'))
print(student.get('phone'))
print(student.get('phone','Not Found'))
student['phone']='555-5555'
print(student.get('phone','Not Found'))
student['name']='Jane'
print(student)
student.update({'name':'Jim','age':26, 'phone':'222-2222'})
print(student)
# del(student['age'])
# print(student)
age = student.pop('age')
print(student)
print(age)
# Loop through keys/values of our dictionary
# First, how many keys in the dict?
print(len(student))
print(student.keys())
print(student.values())
print(student.items())
# Comparisons:
# Equal: ==
# Not Equal: !=
# Greater Than: >
# Less Than: <
# Greater or Equal: >=
# Less or Equal: <=
# Object Identity: is
# False Values:
# False
# None
# Zero of any numeric type
# Any empty sequence. For example, '', (), [].
# Any empty mapping. For example, {}.
condition = False
if condition:
print('Evaluated to True')
else:
print('Evaluated to False')
Advertisements
We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.
AcceptLearn more