KEMBAR78
Python Programming | JNTUA | UNIT 2 | Case Study | | PPTX
Python Programming
Unit – II (Part I)
(Lecture 14)
Case Study
Case Study
Lecture 14
The turtle
module
Simple
Repetition
Encapsulatio
n
Generalizatio
n
Interface
design
Refactoring
docstring
Lecture 15
Lecture 16
Case study
Lecture 14
The turtle
module
Simple
Repetition
The turtle module
• A package is a collection of modules.
• One of the modules in Swampy package is
TurtleWorld.
• It provides a set of functions for drawing lines
by steering turtles around the screen.
The turtle module
• If Swampy is installed as a package on your
system, you can import TurtleWorld like this:
from swampy.TurtleWorld import *
• If you downloaded the Swampy modules but
did not install them as a package hen you can
import TurtleWorld like this:
from TurtleWorld import *
The turtle module
The turtle module
TurtleWorld provides several turtle-
steering functions:
• fd and bk for forward and backward
• lt and rt for left and right turns.
• The functions pu and pd stand for
“pen up” and “pen down”.
The turtle module
Case study
Lecture 14
The turtle
module
Simple
Repetition
Simple Repetition
Case study
Lecture 14
The turtle
module
Simple
Repetition
Python Programming
Unit – II (Part I)
(Lecture 15)
Case Study
Case Study
Lecture 14
The turtle
module
Simple
Repetition
Encapsulatio
n
Generalizatio
n
Interface
design
Refactoring
docstring
Lecture 15
Lecture 16
Case study
Encapsulatio
n
Generalizatio
n
Lecture 15
Encapsulation
• Wrapping a piece of code up in a
function is called encapsulation.
• Advantage is that if you re-use the
code, it is more concise to call a
function twice than to copy and
paste the body!
Case study
Encapsulatio
n
Generalizatio
n
Lecture 15
Generalization
• Adding a parameter to a function is
called generalization because it
makes the function more general:
• in the previous version, the square
is always the same size; in this
version it can be any size.
Case study
Encapsulatio
n
Generalizatio
n
Lecture 15
Python Programming
Unit – II (Part I)
(Lecture 16)
Case Study
Case Study
Lecture 14
The turtle
module
Simple
Repetition
Encapsulatio
n
Generalizatio
n
Interface
design
Refactoring
docstring
Lecture 15
Lecture 16
Case Study
Interface
design
Refactoring
docstring
Lecture 16
Interface design
• n is the number of line segments
in our approximation of a circle,
so length is the length of each
segment.
• Thus, polygon draws a 50-sides
polygon that approximates a
circle with radius r.
Interface design
• One limitation of this solution is that n
is a constant, which means that for
very big circles, the line segments are
too long, and for small circles, we
waste time drawing very small
segments.
Interface design
• The interface of a function is a
summary of how it is used:
• What are the parameters?
• What does the function do?
• What is the return value?
Interface design
• Now the number of segments is
(approximately) circumference/3,
so the length of each segment is
(approximately) 3,
• which is small enough that the
circles look good, but big enough
to be efficient, and appropriate
for any size circle.
Case Study
Interface
design
Refactoring
docstring
Lecture 16
Refactoring
We can’t use polygon or circle to draw an
arc.
Refactoring
Case Study
Interface
design
Refactoring
docstring
Lecture 16
docstring
Case Study
Interface
design
Refactoring
docstring
Lecture 16

Python Programming | JNTUA | UNIT 2 | Case Study |