(CA) Python Unit-4
(CA) Python Unit-4
o State - The attributes of an object represent its state. It also reflects the properties of
an object.
o Behavior - The method of an object represents its behavior.
o Identity - Each object must be uniquely identified and allow interacting with the other
objects.
The classes and objects are the essential key to the object-oriented programming. Classes are
the blueprint of the object. Classes are used to bundling the data and functionality together.
Each newly created class must have its object. Let's understand real-life example of class and
object.
A human is a class which may have may attributes such as walking, sleeping, thinking, etc.
Suppose we want to name and age of 100 humans, so we don't need to create a class for every
person. We just need to instantiate the multiple objects of that perticular class.
The class contains the user-defined data structure that holds the own data members such as
variables, constructs, and member functions, which can be accessed by creating an object of
the class.
The syntax of creating a class is given below. The syntax of creating a class is given below.
1
Syntax:
class ClassName:
#statement_suite
The class keyword is used to define the class and the user-define class name replaces
ClassName.
The object is essential to work with the class attributes. Instantiate is a term used when we
create the object of any class, and the instance is also referred to as an object. The object is
created using the class name. The syntax is given below.
Syntax:
<object-name> = <class-name>(<arguments>)
Example -
class Person:
name = "John"
age = 24 def
display (self):
print("Age: %d \nName: %s"%(self.age,self.name))
# Creating a emp instance of Employee
class per = Person() per.display()
Output:
Age: 24
Name: John
Explanation:
In the above code, we have created a Person class which consisted of two attributes age, name
and display function. We created the object of person class called per . Using the object along
with the .dot operator, we accessed the class function.
2
4.2. Turtle Graphics
Turtle is a Python library to draw graphics. After we import Turtle we can give
commands like forward, backward, right, left etc. These commands will draw different shapes
when we. When We combine Search commands, we can create many nice graphics in the below
example we will see some simple scenarios and then some Complex ones where nice graphics
is created.
fillcolor() Color name Changes the color of the turtle will use to fill a polygon
3
heading() None It returns the current heading
end_fill() None It closes the polygon and fills with the current fill color
Examples
Draw a Star
In the below program we draw a start. We choose appropriate steps to move the cursor
forward and then right continuously to get this result.
Example
4
Multiple Squares
In the next example we see the drawing of multiple squares all starting from a common point.
We sue the usual simple commands to go forward, backward and then turn 90 degrees.
Example
import turtle
mult_square=turtle.Turtle() def
Multiple_Squares(length, colour):
mult_square.pencolor(colour)
mult_square.pensize(2)
mult_square.forward(length)
mult_square.right(90)
mult_square.forward(length)
mult_square.right(90)
mult_square.forward(length)
mult_square.right(90)
mult_square.forward(length)
mult_square.right(90)
mult_square.setheading(360)
for i in range(60,120,15):
Multiple_Squares(i,"blue")
turtle.done
5
A spiral hexagon
This is a very interesting example where we use turtle to create a spiral structure. The final
shape is a hexagon and there are various colours used in producing the sides of the hexagon.
Example
sketch = turtle.Pen()
turtle.bgcolor("black")
for i in range(200):
sketch.pencolor(colors[i % 6])
sketch.width(i/100 + 1)
sketch.forward(i)
sketch.left(59)
6
Create a simple Python module
Let’s create a simple calc.py in which we define two functions, one add and another subtract.
Note: This does not import the functions or classes directly instead imports the module only.
To access the functions inside the module the dot(.) operator is used.
print(calc.add(10, 2))
Output: 12
7
Importing specific attributes from the module
Here, we are importing specific sqrt and factorial attributes from the math module.
Output:
4.0
720
The use of * has its advantages and disadvantages. If you know exactly what you will be
needing from the module, it is not recommended to use *, else do so.
Output
4.0
720
8
Locating Python Modules
Whenever a module is imported in Python the interpreter looks for several locations. First, it
will check for the built-in module, if not found then it looks for a list of directories defined in
the sys.path. Python interpreter searches for the module in the following manner – First, it
searches for the module in the current directory.
• If the module isn’t found in the current directory, Python then searches each
directory in the shell variable PYTHONPATH. The PYTHONPATH is an
environment variable, consisting of a list of directories.
• If that also fails python checks the installation-dependent list of directories
configured at the time Python is installed.
Output:
[‘/home/nikhil/Desktop/gfg’, ‘/usr/lib/python38.zip’, ‘/usr/lib/python3.8’,
‘/usr/lib/python3.8/lib-dynload’, ”, ‘/home/nikhil/.local/lib/python3.8/site-packages’,
‘/usr/local/lib/python3.8/dist-packages’, ‘/usr/lib/python3/dist-packages’,
‘/usr/local/lib/python3.8/dist-packages/IPython/extensions’, ‘/home/nikhil/.ipython’]
9
# 2 radians = 114.59 degrees
print(math.degrees(2))
# 1 * 2 * 3 * 4 = 24
print(math.factorial(4))
Output:
5.0
3.14159265359
114.591559026
1.0471975512
0.909297426826
0.87758256189
0.234143362351
24
3
10
4.4. Top-Down Design Model:
In the top-down model, an overview of the system is formulated without going into detail for
any part of it. Each part of it then refined into more details, defining it in yet more details
until the entire specification is detailed enough to validate the model. if we glance at a haul as
a full, it’s going to appear not possible as a result of it’s so complicated For example: Writing
a University system program, writing a word processor. Complicated issues may be resolved
victimization high down style, conjointly referred to as Stepwise refinement where,
1. We break the problem into parts,
2. Then break the parts into parts soon and now each of parts will be easy to do.
Advantages:
• Breaking problems into parts help us to identify what needs to be done.
• At each step of refinement, new parts will become less complex and therefore
easier to solve.
• Parts of the solution may turn out to be reusable.
• Breaking problems into parts allows more than one person to solve the problem.
S.
No.
TOP DOWN APPROACH BOTTOM UP APPROACH
11
Each part is programmed separately Redundancy is minimized by using data
therefore contain redundancy. encapsulation and data hiding.
3.
In this top function of system might In this sometimes we can not build a
be hard to identify. program from the piece we have started.
7.
12