# Python Numbers

Python allows three ways to represent numbers.

• int – whole numbers such as 1, 20, 100000, -3, and 0.
• float – numbers that have decimal points such as 0.1, 31.2, and -11.2.
• complex – numbers consists of real and imaginary parts. Examples: 3+5j, 3j, and -2j, where j is the imaginary part.

## Integers

Integers are whole numbers with no fractions or decimals. You can represent an integers by a sequence of digits. Following are examples of “literal integers”.

``````>>> 123
123
>>> -20
-20``````

By default, sequence of digits represent a positive number. You can add ‘+’ at the beginning of the sequence and it would still remain as positive number. To represent negative integers, add ‘-‘ before the digits.

You can represent big numbers by using “_” (underscore) and this makes it easy to read numbers. You can represent 100 million using the format as follows:

``````>>> 100_000_000
100000000``````

### Operations

Python support normal math operations on integers.

### Integer Variables

You can assign integer literals to variables and then do various operations on those variables. Like “strings”, integers are immutable.

``````# assign a value to integer
>>> a = 101

# subtract 55 from a
>>> a - 55
46
# divide a by 2
>>> a /2
50.5

# get the exponent of a
>>> a ** 2
10201``````

### Precedence

What would be the output of the following?

``>>> 2 + 3 * 5``

Got 25? Unfortunately, that is wrong. This is because multiplication has higher precedence than addition. Most programming languages have the concept of “precedence”, which is set of rules about the order of operations. By default, the operations are performed from left to right but operations with higher precedence are always executed first.

For the above question, the right answer is 17 since “3 * 5” is evaluated before adding it with 2. If you really want to get an output of 25, you can make a slight modification.

``>>> ( 2 + 3 ) * 5``

The parentheses or the bracket will force “2 + 3” to be executed before multiplying with 5. Please see here for a complete reference on operator precedence.

### Decimal, Binary, Octal and Hex

Integers have “base 10”, meaning there are 10 numbers (from 0 to 9) to represent any integer of “base 10”. With “base 10”, we count from 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 and then we start the sequence by adding “1” on the left or 10th place. Base essentially defines how many digits you can use to represent any number. Other commonly used bases are “binary”, “octal” and “hex”.

The binary base system has only two numbers – 0 and 1. You can represent any number using these two numbers and this is the foundation of representing data in computers.

The octal (base 8) system has 8 digits – 0, 1, 2, 3, 4, 5, 6, and 7.

The hex (base 16) system has 16 digits. They are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F.

You can specify different bases using prefixes as follows:

• 0b for binary (base 2)
• 0o for octal (base 8)
• 0x for hex (base 16)

Here are some examples and please note that Python interactive interpreter will print the values in “base 10” format automatically.

``````>>> 0o10
8
>>> 0b1111
15
>>> 0xFFFF
65535
>>>``````

### Reference

See the tutorials from Khan Academy to learn more about different number systems.

### Try it yourself

• The side of a cube has a length of 10 inches. Calculate the volume of the cube using Python.
• Assume that number of views of a popular Youtube video is doubling every day. On the first day, there were 20 viewers. Calculate the number of viewers at the end of the 5th day.
• There are 20 candies and they are equally divided among 7 students. Figure out the remaining number of candies using a single Python operator.
• It takes 7 days to walk from point A to point B. Calculate the total time required in seconds using Python operators.
0 replies

### Leave a Reply

Want to join the discussion?
Feel free to contribute!