2. 2
Sequence Data Types
Sequence data types allow you to store multiple
items/values in an organized and efficient fashion.
The items are stored in sequence one after another
Basic Sequence types:
list
tuple
3. 3
Python Lists
Lists are the most versatile of Python’s sequence
data types.
A list is mutable: the items in the sequence can be
changed
A list contains items separated by commas and
enclosed within square brackets [ ].
The values stored in a list can be accessed using [ ]
or [ : ] ) with indexes starting at 0 in the beginning of
the list and working their way to end-1.
The plus + sign is the list concatenation operator,
and the asterisk * is the repetition operator.
4. 4
Python Lists
The type() function in Python returns the data type
of the object passed to it as an argument.
7. 7
List Slicing
Slice: a span of items that are taken from a sequence
List slicing format: list[start : end]
Span is a list containing copies of elements from start
up to, but not including, end
If start not specified, 0 is used for start index
If end not specified, len(list) is used for end index
8. 8
List Methods and Useful Built-in
Functions
Len(): returns the length of a list
sorted(): used to sort the elements of the list.
9. 9
List Methods and Useful Built-in
Functions
append(item): used to add items to a list – item is
appended to the end of the existing list.
10. 10
List Methods and Useful Built-in
Functions
insert(index, item): used to insert item at position index in
the list.
11. 11
List Methods and Useful Built-in
Functions
index(item): used to determine where an item is located
in a list
Returns the index of the first element in the list
containing item
Raises ValueError exception if item not in the list
12. 12
List Methods and Useful Built-in
Functions (cont’d.)
remove(item): removes the first occurrence of item in
the list
13. 13
List Methods and Useful Built-in
Functions (cont’d.)
reverse(): reverses the order of the elements in the list
Activity: What will be the output of fighter_jets.reverse()
clear(): The clear() method removes all the elements from a list.
14. 14
List Methods and Useful Built-in
Functions (cont’d.)
copy(): The Python copy() method creates a copy of an
existing list.
15. 15
List Methods and Useful Built-in
Functions (cont’d.)
del: removes an element from a specific index in a list
or name of the list from memory.
print(boeing_planes)
16. 16
List Methods and Useful Built-in
Functions (cont’d.)
print(boeing_planes_copy)
copy(): deep copy, the copy remains even the original
source is removed .
17. 17
Shallow Copy
Assignment operator(=) to copy a list: The assignment operator
( = ) only creates a reference to an object, and will create a new
variable referencing the same memory address.
18. 18
List Methods and Useful Built-in
Functions (cont’d.)
min and max functions: built-in functions that returns the
item that has the lowest or highest value in a sequence
The sequence is passed as an argument
Count(): method counts how many times an element has.
What will be the output of print( fighter_jets.count(" Tiger
Moth"))
19. 19
Tuples
Tuple: an immutable sequence
Very similar to a list
Once it is created it cannot be changed
A tuple contains items separated by commas and
enclosed within square brackets ().
Tuples support operations as lists
Subscript indexing for retrieving elements
Methods such as index
Built in functions such as len, min, max, count, index
Slicing expressions
20. 20
Tuples
Tuples do not support the methods:
append
remove
insert
reverse
sorted
21. 21
Control Structures
Control flow is the order that instructions are executed in a program.
A control statement is a statement that determines the control flow of a set of
instructions.
A control structure is a set of instructions and the control statements
controlling their execution.
The fundamental forms of controls in programming are:
1. Sequential statements ,
2. Selection/ Conditional statements ,
3. Iterative/loop statements .
4. Jump statements
A program consisting of only sequential control is referred to as a “straight-
line program.”
Selection control is provided by a control statement that selectively executes
instructions.
Iterative control is provided by an iterative control statement that repeatedly
executes instructions.
Jump statements are statements through which we can transfer control
anywhere in the program.
22. 22
Sequential statements
Sequential control is the default control structure;
instructions are executed one after another.
Statement 1
Statement 2
Statement 3
……..
……..
……..
24. 24
Selection Statements
The selection control statements are also known as
Decision control statements or conditional branching
statements.
A conditional statement will select the block of statements
that will execute based on the given condition.
A selection statement causes the program control to be
transferred to a specific flow based upon whether a certain
condition is true or not.
The statements used to perform conditional branching :
if
if-else
if- elif-else
25. 25
if Statement
if statements are control flow statements that help us to run a
particular code, but only when a certain condition is met or
satisfied.
27. 27
if-else Statements
The if-else statement evaluates the condition and will execute
the body of if, if the test condition is True, but if the
condition is False, then the body of else is executed.
34. 34
Iterative/Loop/Repetition statements
A repetition statement is used to repeat a
group(block) of programming instructions.
In Python, we generally have two loops/repetitive
statements:
for loop
while loop
35. 35
for Loop
A for loop is used to iterate over a sequence.
36. 36
for Loop
friends = ['Jonathan', 'Kedija', 'Eba']
for friend in friends :
print('Happy New Year:', friend)
print('Done!')
Happy New Year: Jonathan
Happy New Year: Kedija
Happy New Year: Eba
Done!
38. 38
The range() function
To achieve the functionality of conventional for-loop in
Python, the range() function is used. It returns a list of
integers.
39. 39
The range() function
r= range(10) is equivalent to r=range(0,10): the range function
starts at 0 and goes up to but does not include 10 for
sequences.
40. 40
range() function in for Loop
when the range() receives 3 arguments, the sequence starts at the
first value, ends before the second argument and increments or
decrements by the third value.
41. 41
while loop
A while loop consists of a condition and until that condition
is true, a set of statements is executed repeatedly.
43. 43
Jump Statements
Jump statements in python are used to alter the flow of
a loop like you want to skip a part of a loop or
terminate a loop.
Types of jump statements in python:
continue
break
pass
44. 44
continue Statement
When the program encounters the continue statement, it will
skip all the statements present after the continue statement
inside the loop, and proceed with the next iterations.
When continue executed in a while loop
Current iteration of the loop terminates
Execution returns to the loop’s header
45. 45
break Statement
A break statement in Python exits out of a loop.
It terminates the execution of the loop.
46. 46
pass Statement
A pass statement is an empty/null statement that is considered
as a placeholder for future code.
Empty code shouldn’t be included in loops, function definitions,
class definitions, or if statements because there is a possibility
that it will cause an error. To avoid errors, the user can simply
apply a pass statement.