Operators are essentially functions that do something in your code, which are represented by
symbols or keywords. They usually require pieces on information that they can work on, which
are called operands. For example, if you have the expression 4 + 5, the plus (+) sign is the
operator, and the numbers 4 and 5 are operands.
Python Operators
Take a look at how expressions look like in an interpreter prompt:
When you evaluate expressions in an interpreter prompt and you used the right syntax, you will be
able to see the result that you are expecting right after the logical line. Since you will be
producing codes for your own hacking tools, you will need to memorize how operators are used
in a code.
Also take note that Python uses the operators according to precedence. That means that when you
ask your code to perform certain operations that have higher precedence. For example, Python
will always perform operations that require it to divide or multiply variables over operations that
require it to add or subtract. If two operators have the same value of precedence, then Python will
evaluate them from left to right.
Here is a list of the operators that are available in Python.
•Plus (+)
Adds two objects.
For example:
4 + 5 will give you 9, and ‘e’ + ‘j’ gives you ‘ab’
•Minus (-)
Subtracts one number from another number. In case that the first operand in the equation is
absent, Python assumes that it is zero.
For example:
-87 will give you a negative number, and 80 – 40 gives you 40.
•Multiply (8)
Multiplies to numbers or repeats a string a certain number of times.
For example:
2*5 gives you 10, and ‘ha’ * 3 will give you ‘hahaha’
•Power (**)
Raises a certain number to the power of the next operand.
For example:
3 ** 3 will give you 9 (this is computed as 3 * 3 * 3)
•Divide (/)
Divides the first operand with the next one.
For example:
4 / 2 gives 2
•Divide and floor (//)
Divides the first operand with the next one, and then rounds the result to the nearest
number.
For example:
5 // 2 gives you 2
•Modulo (%)
Gives you the remainder of a division
For example:
13 % 3 will give you 1
•Less than (<)
Gives you a result of whether the first operand is less than the next one. The comparison
operator will say whether it is TRUE or FALSE.
For example:
3 < 9 returns gives you TRUE
•Greater than (>)
Gives you a result of whether the first operand is greater than the next one. The operator
will also say whether it is TRUE or FALSE.
For example:
9 > 3 gives you TRUE
•Less than or equal to (<=)
Gives you a result of whether the first operand is less than or equal to the next one.
For example:
x = 6; y = 9; x <= y gives you TRUE
•Greater than or equal to (>=)
Gives you a result of whether the first operand is greater than or equal the next one
For example:
x = 6; y = 3; x >= y gives you TRUE
•Equal to (==)
Tells you if to operands are equal.
For example:
x = 3; y = 3; x == y gives you TRUE
•Not equal to (!=)
Tells you if the operands are not equal
For example:
x = 3; y = 4; x != y gives you TRUE
Expressions
Expressions are combinations of operators and values in your code. You can think of it as
anything that “expresses” something that has a value. For example, if you use the function eval(1 +
1), you will get a result that provides you the value of these two numbers added together.
Take a look at this example:
Save this as expression.py and then run it at the interpreter prompt. You should be able to get this
output:
As you may have noticed, Python stored values in the variables ‘length’ and ‘breadth’, and you
are able to calculate the perimeter and the area of a rectangle using these expressions. You are
also able to store the value of the expression length * breadth in another variable, which is named
area, and then displayed it using the print function.
Now that you are aware of how you can use the building blocks of a programming language, you
can now ready to learn how you can use them in a code!
0 Comments