Chapter 3
Chapter 3
A class is user-defined data types that act a blueprint for the object.
Let’s take as example a class parrot can be :
We use the class keyword to define an empty class Parrot. From class, we
construct instances. An instance is a specific object created from a particular
class.
Object
These attributes are defined inside the
__init__ method of the class. It is the initializer
method that is first run as soon as the object is
created.
species is what we call class attributes are the
same for all instances of a class.
We create instances of the Parrot class. Here,
blu is a reference (value) to our new objects.
Methods
Methods are functions defined inside the body of a class. They are used to define
the behaviors of an object.
In this program, we define two methods
i.e sing() and dance(). These are called
instance methods because they are
called on an instance object i.e blu.
Static Methods
Static methods in Python are extremely similar to python class level methods, the
difference being that a static method is bound to a class rather than the objects for
that class.
Using the function staticmethod():
NumPy arrays are stored at one continuous place in memory unlike lists, so
processes can access and manipulate them very efficiently.
NumPy is a Python library and is written partially in Python, but most of the parts
that require fast computation are written in C or C++.
Numpy is optimized to work with latest CPU architectures.
NumPy Creating Arrays
An array that has 2-D arrays NumPy Arrays provides the ndim attribute
(matrices) as its elements is called that returns an integer that tells us how
3-D array. many dimensions the array have.
Array Indexing
Array indexing is the same as accessing an array element. You can access an array
element by referring to its index number.
Array Slicing
Slicing in Numpy arrays works similarly to slicing in lists. Which means taking
elements from one given index to another given index. We pass slice instead of
index like this: [start:end:step].
Example:
Searching Arrays
You can search an array for a certain value, and return the indexes that get a match.
To search an array, use the where() method.
numpy.where(condition, [x, y, ]/) Return elements chosen from x or y depending on
condition.