0% found this document useful (0 votes)
19 views

Python Note 3

Personal Python Learning Note

Uploaded by

brian07151992
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Python Note 3

Personal Python Learning Note

Uploaded by

brian07151992
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Basic operators

An operator is a symbol of the programming language, which is able to


operate on the values.

For example, just as in arithmetic, the + (plus) sign is the operator which is
able to add two numbers, giving the result of the addition.

Not all Python operators are as obvious as the plus sign, though, so let's go
through some of the operators available in Python, and we'll explain which
rules govern their use, and how to interpret the operations they perform.

We'll begin with the operators which are associated with the most widely
recognizable arithmetic operations:

+ , - , * , / , // , % , **

The order of their appearance is not accidental. We'll talk more about it once
we've gone through them all.

Remember: Data and operators when connected together


form expressions. The simplest expression is a literal itself.

Arithmetic operators: exponentiation

A ** (double asterisk) sign is an exponentiation (power) operator. Its left


argument is the base, its right, the exponent.

Classical mathematics prefers notation with superscripts, just like this: 23.
Pure text editors don't accept that, so Python uses ** instead, e.g., 2 ** 3 .

Remember: It's possible to formulate the following rules based on this result:

 when both ** arguments are integers, the result is an integer, too;


 when at least one ** argument is a float, the result is a float, too.
Arithmetic operators: multiplication

An * (asterisk) sign is a multiplication operator.

Arithmetic operators: division

A / (slash) sign is a divisional operator.

The value in front of the slash is a dividend, the value behind the slash,
a divisor.

The result produced by the division operator is always a float,


regardless of whether or not the result seems to be a float at first glance: 1 /
2, or if it looks like a pure integer: 2 / 1.

Arithmetic operators: integer division

A // (double slash) sign is an integer divisional operator. It differs from the


standard / operator in two details:

 its result lacks the fractional part - it's absent (for integers), or is
always equal to zero (for floats); this means that the results are
always rounded;
 it conforms to the integer vs. float rule.

 The result of integer division is always rounded to the nearest integer


value that is less than the real (not rounded) result.
 This is very important: rounding always goes to the lesser
integer.

Look at the code below and try to predict the results once again:

print(-6 // 4)
print(6. // -4)
Note: some of the values are negative. This will obviously affect the result.
But how?

The result is two negative twos. The real (not rounded) result is -1.5 in both
cases. However, the results are the subjects of rounding. The rounding goes
toward the lesser integer value, and the lesser integer value is -2 ,
hence: -2 and -2.0 .

Integer division can also be called floor division.

Operators: remainder (modulo)

Its graphical representation in Python is the % (percent) sign, which may look
a bit confusing.

Try to think of it as of a slash (division operator) accompanied by two funny


little circles.

The result of the operator is a remainder left after the integer division.

In other words, it's the value left over after dividing one value by another to
produce an integer quotient.

Note: the operator is sometimes called modulo in other programming


languages.

Take a look at the snippet - try to predict its result and then run it:

print(14 % 4)

As you can see, the result is two. This is why:

 14 // 4 gives 3 → this is the integer quotient;


 3 * 4 gives 12 → as a result of quotient and divisor multiplication;
 14 - 12 gives 2 → this is the remainder.

Operators: how not to divide

As you probably know, division by zero doesn't work.


Do not try to:

 perform a division by zero;


 perform an integer division by zero;
 find a remainder of a division by zero.

Operators: addition

The addition operator is the + (plus) sign, which is fully in line with
mathematical standards.

The subtraction operator, unary and binary

operators

The subtraction operator is obviously the - (minus) sign, although you


should note that this operator also has another meaning - it can change the
sign of a number.

This is a great opportunity to present a very important distinction


between unary and binary operators.

In subtracting applications, the minus operator expects two arguments:


the left (a minuend in arithmetical terms) and right (a subtrahend).

But the minus operator may be used in a different (unary) way - take a look at
the last line of the snippet below:

print(-4 - 4)
print(4. - 8)
print(-1.1)

Operators and their priorities


Multiplications precede additions.

Some operators to act before others is known as the hierarchy of


priorities.

Operators and their bindings

The binding of the operator determines the order of computations performed


by some operators with equal priority, put side by side in one expression.

Most of Python's operators have left-sided binding, which means that the
calculation of the expression is conducted from left to right.

This simple example will show you how it works. Take a look:

print(9 % 6 % 2)

There are two possible ways of evaluating this expression:

 from left to right: first 9 % 6 gives 3 , and then 3 % 2 gives 1 ;


 from right to left: first 6 % 2 gives 0 , and then 9 % 0 causes a fatal
error.

Run the example and see what you get.

The result should be 1 . This operator has left-sided binding. But there's one
interesting exception.

Operators and their bindings: exponentiation

Use this snippet of code:

print(2 ** 2 ** 3)

The two possible results are:

 2 ** 2 → 4 ; 4 ** 3 → 64
 2 ** 3 → 8 ; 2 ** 8 → 256
Run the code. What do you see?

The result clearly shows that the exponentiation operator uses right-
sided binding.

Key takeaways

1. An expression is a combination of values (or variables, operators, calls to


functions ‒ you will learn about them soon) which evaluates to a certain
value, e.g., 1 + 2 .

2. Operators are special symbols or keywords which are able to operate on


the values and perform (mathematical) operations, e.g., the * operator
multiplies two values: x * y .

3. Arithmetic operators in
Python: + (addition), - (subtraction), * (multiplication), / (classic division ‒
always returns a float), % (modulus ‒ divides left operand by right operand
and returns the remainder of the operation, e.g., 5 % 2 =
1 ), ** (exponentiation ‒ left operand raised to the power of right operand,
e.g., 2 ** 3 = 2 * 2 * 2 = 8 ), // (floor/integer division ‒ returns a number
resulting from division, but rounded down to the nearest whole number,
e.g., 3 // 2.0 = 1.0 )

4. A unary operator is an operator with only one operand, e.g., -1 , or +3 .

5. A binary operator is an operator with two operands, e.g., 4 + 5 , or 12 % 5 .

6. Some operators act before others – the hierarchy of priorities:

 the ** operator (exponentiation) has the highest priority;


 then the unary + and - (note: a unary operator to the right of the
exponentiation operator binds more strongly, for example: 4 ** -
1 equals 0.25 )
 then * , / , // , and % ;
 and, finally, the lowest priority: the binary + and - .

7. Subexpressions in parentheses are always calculated first, e.g., 15 - 1 *


(5 * (1 + 2)) = 0 .
8. The exponentiation operator uses right-sided binding, e.g., 2 ** 2 **
3 = 256 .

You might also like