0% found this document useful (0 votes)
19 views

Module 5 - For Loop and Dictionary

Uploaded by

merryfil.adolfo
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Module 5 - For Loop and Dictionary

Uploaded by

merryfil.adolfo
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Click to edit Master title style

Dictionary and
For Loop
Module 5

1
Click to edit Master title style
Outline:

• Dictionary
• For Loop

2
Click to Dictionary
Python edit Master title style

• The dictionary is an unordered collection that contains key:value


pairs separated by commas inside curly brackets
• Dictionaries are optimized to retrieve values when the key is
known.
• Dictionary object contains key-value pairs inside { } separated by
a colon symbol :
• The left side of : is a key, and the right side is a value

3
Click to Dictionary
Python edit Master title style

• The key should be unique and an immutable object


• A number, string or tuple can be used as key
• However, a dictionary with a list as a key is not valid, as the list is
mutable but a list can be used as a value
• The same key cannot appear more than once in a collection
• If the key appears more than once, only the last will be retained
• The value can be of any data type
• One value can be assigned to more than one key

4
Clickdict()
The to edit Master title
Constructor style
Method

• The dict is the class of all dictionaries


• A dictionary can also be created using the dict() constructor
method

5
Click to edit
Accessing Master title style
Dictionary

• Dictionary is an unordered collection, so a value cannot be


accessed using an index; instead, a key must be specified in the
square brackets
• Keys are case-sensitive
• If the specified key does not exist then it will raise an error
• Use the get() method to retrieve the key's value even if keys are
not known
• It returns None if the key does not exist instead of raising an error
• Use the for loop to iterate a dictionary in the Python script

6
Click to edit
Dictionary MasterDelete,
Update, title style
Retrieve, and Check

• The key cannot appear more than once


• Use the same key and assign a new value to it to update the dictionary
object
• Use a new key and assign a value to it
• The dictionary will show an additional key-value pair in it
• Use the del keyword, pop(), or popitem() methods to delete a pair
from a dictionary or the dictionary object itself
• To delete a pair, use its key as a parameter
• To delete a dictionary object, use its name

7
Click to edit
Dictionary MasterDelete,
Update, title style
Retrieve, and Check

• The keys() and values() methods return a view objects containing


keys and values respectively
• You can check whether a particular key exists in a dictionary
collection or not using the in or not in keywords
• Note that it only checks for keys not values

8
Click to edit Master title style
Built-in
Dictionary
Methods

9
Click to edit[14]
Simulation Master title style

10
Click to For
Python editLoop
Master title style

• In Python, the for keyword provides a more comprehensive


mechanism to constitute a loop
• The for loop is used with sequence types such as list, tuple, set,
range, etc.

• The body of the for loop is executed for each member element in
the sequence
• Hence, it doesn't require explicit verification of a boolean expression
controlling the loop (as in the while loop)

11
Click
For to edit
Loop Master title style
Syntax

12
Click
For to edit
Loop withMaster title style
the range() Function

• The range class is an immutable sequence type


• The range() returns the range object that can be used with the for
loop

13
Click
For to edit
Loop Master
Exit, title Else
Continue, styleBlock, and Nested

• The execution of the for loop can be stop and exit using the break
keyword on some condition
• Use the continue keyword to skip the current execution and
continue on the next iteration using the continue keyword on some
condition
• The else block can follow the for loop, which will be executed when
the for loop ends
• If a loop (for loop or while loop) contains another loop in its body
block, we say that the two loops are nested
• If the outer loop is designed to perform m iterations and the inner loop is
designed to perform n repetitions, the body block of the inner loop will get
executed m X n times
14
Click to edit[15]
Simulation Master title style

15

You might also like