Operators

Operators in Python are symbols that represent functional executions.

Note

More details about this can be found at https://docs.python.org/2/library/operator.html.

The important thing to remember is that Python has extensive capabilities that allow complex mathematical and comparative operations. Only a few of them will be covered here to prepare you for more detailed work.

Comparison operators

A comparison operator checks whether a condition is true or false based on the method of evaluation. In simpler terms, we try to determine whether one value equals, does not equal, is greater than, is less than, is greater than or equal to, or is less than or equal to another value. Interestingly enough, the Python comparison operators are very straightforward.

The following table will help define the details of operators:

Comparison test

Operator

Are the two values equal?

==

Are the values not equal?

!=

Is the value on the left greater than the value on the right?

>

Is the value on the left less than the value on the right?

<

Is the value on the left greater than or equal to the value on the right?

>=

Is the value on the left less than or equal to the value on the right?

<=

Assignment operators

Assignment operators confuse most people when they transition from a different language. The reason for this is that AND assignment operators are different from most languages. People who are used to writing incrementors short hands of variable = variable + 1 from in other languages using the format variable++, they are often confused to see the exact operation is not done in Python.

The functional equivalent of a variable incrementor in Python is variable=+1, which is the same as variable = variable + 1. You might notice something here, however; you can define what is added to the variable in this expression. So, instead of the double addition sign, which means, "add 1 to this variable," the AND expression allows you to add anything you want to it.

This is important when you write exploits, because you can append multiple hexadecimal values to the same string with this operator, as shown in the previous string concatenation example, where two strings were added together. Chapter 8, Exploit Development with Python, Metasploit, and Immunity, will cover more of this when you develop a Remote Code Execution (RCE) exploit. Until then, consider this table to see the different assignment operators and what they are used for:

Assignment action

Operator

Set a value to something

=

Add a value to the variable on the left, and set the new value to the same variable on the left

+=

Subtract a value from the variable on the left, and set the new value to the same variable on the left

-=

Multiply a value by the variable on the left, and set the new value to the same variable on the left

*=

Divide a value by the variable on the left, and set the new value to the same variable on the left

/=

Arithmetic operators

Arithmetic operators are extremely simple overall and are what you would expect. Addition executions use the + symbol, subtraction executions use -, multiplication executions use *, and division executions use /. There are also additional items that can be used, but these four cover the majority of cases you are going to see.

Logical and membership operators

Logical and membership operators utilize words instead of symbols. Generally, Python's most confusing operators are membership operators, because new script writers think of them as logical operators. So let's take a look at what a logical operator really is.

A logical operator helps a statement or a compound statement determine whether multiple conditions are met so as to prove a true or false condition. So what does this mean in layman terms? Look at the following script, which helps determine whether two variables contain the values required to continue the execution:

#!/usr/bin/env python

a = 10
b = 5
if a == 10 and b == 5:
    print("The condition has been met")
else:
    print("the condition has not been met")

Logical operators include and, or, and not, which can be combined with more complex statements. The not operator here can be confused with not in, which is part of a membership operator. A not test reverses the combined condition test. The following example highlights this specifically; if both values or False or not equal to each other, then the condition is met; otherwise, the test fails. The reason for this is that the test checks whether it is both. Examples similar to this do surface, but they are not common, and this type of code can be avoided if you are not feeling comfortable with the logic flow yet:

#!/usr/bin/env python

a = False
b = False
if not(a and b):
    print("The condition has been met")
else:
    print("The condition has not been met")

Membership operators, instead, test for the value being part of a variable. There are two of these types of operators, in and not in. Here is an example of their usage:

#!/usr/bin/env python

variable = "X-Team"

if "Team" in variable:
    print("The value of Team is in the variable")
else:
    print("The value of Team is not in the variable")

The logic of this code will cause the statement to return as True and the first conditional message will be printed to screen.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.15.135.175