SlideShare une entreprise Scribd logo
1  sur  47
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!

Contenu connexe

Similaire à PE1 Module 3.ppt

16. Arrays Lists Stacks Queues
16. Arrays Lists Stacks Queues16. Arrays Lists Stacks Queues
16. Arrays Lists Stacks QueuesIntro C# Book
 
if else python.pdf
if else python.pdfif else python.pdf
if else python.pdfGshs6
 
Data Structures: Stacks (Part 1)
Data Structures: Stacks (Part 1)Data Structures: Stacks (Part 1)
Data Structures: Stacks (Part 1)Ramachandra Adiga G
 
Data Structures by Maneesh Boddu
Data Structures by Maneesh BodduData Structures by Maneesh Boddu
Data Structures by Maneesh Boddumaneesh boddu
 
ppt python notes list tuple data types ope
ppt python notes list tuple data types opeppt python notes list tuple data types ope
ppt python notes list tuple data types opeSukhpreetSingh519414
 
c_programming.pdf
c_programming.pdfc_programming.pdf
c_programming.pdfHome
 
Assg 05 QuicksortCOSC 2336 Data StructuresObjectives.docx
Assg 05 QuicksortCOSC 2336 Data StructuresObjectives.docxAssg 05 QuicksortCOSC 2336 Data StructuresObjectives.docx
Assg 05 QuicksortCOSC 2336 Data StructuresObjectives.docxjane3dyson92312
 
Assg 05 QuicksortCOSC 2336 Data StructuresObjectives.docx
Assg 05 QuicksortCOSC 2336 Data StructuresObjectives.docxAssg 05 QuicksortCOSC 2336 Data StructuresObjectives.docx
Assg 05 QuicksortCOSC 2336 Data StructuresObjectives.docxfestockton
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programmingAMAN ANAND
 
Control structures ii
Control structures ii Control structures ii
Control structures ii Ahmad Idrees
 
UNIT II_python Programming_aditya College
UNIT II_python Programming_aditya CollegeUNIT II_python Programming_aditya College
UNIT II_python Programming_aditya CollegeRamanamurthy Banda
 
C++ Please test your program before you submit the answer.pdf
C++ Please test your program before you submit the answer.pdfC++ Please test your program before you submit the answer.pdf
C++ Please test your program before you submit the answer.pdfaashisha5
 
This first assignment will focus on coding in Python, applying kno.docx
This first assignment will focus on coding in Python, applying kno.docxThis first assignment will focus on coding in Python, applying kno.docx
This first assignment will focus on coding in Python, applying kno.docxabhi353063
 

Similaire à PE1 Module 3.ppt (20)

Python_Module_2.pdf
Python_Module_2.pdfPython_Module_2.pdf
Python_Module_2.pdf
 
16. Arrays Lists Stacks Queues
16. Arrays Lists Stacks Queues16. Arrays Lists Stacks Queues
16. Arrays Lists Stacks Queues
 
if else python.pdf
if else python.pdfif else python.pdf
if else python.pdf
 
Day2
Day2Day2
Day2
 
Data Structures: Stacks (Part 1)
Data Structures: Stacks (Part 1)Data Structures: Stacks (Part 1)
Data Structures: Stacks (Part 1)
 
Data Structures by Maneesh Boddu
Data Structures by Maneesh BodduData Structures by Maneesh Boddu
Data Structures by Maneesh Boddu
 
ppt python notes list tuple data types ope
ppt python notes list tuple data types opeppt python notes list tuple data types ope
ppt python notes list tuple data types ope
 
functions- best.pdf
functions- best.pdffunctions- best.pdf
functions- best.pdf
 
c_programming.pdf
c_programming.pdfc_programming.pdf
c_programming.pdf
 
Assg 05 QuicksortCOSC 2336 Data StructuresObjectives.docx
Assg 05 QuicksortCOSC 2336 Data StructuresObjectives.docxAssg 05 QuicksortCOSC 2336 Data StructuresObjectives.docx
Assg 05 QuicksortCOSC 2336 Data StructuresObjectives.docx
 
Assg 05 QuicksortCOSC 2336 Data StructuresObjectives.docx
Assg 05 QuicksortCOSC 2336 Data StructuresObjectives.docxAssg 05 QuicksortCOSC 2336 Data StructuresObjectives.docx
Assg 05 QuicksortCOSC 2336 Data StructuresObjectives.docx
 
Ppt on java basics1
Ppt on java basics1Ppt on java basics1
Ppt on java basics1
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programming
 
