Integers in Python

We’ll start our survey of data types in Python with the most basic type: numbers. If you’re used to other programming languages, you might expect only the two most basic numeric types: integers and floats, or floating point numbers. Python offers support for these, but also for several other more general numeric types.

First, a reminder that Python does not require (or even allow) you to declare a specific data type for a variable. Variables are dynamically typed, which means that its data type is inferred from the data that the variable currently holds.

In the following, you will find it easiest to understand the discussion if you have a Python console into which you can type some test statements. As these examples are all quite short, you’ll probably find it easier to use the interactive console, rather than creating a Python project and writing Python source code files.

Basic arithmetic

The four standard arithmetic operations are supported as you’d expect, with the operators +, -, * and / for addition, subtraction, multiplication and division. The % operator gives the integer remainder when its first operand is divided by its second.

Try a few examples in the console, such as (type each statement, then press Enter to see the result in each case). Although Python doesn’t require you to type a blank on either side of an operator, it’s considered good code etiquette to do so. Thus 13+42 will give the same answer as 13 + 42, but the latter makes code easier to read. For simple console expressions such as these, you can get away without putting in the blanks, but when you write more substantial code, it’s a good idea to put them in.

You should try these examples yourself before expanding the ‘See answers’ block below.

13 + 42
13 - 42
13 * 42
13 / 42
42 % 13
See answers

The first three answers are what you’d expect, but if you’re used to integer arithmetic in other languages like Java or C#, you might be surprised by the result of the division. Most common languages return only the integer (whole number) part of the quotient of two integers, so 13/42 would give zero. Python’s division operator treats the quotient of two integers as a float, so you get the fractional part of the quotient as well.

If you want a true integer division result, you can use the // operator (known as the floor operator), which returns the integer part of the quotient and throws away the fractional part, so 42 // 13 = 3 and 13 // 42 = 0.

Unlimited integer size

Python provides the ** operator for exponentiation. Thus 2 ** 4 raises 2 to the 4th power. One nice feature of Python is that integers can grow to any size. Try typing 2 ** 100 into the console.

See answer

This is about 1.26 \times 10^{31}, considerably bigger than standard integers variables that are allowed in Java or C#.

To get a feel for just how big integers can get in Python we can calculate 2 ** 2000000 (2 to the power of two million). Don’t type this in just yet, though – you will be waiting a while for the answer to be printed. Rather, we can see how many digits this result contains by giving the command

len(str(2 ** 2000000))
See answer

That is, the answer contains more than 600,000 digits! For reference, the function str() converts its argument to a string, and len() gives the length of the string.

Bitwise logical operators

Besides the 5 arithmetic operators discussed above, Python provides the standard set of bitwise logical operators. To apply bitwise operators to two integers, the integers are first expressed in binary, and then the operator is applied to each pair of corresponding bits in the two integers. 

The bitwise OR operator is |, the vertical bar. To calculate 2 | 4, for example, we write 2 as 010 in binary and 4 as 100. We need to add zeros to the left hand side of the smaller number so that it contains the same number of bits as the larger number.

To calculate 2 | 4, we just apply the OR operation to each pair of corresponding bits from the two numbers, so we get 2 | 4 = (0 | 1)(1 | 0)(0 | 0) = 110 = 6 in decimal. 

The bitwise AND is denoted with &, so we have 2 & 4 = 0, since each 1 in one of the numbers has a 0 in the corresponding place in the other.

Finally, we have the XOR operator ^. An XOR (exclusive OR) is 1 (true) only if one of its operands is 0 and the other is 1. Thus 2 ^ 4 = 6.

Here are a few for you to try. You should try to figure out the answer yourself before typing it into the console.

1 | 9
1 & 9
1 ^ 9
10 | 5
10 & 5
10 ^ 5
See answers
1 | 9 = 9
1 & 9 = 1
1 ^ 9 = 8
10 | 5 = 15
10 & 5 = 0
10 ^ 5 = 15

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.