Object-Oriented Programming

At this point, it’s important to discuss object-oriented programming (or OOP). OOP is a

coding model that serves as the core principle behind major computer languages (e.g.

Java). You need to understand OOP if you want to be a skilled hacker.

The Components of an Object

Each object has methods (things it can do) and properties (states or attributes).

OOP allows programmers to link their activities with the real world. For instance, a

computer has methods (e.g. turns on, accesses the internet, launches applications, etc.) and

properties (e.g. available space, processing speed, brand, etc.). If you’ll think of OOP as a

human language, objects are nouns, methods are verbs, and properties are adjectives.

Each object belongs to a class. A computer, for example, belongs to the class called

“machines”. “Machines” is the class, “computers’ is a subclass, and “laptops” is a subsubclass.

An object gets the characteristics of its class.

Variables

Variables point to information that exists in a computer’s memory. In Python, this memory

can keep different pieces of data (e.g. strings, lists, integers, Booleans, dictionaries, real

numbers, etc.).

Variable types act like classes. The script you’ll see below shows some of these types.

Launch a text editor and type the following code:

#!usr/bin/python/

SampleStringVariable = “This is an awesome variable.”;

SampleList = [10,20,30,40,50]

SampleDictionary = {‘example’: ‘Hacker’, ‘number’: 23}

print SampleStringVariable

After running that script, you will see the following message on your screen:

This is an awesome variable.

Important Note: Python can choose the right type of variable on your behalf. You don’t

have to declare the variable before setting its value.

Functions

The Python language comes with preinstalled functions. Kali Linux has an extensive

collection of functions, although you may download more from online libraries. Here are

some functions that you’ll use in your programs:

int() – Use this function to truncate numeric data. It simply gives the integer part of

the argument.

len() – This function counts the items in a list.

exit() – This function lets you exit a program.

max() – With this function, you can determine the highest value of a list.

type() – Use this function to identify the data type of a Python object.

float() – This function converts its argument into a floating-point numeral.

sorted() – Use this function to sort the entries of a list.

range() – This function gives a list of numbers between two specific values. You

need to set the said values as the function’s arguments.

Lists

Most programming languages use arrays. An array is a collection of different objects. You

may retrieve an entry from an array by specifying the position of the former. For example,

you can get the fourth value of an array by typing [4]. Python has a similar feature, but it

is known as “list”.

Python lists are “iterable”. That means you can use them for your loop statements (you’ll

learn more about loops later). Let’s assume that you want to retrieve the third element of

the “SampleList” (i.e. the one you created earlier). Here are the things that you should do:

1. Type the word “print”. This command allows you to display information.

2. Specify the name of the list (i.e. SampleList).

3. Add a pair of brackets.

4. Insert “2” between the brackets. This number signifies the position of the item you

want to retrieve. It is important to note that the numbering begins at zero. Thus,

typing “1” will give you the second element, typing “2” will give you the third

element, etc.

The Python script should look like this:

print SampleList[2]

If you did everything correctly, your terminal should display this:

30

LEAVE A REPLY

Please enter your comment!
Please enter your name here