Ch-8.pdf
Ch-8.pdfCh-8.pdf
Ch-8.pdf
 
Control structures ii
Control structures ii Control structures ii
Control structures ii
 
Maxbox starter
Maxbox starterMaxbox starter
Maxbox starter
 
Python_UNIT-I.pptx
Python_UNIT-I.pptxPython_UNIT-I.pptx
Python_UNIT-I.pptx
 
UNIT II_python Programming_aditya College
UNIT II_python Programming_aditya CollegeUNIT II_python Programming_aditya College
UNIT II_python Programming_aditya College
 
C++ Please test your program before you submit the answer.pdf
C++ Please test your program before you submit the answer.pdfC++ Please test your program before you submit the answer.pdf
C++ Please test your program before you submit the answer.pdf
 
This first assignment will focus on coding in Python, applying kno.docx
This first assignment will focus on coding in Python, applying kno.docxThis first assignment will focus on coding in Python, applying kno.docx
This first assignment will focus on coding in Python, applying kno.docx
 

Plus de balewayalew

Chapter 1-Introduction.ppt
Chapter 1-Introduction.pptChapter 1-Introduction.ppt
Chapter 1-Introduction.pptbalewayalew
 
Data Analytics.ppt
Data Analytics.pptData Analytics.ppt
Data Analytics.pptbalewayalew
 
PE1 Module 4.ppt
PE1 Module 4.pptPE1 Module 4.ppt
PE1 Module 4.pptbalewayalew
 
PE1 Module 2.ppt
PE1 Module 2.pptPE1 Module 2.ppt
PE1 Module 2.pptbalewayalew
 
Chapter -6- Ethics and Professionalism of ET (2).pptx
Chapter -6- Ethics and Professionalism of ET (2).pptxChapter -6- Ethics and Professionalism of ET (2).pptx
Chapter -6- Ethics and Professionalism of ET (2).pptxbalewayalew
 
Chapter -5- Augumented Reality (AR).pptx
Chapter -5- Augumented Reality (AR).pptxChapter -5- Augumented Reality (AR).pptx
Chapter -5- Augumented Reality (AR).pptxbalewayalew
 
PE1 Module 1.ppt
PE1 Module 1.pptPE1 Module 1.ppt
PE1 Module 1.pptbalewayalew
 
Ch 1-Non-functional Requirements.ppt
Ch 1-Non-functional Requirements.pptCh 1-Non-functional Requirements.ppt
Ch 1-Non-functional Requirements.pptbalewayalew
 
Ch 6 - Requirement Management.pptx
Ch 6 - Requirement Management.pptxCh 6 - Requirement Management.pptx
Ch 6 - Requirement Management.pptxbalewayalew
 

Plus de balewayalew (20)

slides.06.pptx
slides.06.pptxslides.06.pptx
slides.06.pptx
 
slides.07.pptx
slides.07.pptxslides.07.pptx
slides.07.pptx
 
slides.08.pptx
slides.08.pptxslides.08.pptx
slides.08.pptx
 
Chapter 1-Introduction.ppt
Chapter 1-Introduction.pptChapter 1-Introduction.ppt
Chapter 1-Introduction.ppt
 
Data Analytics.ppt
Data Analytics.pptData Analytics.ppt
Data Analytics.ppt
 
PE1 Module 4.ppt
PE1 Module 4.pptPE1 Module 4.ppt
PE1 Module 4.ppt
 
PE1 Module 2.ppt
PE1 Module 2.pptPE1 Module 2.ppt
PE1 Module 2.ppt
 
Chapter -6- Ethics and Professionalism of ET (2).pptx
Chapter -6- Ethics and Professionalism of ET (2).pptxChapter -6- Ethics and Professionalism of ET (2).pptx
Chapter -6- Ethics and Professionalism of ET (2).pptx
 
Chapter -5- Augumented Reality (AR).pptx
Chapter -5- Augumented Reality (AR).pptxChapter -5- Augumented Reality (AR).pptx
Chapter -5- Augumented Reality (AR).pptx
 
Chapter 8.ppt
Chapter 8.pptChapter 8.ppt
Chapter 8.ppt
 
PE1 Module 1.ppt
PE1 Module 1.pptPE1 Module 1.ppt
PE1 Module 1.ppt
 
chapter7.ppt
chapter7.pptchapter7.ppt
chapter7.ppt
 
chapter6.ppt
chapter6.pptchapter6.ppt
chapter6.ppt
 
chapter5.ppt
chapter5.pptchapter5.ppt
chapter5.ppt
 
chapter4.ppt
chapter4.pptchapter4.ppt
chapter4.ppt
 
