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

Module 4 - While Loop, List, and Tuple

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)
10 views

Module 4 - While Loop, List, and Tuple

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/ 30

Click to edit Master title style

While Loop,
List, and Tuple
Module 4

1
Click to edit Master title style
Outline:

• While Loop
• List
• Tuple

2
Click to
While edit Master title style
Loop

• Python uses the while and for keywords to constitute a conditional


loop, by which repeated execution of a block of statements is
done until the specified boolean expression is true.

3
Click to
While edit Master title style
Loop

4
Click to
While edit Master title style
Loop

• Python keyword while has a conditional expression followed by


the : symbol to start a block with an increased indent
• This block has statements to be executed repeatedly
• Such a block is usually referred to as the body of the loop
• The body will keep executing till the condition evaluates to True
• If and when it turns out to be False, the program will exit the loop

5
Clickfrom
Exit to edit
theMaster title style
While Loop

• Use the break keyword to exit the while loop at some condition
• Use the if condition to determine when to exit from the while loop

6
Click to edit
Continue NextMaster title style
Iteration

• Use the continue keyword to start the next iteration and skip the
statements after the continue statement on some conditions

7
Click to
While editwith
Loop Master
elsetitle style
Block

• The else block can follow the while loop


• The else block will be executed when the boolean expression of
the while loop evaluates to False
• Use the continue keyword to start the next iteration and skip the
statements after the continue statement on some conditions

8
Click to edit[11]
Simulation Master title style

9
Click to List
Python edit Master title style

• In Python, the list is a mutable sequence type


• A list object contains one or more items of different data types in
the square brackets [] separated by a comma

10
ClickData
List to edit
andMaster
Accesstitle style

• A list can contain unlimited data depending upon the limitation of


your computer's memory
• List items can be accessed using a zero-based index in the
square brackets []
• Indexes start from zero and increment by one for each item
• Accessing an item using a large index than the list's total items
would result in IndexError
• A list can contain multiple inner lists as items that can be
accessed using indexes

11
ClickClass
List to edit Master title style

• All the list objects are the objects of the list class in Python
• Use the list() constructor to convert from other sequence types
such as tuple, set, dictionary, string to list

12
ClickIteration,
List to edit Master title
Update, andstyle
Remove

• A list items can be iterate using the for loop


• The list is mutable. You can add new items in the list using the
append() or insert() methods, and update items using indexes
• Note that an error "index out of range" will be thrown if the
element at the specified index does not exist
• Use the remove(), pop() methods, or del keyword to delete the list
item or the whole list

13
ClickOperators
List to edit Master title style

• Like the string, the list is also a sequence


• Hence, the operators used with strings are also available for use
with the list (and tuple also)

14
ClickOperators
List to edit Master title style

15
ClickOperators
List to edit Master title style

16
ClickOperators
List to edit Master title style

17
ClickOperators
List to edit Master title style

18
ClickMethods
List to edit Master title style

19
Click to edit[12]
Simulation Master title style

20
Click to Tuples
Python edit Master title style

• Tuple is an immutable (unchangeable) collection of elements of


different data types
• It is an ordered collection, so it preserves the order of elements in
which they were defined
• Tuples are defined by enclosing elements in parentheses (),
separated by a comma
• However, it is not necessary to enclose the tuple elements in
parentheses
• The tuple object can include elements separated by a comma
without parentheses

21
Click to editTuples
Identifying Master title style

• Tuples cannot be declared with a single element unless followed


by a comma
• However, it is not necessary to enclose the tuple elements in
parentheses
• The tuple object can include elements separated by a comma
without parentheses

22
Click to edit
Accessing Master
Tuple title style
Elements

• Each element in the tuple is accessed by the index in the square


brackets []
• An index starts with zero and ends with (number of elements - 1)
• The tuple supports negative indexing also, the same as list type
• The negative index for the first element starts from -number of
elements and ends with -1 for the last element

23
Click to edit
Accessing Master
Tuple title style
Elements

• If the element at the specified index does not exist, then the error
"index out of range" will be thrown
• Tuple elements can be unpacked and assigned to variables
• However, the number of variables must match with the number of
elements in a tuple; otherwise, an error will be thrown

24
Click to /edit
Update Master
Delete title
Tuple style
Elements

• Tuple is unchangeable
• So, once a tuple is created, any operation that seeks to change its
contents is not allowed
• However, you can delete an entire tuple using the del keyword

25
Click to
Tuple edit Master title style
Class

• The underlying type of a tuple is the tuple class


• You can check the type of a variable using the type() function
• The tuple() constructor is used to convert any iterable to tuple
type

26
Click to
Tuple edit Master title style
Operations

• Like string, tuple objects are also a sequence


• Hence, the operators used with strings are also available for the
tuple

27
Click to
Tuple edit Master title style
Operations

28
Click to
Tuple edit Master title style
Operations

29
Click to edit[13]
Simulation Master title style

30

You might also like