Introduction to Pearl

 

Keywords

Perl Keywords
and del from None True
as elif global nonlocal try
assert else if not while
break except import or with
class False in pass yield
continue finally is raise  
def for lambda    

Arithmetic Operators

Just like mathematics, Python has several arithmetic operators that you can use for computation. They are:

Python Arithmetic Operators
Operator Meaning Example Outcome
+ Add x, y  = 3,  4
z = x + y
z is equal to 7
- Subtract x, y = 3, 4
z = x - y;
z is equal to -1
* Multiply x, y = 3, 4
z = x * y
z is equal to 12
/ Divide x, y = 6, 2
z = x / y
z is equal to 3
% Modulus x, y = 5, 3
z = x % y
z is equal to 2
** Exponent x, y = 2, 3
z = x ** y
z is equal to 8
// Floor Division x, y = 20, 12
z = x // y
z is equal to 1

There are a couple of these operators that need some explanation. First the modulus operator will return the remainder for a division

  • 10 % 3 results in 1 be case 10 / 3 = 3 with a remainder of 1
  • 12 % 7 results in 5 Because 12 / 7 = 1 with a remainder of 5

The floor division operator gives a division of a number where the numbers after the decimal point are truncated.

  • 15 // 4 is 3 because 4 goes into 15 three times; the remainder is truncated.

In other high level languages whole number division gives you a whole number result. This is not the case in Python. If you divide two whole numbers like

Escape Characters

The following table is a complete list of escape characters that can be used to format strings.

Python Escape Characters
Character Description
 \a  Bell 
 \b  Backspace
 \Cx  Control-x
 \e  Escape
 \f  Formfeed
 \M-\C-x  Meta-Control-x
 \n  Newline
\r  Carriage Return
\s  Space
\t  Tab
\v  Vertical Tab
\x  Character x 
\xnn  Hexadecimal notation. Where n is in the range of 0-9, a-f or A-F

Bitwise Operators

Bitwise operators in Python
Operator Meaning Example
& Bitwise AND x& y = 0 (0000 0000)
| Bitwise OR x | y = 14 (0000 1110)
~ Bitwise NOT ~x = -11 (1111 0101)
^ Bitwise XOR x ^ y = 14 (0000 1110)
>> Bitwise right shift x>> 2 = 2 (0000 0010)
<< Bitwise left shift x<< 2 = 40 (0010 1000)