chapter3.ppt
chapter3.pptchapter3.ppt
chapter3.ppt
 
chapter2.ppt
chapter2.pptchapter2.ppt
chapter2.ppt
 
chapter1.ppt
chapter1.pptchapter1.ppt
chapter1.ppt
 
Ch 1-Non-functional Requirements.ppt
Ch 1-Non-functional Requirements.pptCh 1-Non-functional Requirements.ppt
Ch 1-Non-functional Requirements.ppt
 
Ch 6 - Requirement Management.pptx
Ch 6 - Requirement Management.pptxCh 6 - Requirement Management.pptx
Ch 6 - Requirement Management.pptx
 

Dernier

Solving The Right Triangles PowerPoint 2.ppt
Solving The Right Triangles PowerPoint 2.pptSolving The Right Triangles PowerPoint 2.ppt
Solving The Right Triangles PowerPoint 2.pptJasonTagapanGulla
 
Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHC Sai Kiran
 
Correctly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleCorrectly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleAlluxio, Inc.
 
Indian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.pptIndian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.pptMadan Karki
 
NO1 Certified Black Magic Specialist Expert Amil baba in Uae Dubai Abu Dhabi ...
NO1 Certified Black Magic Specialist Expert Amil baba in Uae Dubai Abu Dhabi ...NO1 Certified Black Magic Specialist Expert Amil baba in Uae Dubai Abu Dhabi ...
NO1 Certified Black Magic Specialist Expert Amil baba in Uae Dubai Abu Dhabi ...Amil Baba Dawood bangali
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfROCENODodongVILLACER
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...asadnawaz62
 
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor CatchersTechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catcherssdickerson1
 
Input Output Management in Operating System
Input Output Management in Operating SystemInput Output Management in Operating System
Input Output Management in Operating SystemRashmi Bhat
 
National Level Hackathon Participation Certificate.pdf
National Level Hackathon Participation Certificate.pdfNational Level Hackathon Participation Certificate.pdf
National Level Hackathon Participation Certificate.pdfRajuKanojiya4
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfAsst.prof M.Gokilavani
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 
System Simulation and Modelling with types and Event Scheduling
System Simulation and Modelling with types and Event SchedulingSystem Simulation and Modelling with types and Event Scheduling
System Simulation and Modelling with types and Event SchedulingBootNeck1
 
Industrial Safety Unit-IV workplace health and safety.ppt
Industrial Safety Unit-IV workplace health and safety.pptIndustrial Safety Unit-IV workplace health and safety.ppt
Industrial Safety Unit-IV workplace health and safety.pptNarmatha D
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvLewisJB
 
US Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of ActionUS Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of ActionMebane Rash
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxKartikeyaDwivedi3
 

Dernier (20)

Solving The Right Triangles PowerPoint 2.ppt
Solving The Right Triangles PowerPoint 2.pptSolving The Right Triangles PowerPoint 2.ppt
Solving The Right Triangles PowerPoint 2.ppt
 
Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECH
 
Correctly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleCorrectly Loading Incremental Data at Scale
Correctly Loading Incremental Data at Scale
 
Design and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdfDesign and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdf
 
Indian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.pptIndian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.ppt
 
NO1 Certified Black Magic Specialist Expert Amil baba in Uae Dubai Abu Dhabi ...
NO1 Certified Black Magic Specialist Expert Amil baba in Uae Dubai Abu Dhabi ...NO1 Certified Black Magic Specialist Expert Amil baba in Uae Dubai Abu Dhabi ...
NO1 Certified Black Magic Specialist Expert Amil baba in Uae Dubai Abu Dhabi ...
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdf
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...
 
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor CatchersTechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
 
Input Output Management in Operating System
Input Output Management in Operating SystemInput Output Management in Operating System
Input Output Management in Operating System
 
National Level Hackathon Participation Certificate.pdf
National Level Hackathon Participation Certificate.pdfNational Level Hackathon Participation Certificate.pdf
National Level Hackathon Participation Certificate.pdf
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 
System Simulation and Modelling with types and Event Scheduling
System Simulation and Modelling with types and Event SchedulingSystem Simulation and Modelling with types and Event Scheduling
System Simulation and Modelling with types and Event Scheduling
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
Industrial Safety Unit-IV workplace health and safety.ppt
Industrial Safety Unit-IV workplace health and safety.pptIndustrial Safety Unit-IV workplace health and safety.ppt
Industrial Safety Unit-IV workplace health and safety.ppt
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvv
 
US Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of ActionUS Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of Action
 
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptx
 

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.