SlideShare a Scribd company logo
PE1 Module 3
Sequence Data Types and Control
Structures in Python
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
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
Python Lists
 The type() function in Python returns the data type
of the object passed to it as an argument.
5
Python Lists
6
Python Lists
Boeing 737 is no longer available and replaced by
Boeing 737 Original
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
List Methods and Useful Built-in
Functions
 Len(): returns the length of a list
 sorted(): used to sort the elements of the list.
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
List Methods and Useful Built-in
Functions
 insert(index, item): used to insert item at position index in
the list.
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
List Methods and Useful Built-in
Functions (cont’d.)
 remove(item): removes the first occurrence of item in
the list
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
List Methods and Useful Built-in
Functions (cont’d.)
 copy(): The Python copy() method creates a copy of an
existing list.
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
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
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
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
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
Tuples
 Tuples do not support the methods:
append
remove
insert
reverse
sorted
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
Sequential statements
 Sequential control is the default control structure;
instructions are executed one after another.
Statement 1
Statement 2
Statement 3
……..
……..
……..
23
Sequential Statement
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
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.
26
if Statement
Discus the output of the following codes:
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.
28
if-else Statements
29
if-else Statements
Discus the output of the following codes:
30
if-else Statements
Discus the output of the following codes:
31
if- elif-else Statement
 if-elif-else: The if-elif-else statement is used to conditionally
execute a statement or a block of statements.
32
if- elif-else Statement
33
if- elif-else Statement
Discus the output of the following codes:
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
for Loop
 A for loop is used to iterate over a sequence.
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!
37
for Loop
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
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
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
while loop
 A while loop consists of a condition and until that condition
is true, a set of statements is executed repeatedly.
42
while Loop
 Example : Program displays 1 – 5, after loop terminates,
num will be 6
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
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
break Statement
 A break statement in Python exits out of a loop.
 It terminates the execution of the loop.
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.
47
Any Question?
Thank You!

More Related Content

PPTX
Python Revision Tour.pptx class 12 python notes
PPTX
Unit - 2 CAP.pptx
PPTX
Chapter - 3.pptx
PPT
Control structures i
PPTX
Types of Statements in Python Programming Language
PPTX
Python For Data Science.pptx
PDF
2-functions.pptx_20240619_085610_0000.pdf
PPT
Of Lambdas and LINQ
Python Revision Tour.pptx class 12 python notes
Unit - 2 CAP.pptx
Chapter - 3.pptx
Control structures i
Types of Statements in Python Programming Language
Python For Data Science.pptx
2-functions.pptx_20240619_085610_0000.pdf
Of Lambdas and LINQ

Similar to PE1 Module 3.ppt (20)

PPTX
Learn more about the concepts of Data Types in Python
PDF
PDF
Python_Module_2.pdf
PPTX
16. Arrays Lists Stacks Queues
PDF
CS305PC_C++_UNIT 2.pdf jntuh third semester
PDF
if else python.pdf
PPTX
python programming control structures.pptx
PPTX
theory of programming languages by shikra
ODP
PDF
advanced python for those who have beginner level experience with python
PDF
Advanced Python after beginner python for intermediate learners
PPTX
Data Structures: Stacks (Part 1)
PPT
Data Structures by Maneesh Boddu
PPTX
“Python” or “CPython” is written in C/C+
PPTX
Introduction to C++.pptx learn c++ and basic concepts of OOP
PDF
ppt python notes list tuple data types ope
PDF
functions- best.pdf
PDF
c_programming.pdf
DOCX
Assg 05 QuicksortCOSC 2336 Data StructuresObjectives.docx
DOCX
Assg 05 QuicksortCOSC 2336 Data StructuresObjectives.docx
Learn more about the concepts of Data Types in Python
Python_Module_2.pdf
16. Arrays Lists Stacks Queues
CS305PC_C++_UNIT 2.pdf jntuh third semester
if else python.pdf
python programming control structures.pptx
theory of programming languages by shikra
advanced python for those who have beginner level experience with python
Advanced Python after beginner python for intermediate learners
Data Structures: Stacks (Part 1)
Data Structures by Maneesh Boddu
“Python” or “CPython” is written in C/C+
Introduction to C++.pptx learn c++ and basic concepts of OOP
ppt python notes list tuple data types ope
functions- best.pdf
c_programming.pdf
Assg 05 QuicksortCOSC 2336 Data StructuresObjectives.docx
Assg 05 QuicksortCOSC 2336 Data StructuresObjectives.docx
Ad

More from balewayalew (20)

PPTX
slides.06.pptx
PPTX
slides.07.pptx
PPTX
slides.08.pptx
PPT
Chapter 1-Introduction.ppt
PPT
Data Analytics.ppt
PPT
PE1 Module 4.ppt
PPT
PE1 Module 2.ppt
PPTX
Chapter -6- Ethics and Professionalism of ET (2).pptx
PPTX
Chapter -5- Augumented Reality (AR).pptx
PPT
Chapter 8.ppt
PPT
PE1 Module 1.ppt
PPT
chapter7.ppt
PPT
chapter6.ppt
PPT
chapter5.ppt
PPT
chapter4.ppt
PPT
chapter3.ppt
PPT
chapter2.ppt
PPT
chapter1.ppt
PPT
Ch 1-Non-functional Requirements.ppt
PPTX
Ch 6 - Requirement Management.pptx
slides.06.pptx
slides.07.pptx
slides.08.pptx
Chapter 1-Introduction.ppt
Data Analytics.ppt
PE1 Module 4.ppt
PE1 Module 2.ppt
Chapter -6- Ethics and Professionalism of ET (2).pptx
Chapter -5- Augumented Reality (AR).pptx
Chapter 8.ppt
PE1 Module 1.ppt
chapter7.ppt
chapter6.ppt
chapter5.ppt
chapter4.ppt
chapter3.ppt
chapter2.ppt
chapter1.ppt
Ch 1-Non-functional Requirements.ppt
Ch 6 - Requirement Management.pptx
Ad

Recently uploaded (20)

PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
Construction Project Organization Group 2.pptx
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PDF
PPT on Performance Review to get promotions
PPTX
OOP with Java - Java Introduction (Basics)
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
UNIT 4 Total Quality Management .pptx
PPT
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPTX
Geodesy 1.pptx...............................................
PPTX
Welding lecture in detail for understanding
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Model Code of Practice - Construction Work - 21102022 .pdf
UNIT-1 - COAL BASED THERMAL POWER PLANTS
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Construction Project Organization Group 2.pptx
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPT on Performance Review to get promotions
OOP with Java - Java Introduction (Basics)
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
UNIT 4 Total Quality Management .pptx
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
bas. eng. economics group 4 presentation 1.pptx
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Geodesy 1.pptx...............................................
Welding lecture in detail for understanding
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf

PE1 Module 3.ppt

  • 1. PE1 Module 3 Sequence Data Types and Control Structures in Python
  • 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.
  • 6. 6 Python Lists Boeing 737 is no longer available and replaced by Boeing 737 Original
  • 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.
  • 26. 26 if Statement Discus the output of the following codes:
  • 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.
  • 29. 29 if-else Statements Discus the output of the following codes:
  • 30. 30 if-else Statements Discus the output of the following codes:
  • 31. 31 if- elif-else Statement  if-elif-else: The if-elif-else statement is used to conditionally execute a statement or a block of statements.
  • 33. 33 if- elif-else Statement Discus the output of the following codes:
  • 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.
  • 42. 42 while Loop  Example : Program displays 1 – 5, after loop terminates, num will be 6
  • 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.