0% found this document useful (0 votes)
1 views35 pages

McMullen ProgwPython 1e Mod27 PowerPoint

The document outlines programming paradigms, focusing on imperative and declarative paradigms, procedural programming, and object-oriented programming. It details the characteristics, advantages, and disadvantages of each paradigm, as well as their applications and associated programming languages. The content is structured to help learners understand the fundamental concepts and approaches in programming with Python.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views35 pages

McMullen ProgwPython 1e Mod27 PowerPoint

The document outlines programming paradigms, focusing on imperative and declarative paradigms, procedural programming, and object-oriented programming. It details the characteristics, advantages, and disadvantages of each paradigm, as well as their applications and associated programming languages. The content is structured to help learners understand the fundamental concepts and approaches in programming with Python.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 35

Programming with Python

Module 27: Programming


Paradigms

McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved.
May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Module Objectives (1 of 3)

• 27.1 Imperative and Declarative Paradigms


• 27.1.1 Define the term “programming paradigm.”
• 27.1.2 Differentiate between imperative and declarative programming paradigms.
• 27.2 The Procedural Paradigm
• 27.2.1 Associate procedural programming with step-by-step algorithms that specify how
a computer should perform a task.
• 27.2.2 Associate the procedural paradigm with imperative programming.
• 27.2.3 List the key characteristics of the procedural paradigm.
• 27.2.4 List advantages and disadvantages of the procedural paradigm.
• 27.2.5 Associate procedural programming with early high-level languages such as
Fortran, BASIC, Pascal, and C.

McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Module Objectives (2 of 3)

• 27.3 The Object-Oriented Paradigm


• 27.3.1 Associate the object-oriented paradigm with creating data models of a problem’s
entities, actions, and relationships.
• 27.3.2 Explain the significance of classes, objects, attributes, and methods in the object-
oriented paradigm.
• 27.3.3 List the key characteristics of OOP.
• 27.3.4 List advantages and disadvantages of the object-oriented paradigm.
• 27.3.5 Identify problems that can best be solved using the object-oriented paradigm.
• 27.3.6 Identify programming languages that support the object-oriented paradigm.
• 27.3.7 State that a programming language can support multiple paradigms.

McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Module Objectives (3 of 3)

• 27.4 Declarative Paradigms


• 27.4.1 Identify the approach of declarative paradigms as expressing the logic of a
problem rather than specifying a step-by-step algorithm.
• 27.4.2 List the key characteristics of declarative paradigms.
• 27.4.3 Identify problems that can best be solved using the declarative paradigm.
• 27.4.4 List logic programming, functional programming, and database query as
examples of the declarative paradigm.
• 27.4.5 Associate various programming languages with each type of declarative
paradigm.
• 27.4.6 List advantages and disadvantages of the declarative paradigm.

McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
27.1 Imperative and Declarative Paradigms

• Think outside the box


• Programmers can take various approaches to programming, referred to as
programming paradigms:
• View the task as a problem that can be solved by specifying a series of steps for the
computer to execute
• Visualize the computer manipulating data for several objects, people, or places
• Create a solution based on a series of rules
• Programming paradigms fall into one of two categories:
• Imperative programming paradigms: paradigms that focus on how by specifying
the steps required to carry out a task (e.g., procedural and object-oriented
paradigms)
• Declarative programming paradigms: paradigms that focus on what by describing
the problem or task (e.g., logic, functional, and database query paradigms)

McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
27.2 The Procedural Paradigm (1 of 7)

• Procedural basics
• Procedural paradigm: the traditional approach to programming, which helps you
visualize a program as a step-by-step algorithm
• A procedural program typically consists of instructions that indicate how a computer
should perform a task or solve a problem
• Characteristics of procedural programs
• Order
• Use of variables
• Use of selection control structures
• Use of repetition control structures
• Top-down decomposition

McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
27.2 The Procedural Paradigm (2 of 7)

Figure 27-4 A procedural algorithm


specifies how the computer
determines the trip with the lowest
gas cost

McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
27.2 The Procedural Paradigm (3 of 7)

• Characteristics of procedural programs


• Order is important because the steps are executed in the order they appear
• Use of variables to store data used for computations, output, and other operations
• Use of selection control structures, which tell the computer what to do based on
whether a condition is true or false
• Use of repetition control structures, which tell the computer to repeat one or more
instructions until a certain condition is met
• Top-down decomposition, or division of complex tasks into smaller tasks
• Smaller tasks can be coded as separate but linked programs or as segments of code
called subroutines, procedures, or functions within a comprehensive program

McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
27.2 The Procedural Paradigm (4 of 7)

Figure 27-5 Procedural


programs have a main
execution path, but
subroutines, procedures, and
functions serve as side trips

McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
27.2 The Procedural Paradigm (5 of 7)

Figure 27-6 The


procedural
paradigm works
well for these
types of
applications

McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
27.2 The Procedural Paradigm (6 of 7)

• Procedural paradigm applications


• Advantages of the procedural paradigm include:
• Program structure corresponds closely to the linear sequence in which the CPU
executes instructions
• Easy to learn and is a classic approach understood by most programmers
• Most popular programming languages support the procedural approach
• Flexibility for successfully implementing a wide range of problems and coding tasks
• Modularity allows a team of programmers to contribute code segments to a
comprehensive application

McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
27.2 The Procedural Paradigm (7 of 7)

• Procedural paradigm applications (continued)


• Disadvantage: does not fit certain categories of problems
• Applications without a clear order of events, start point, or end point
• Unstructured problems that may require solutions based on subjective, incomplete,
or uncertain data
• Activities without a clear algorithm
• Language-based problems that manipulate textual rather than numerical data
• Complex problems that have unusually complex logic
• Supported by many languages including early ones such as Fortran and COBOL,
BASIC, Pascal, C, C++, Python, and Java

McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Activity 27.1: Knowledge Check (1 of 2)

1. The various approaches to programming are referred to as _____.

2. The _____ programming category includes approaches that focus on describing the
problem or task.

McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Activity 27.1: Knowledge Check (2 of 2)

3. The _____, considered the traditional approach to programming, helps you visualize a
program as a step-by-step algorithm.

4. The division of complex programming tasks into smaller tasks, which can be coded as
linked programs or abstracted into segments of code called subroutines, procedures, or
functions, is known as _____.

McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Activity 27.1: Knowledge Check Answers (1 of 2)

1. The various approaches to programming are referred to as _____.

Answer: programming paradigms

2. The _____ programming category includes approaches that focus on describing the
problem or task.

Answer: declarative

McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Activity 27.1: Knowledge Check Answers (2 of 2)

3. The _____, considered the traditional approach to programming, helps you visualize a
program as a step-by-step algorithm.

Answer: procedural paradigm

4. The division of complex programming tasks into smaller tasks, which can be coded as
linked programs or abstracted into segments of code called subroutines, procedures, or
functions, is known as _____.

Answer: decomposition

McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
27.3 The Object-Oriented Paradigm (1 of 4)

• Objects, classes, and methods


• Object-oriented paradigm: paradigm based on the idea that programs can be designed
by visualizing objects that interact with each other
• Objects: abstractions of real-world entities such as people, places, and things
• Class: a template for specific object instances that defines their attributes and methods
• Attributes: object characteristics
• Methods: tasks that objects can perform
• General approach:
• Define classes
• Use the classes to create objects
• Use object data (attributes) and methods to perform calculations, etc.

McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
27.3 The Object-Oriented Paradigm (2 of 4)

Figure 27-9 In the object-oriented


paradigm, objects interact

McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
27.3 The Object-Oriented Paradigm (3 of 4)

• Characteristics of object-oriented programs


• Inheritance: a feature of OOP that allows subclasses to acquire common attributes from
a parent class
• Polymorphism: the capability of including multiple methods with the same name that
process objects differently depending on their class
• Encapsulation: classes are abstractions that can be visualized as black boxes that
encapsulate data attributes and methods
• Object-oriented applications
• Simulation and modeling systems
• Office automation systems that communicate and share information
• Internet services
• Web apps
• Mobile apps

McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
27.3 The Object-Oriented Paradigm (4 of 4)

• Object-oriented applications (continued)


• Advantages:
• Program structure can mirror reality because classes reflect real-world objects
• Inheritance and polymorphism provide flexibility
• Encapsulated classes and methods can be reused
• Programs are fairly easy to test, debug, and maintain
• Widely used for most software development
• Disadvantages:
• Large programs (more lines of code)
• Nonoptimal processing speed
• Not appropriate for some applications, e.g., those with complex rule sets or that
require natural language processing
• Supported by Simula, SmallTalk, Java, Python, Ruby, C++, and many others

McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
27.4 Declarative Paradigms (1 of 6)

• Declarative basics
• Declarative paradigms approach programming by describing what a program is
supposed to accomplish rather than the steps required to accomplish a task
• Example: logic programming
• Three types of statements: facts that form a database, rules that define the logic, and
queries that trigger processing
• Predicate expression: an expression that specifies a relationship to provide
information in a logic programming fact
• Queries are used to access facts and the results of rules
• Rules are used to perform calculations, display data, etc.
• The sequence of instructions in the code is not important
• Programming languages that support declarative programing handle flow control
internally

McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
27.4 Declarative Paradigms (2 of 6)

Figure 27-13 Logic programming uses


predicate expressions

McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
27.4 Declarative Paradigms (3 of 6)

Figure 27-14 A query


searches the
database for
matching facts

McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
27.4 Declarative Paradigms (4 of 6)

Figure 27-15 Rules provide


the logic for processing the
data provided by the
program’s facts

McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Activity 27.2: Breakout Groups:
A Problem Three Ways
1. Form pairs or small groups.
2. Search online to find a coding problem/challenge that interests you.
3. In writing, outline how a procedural program, an object-oriented program, and a logic
program would each handle this task. Be sure to identify key elements of the program for
each paradigm and, optionally, include sample code or pseudocode.
4. Now, compare the three approaches.
• How are they alike and different?
• Does one paradigm seem more suited to the task than the others?

McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
27.4 Declarative Paradigms (5 of 6)

• Characteristics of the declarative paradigm


• You specify the facts, rules, and queries rather than the steps for producing output
• Not based on a step-by-step algorithm, so the order of statements is less important
• The programming language handles the flow of control, so repetition controls are not
needed
• Applications for declarative programs
• Appropriate for applications involving complex relationships, interrelated rules, statistical
analysis, and database queries
• Logic programming paradigm: an approach that is effective for dealing with complex
relationships, decisions, and rule sets expressed as text; supported by Prolog
• Functional programming paradigm: an approach that is effective for applications that
have interacting text-based or numerical rules; supported by Haskell, Scheme, Ocaml,
Scala, and Clojure

McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
27.4 Declarative Paradigms (6 of 6)

• Applications for declarative programs (continued)


• Database query programming: using declarative commands to access and work with
data from databases, e.g., coding with SQL
• Advantages:
• Programmers do not have to code background control structures
• Relatively simple code is required for tasks involving complex and interrelated rules
or conditions
• Disadvantages:
• Program execution speed is not optimal because program flow is handled the same
way for every program
• Requires non-imperative thinking/design

McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Activity 27.3: Knowledge Check (1 of 2)

1. Reusing templates for combinations of attributes and methods through inheritance is a


feature of _____.

2. Classes are black boxes that _____ data attributes and methods.

McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Activity 27.3: Knowledge Check (2 of 2)

3. In logic programming, a fact supplies basic information in the form of a(n) _____.

4. _____ programming is effective for applications with interacting text-based or numerical


rules and is supported by languages such as Haskell and Clojure.

McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Activity 27.3: Knowledge Check Answers (1 of 2)

1. Reusing templates for combinations of attributes and methods through inheritance is a


feature of _____.

Answer: object-oriented programming

2. Classes are black boxes that _____ data attributes and methods.

Answer: encapsulate

McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Activity 27.3: Knowledge Check Answers (2 of 2)

3. In logic programming, a fact supplies basic information in the form of a(n) _____.

Answer: predicate expression

4. _____ programming is effective for applications with interacting text-based or numerical


rules and is supported by languages such as Haskell and Clojure.

Answer: Functional

McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Activity 27.4: Discussion

1. Each programming paradigm discussed in Module 27 has its advocates and its detractors.
Why would each appeal to some programmers in some situations? Support your answer
with the paradigm characteristics described in the module.

2. How might more than one programming approach be used in combination to optimize a
large software project with multiple components? What are the benefits and risks of
utilizing multiple paradigms when creating and maintaining a large project?

3. Recall what you learned in previous modules regarding machine language and assembly
language. Which programming paradigm most closely mirrors the CPU’s operations? What
are the implications of this for the other paradigms?

McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Activity 27.5: Breakout Groups:
Python Practice
1. Form pairs or small groups.
2. To continue sharpening your Python skills, design and write out the code for your own
short program related to travel. You could continue the theme of road trips and their costs
from the module or shift to a different form of travel or focus.
3. Test your program to ensure that it works as expected.
4. Which programming paradigm did you use?

McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Self-Assessment

1. Which of the three major programming paradigms discussed in Module 27 have you used
most often in writing your own programs? How do you feel about that paradigm?

2. Which of the paradigms do you think you would prefer to use as your “go to” strategy as a
professional programmer? What additional skills would you need to learn to use this
paradigm on the job?

McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Summary

Click the link to review the objectives for this presentation.


Link to Objectives

McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.

You might also like