## Python Dictionary

Python’s dictionaries are (key,value) pairs which are used to map a key to a value. In lists, elements are accessed using integers whereas in dictionaries, elements are accessed using keys which can be either an integer or a string.

list = [‘Kshitiz’,18,24]

Here is a list with name as 1st index, age as 2nd index and height as 3rd index where integer index has no meaning to the values which are associated with it. Dictionaries rule out this problem by having key value pairs where each key is mapped to a particular value which itself can be an object. For example

personal_details = {‘name’: ‘Kshitiz’, ‘age’: 18, ‘height’: 24}

Dictionaries are enclosed by curly braces ({ }) which can be accessed by using square braces([]) like we used to do in lists and tuples.

dict = {}
dict[‘hello’] = “Hello. My name is python”
dict     = “I am an awesome language.”

personal_details = {‘name’: ‘Emma’, ‘age’: 18, ‘height’: 24}

print (personal_details[‘name’])       # Prints value for ‘name’ key
print (personal_details[age])           # Prints value for ‘age’ key
print (personal_details)          # Prints complete dictionary ‘personal_details’
print (personal_details.keys())   # Prints all the keys
print (personal_details.values()) # Prints all the values

This produce the following result −

Emma
18
{‘name’: ‘Emma’, ‘age’: 18, ‘height’: 24}
[‘name’, ‘age’, ‘height’]
[‘Emma’, 18, 24]

## Python Comparison Operator

The <, <=, >, >=, ==, != operators compare the values of 2 objects and returns True or False. Comparison operators can also be chained as follows-

>>> x = 10
>>> 1 < x < 3
False
>>> 10 < x < 20
False
>>> 3 > x <= 2
False
>>> 2 == x < 4
True

Recommended Post   Python Fundamentals
 Operators Description < x x>y : Returns true when x is greater than y. Else returns False >= x>=y : Returns true when x is greater than or equal to y. Else returns False == x==y : Returns true when x is equal to y. Else returns False != x!=y : Returns true when x is not equal to y. Else returns False

Learning by doing

Question 1 : Make a list of lists where each list has first name, last name and age of a particular person. Scan each list and find out the list where first name is equal to ‘Fred’.

list = [[‘Ron’,’Wordy’,23],[‘Emma’,’Williams’,23],[‘Fred’,’Wine’,’45’]]

Solution :

persons = [[‘Ron’,’Wordy’,23],[‘Emma’,’Williams’,23],[‘Fred’,’Wine’,’45’]]

result = []

for person in persons:                            # To iterate in list persons

if person_details==’Fred’:

result.append(person_details)
Question 2 : In above question, return person which has age greater than or equal to 23.

Solution :

persons = [[‘Ron’,’Wordy’,23],[‘Emma’,’Williams’,23],[‘Fred’,’Wine’,’45’]]

result = []

for person in persons:                            # To iterate in list persons

if person_details>=23:

result.append(person_details)

TRY IT OUT :

Question 3 : In above question, Find out person which has age greater than or equal to 23 and whose name is ‘Fred’.

The evaluation using the and and or operators follow these rules:

• AND – If all the conditions are True, then return true. Else it returns False
• OR – If any one condition is True, It return True. Else it returns False.

AND Clause = condition_1 and condition_2 and ………….. and condition_N

OR Clause = condition_1 or condition_2 or ………….. or condition_N

Recommended Post   Writing Your First Python Program
 Operators Descriptions not x Returns True if x is True, False otherwise x and y Returns x if x is False, y otherwise x or y Returns y if x is False, x otherwise
SHARE
Previous articlePython Variable Types
Next articlePython Assignment Operators