Numbers in Python

by Alex
Numbers in Python

Different kinds of numbers are used in the Python programming language to process data related to quantitative information and to perform arithmetic operations: integers, real numbers, and complex numbers. The standard Python library contains a lot of useful methods that allow you to easily interact with numeric data types.

Integers

The easiest and most common type of number in Python, as in many other programming tools, is integer data type. To represent it, numeric values are used, which are usually assigned to specific variables. Below is an example of an integer assignment in a program that creates an integer variable named i and then displays its value and type:

i = 42 # integer
print("i = " + str(i))
print(type(i))

i = 42
<type 'int'>

Real numbers

In order to write a real number in Python, there are floating point numbers. By working with such values in a program, you can get more accurate data in arithmetic calculations. To initialize an object that contains a real number, you must assign it a value, with integer and fractional parts separated by a dot character. This example shows the creation of the variable f:

f = 3.14 # real number
print("f = " + str(f))
print(type(f))

f = 3.14
<type 'float'>

Complex numbers

The Python programming language also supports working with complex numbers. Such information is represented as a pair of values: the real and imaginary parts, which are separated by the symbol of the addition operation. Also, at the end of the imaginary part of a complex number there must be the letter j. The following code fragment demonstrates the creation of the complex variable c and displays all information about it:

c = 1 + 2j # complex number
print("c = " + str(c))
print(type(c))

c = (1+2j)
<type 'complex'>

It is possible to get the real and imaginary parts of numbers separately. To do this, use real and image:

c = (1+2j)
print(c.real)
print(c.imag)

1.0
2.0

Number systems

In addition to the decimal number system, Python supports binary, octal and hexadecimal conversions. To start working with these numbers you must initialize the variables with constants that have the prefixes 0b, 0o and 0x for the type you choose. The program below creates and displays data in various systems:

b = 0b110101 # number in binary system
o = 0o342 # number in octal system
x = 0x1FE9 # number in hexadecimal system
print("b = " + str(b))
print("o = " + str(o))
print("x = " + str(x))

b = 53
o = 226
x = 8169

Type conversion

Using the standard Python language tools, you can quite easily change the type identity of almost any variable. To do this, there are methods such as int, float, complex, bin, oct, and hex. The last three functions let you convert a number or variable to decimal, octal, or hexadecimal respectively. The following code snippet demonstrates how all six of the above methods work:

i = int(67.23) # a real number is truncated to an integer
f = float('1304') # the string becomes a real number
c = complex(2, 6) # complex number is formed
b = bin(42) # converts number to binary
o = oct(993) # number converts to octal
x = hex(4152) # number converted to hexadecimal
print("i = " + str(i))
print("f = " + str(f))
print("c = " + str(c))
print("b = " + str(b))
print("o = " + str(o))
print("x = " + str(x))

i = 67
f = 1304.0
c = (2+6j)
b = 0b101010
o = 01741
x = 0x1038

Operations on numbers

Manipulations on numeric values in the Python programming language are performed thanks to a variety of different operations, whose symbolic designations tend to coincide with their counterparts in traditional mathematics. Thus, you can add, subtract, multiply, divide, find the remainder of a division, and raise numeric values of any variety. To familiarize yourself with the basic types of operations that are performed on numeric values, see the following small table:

Operation Destination
a + b Adding a and b
a – b Difference between a and b
a * b Multiplication of a by b
a / b Dividing a by b
a % b Residue from dividing a by b
a // b Integer part of a and b
a ** b Raising a to the power of b

In addition to the arithmetic operations listed above, you can perform bit operations on numbers, which involve their binary representation. A list of such operations is shown in the following table, where you can find the purpose of each of them:

The operation Purpose
a & b Bitwise AND for a and b
a | b Bitwise OR for a and b
a ^ b Exclusive OR for a & b
~a Bit inversion for a
a << b Left bit shift for a to b
a >> b Bitwise shift to the right for a >> b

Also, in order to process numerical data more efficiently, special methods have been added to Python that allow many complex operations to be performed in just one action. Among the most popular of these are methods for quickly finding the square root, modulus, and rounding a number. To use some of the arithmetic functions, you must connect to the standard math library with an import math call. A list of popular methods is shown in this table:

Method Assignment
sqrt(a) The square root of a
log(a) The natural logarithm of a
fabs(a) Returns the modulus of a
round(a) Rounds a to the nearest integer
round(a, b) Rounds a to b decimal places
floor(a) Rounds a to the smallest whole value
ceil(a) Rounds a to a higher integer value
isfinite(a) Checks if a is a number
modf(a) Returns the integer and fractional parts of a
sin(a) The sine of the angle a, given in radians
cos(a) The cosine of the angle a, given in radians
tan(a) Tangent of the angle a, given in radians

The math module’s fabs function first tries to convert the argument to a real type (float), and only then calculates the modulus. To calculate the modulus of a number, there is also the standard abs function.

Entering a number from the keyboard

To get numeric data from the user, the standard method input is used. It is used to get information from the keyboard, and it is executed when you run a program on your computer. A string can be used as an argument to this method inviting the user to enter numerical information. An example of how to enter a number into Pyhon from the keyboard is shown below. The variable n receives a value and is displayed on the screen using print:

n = input("Press n: ")
print("n = " + str(n))

Press n: 10
n = 10

Maximum value

You can get the maximum value of an integer variable that the current version of the Python language supports by using the sys.maxsize variable. Generally, this number will not be the same on different computers due to different processor architectures. Python has now removed any restrictions on the dimensionality of input values. You can display the maximum number in Python as follows:

import sys
print(sys.maxsize)

9223372036854775807

Conclusion

The Python programming language uses three kinds of numerical constants to process numerical data: integers, real numbers, and complex numbers. Standard mathematical operations on numbers are usually performed using a set of common symbols for addition, subtraction, multiplication, and division. To perform specific operations on numeric values, various methods from the standard Python library are used.

Related Posts

LEAVE A COMMENT