The Differences Between the Types

Within Python, there are a few numeric types that we will often use within the scope of Ignition: integers, floats and booleans. Integers have at least 32 bits of precision and consist of non decimal values (1, 2, 50, 246). When working with larger numbers, Python also supports a long type that has unlimited precision. Floats consist of numbers with decimal values (1.0, 2.4, 50.7, 246.8734). Booleans are a unique subset of integers. They are either True or False, but can also be represented using a 1 or 0, respectively.

On this page ...

Numbers in Arithmetic

When using numeric values in expressions or equations, Python is very flexible in how it handles multiple types in the same operation. When using two of the same type in an expression, the outcome will always be of that type. Adding two integers together yields an integer, while multiplying two floats will yield a float. With division, using integers will always result in a floor division so that the answer remains an integer. Floor division will simply remove the decimal, and will not round up at any point.

When using different types, the more precise type will be used for the outcome. Doing an operation with one integer and one float will produce an outcome that is a float. Adding a float and an integer together will yield a float. 

Below, we have some examples that show off some of the different ways that Python handles expressions. You can try these out in the interactive interpreter of the Script Console.

Type of ArthimeticDescriptionExampleOutput
Addition and Subtraction

Adding/Subtracting integers will return an Integer.

If at least one of the numbers is a float, then the resulting number will also be a float. Otherwise the result will
be an integer

4 + 5 # int + int = int
4.0 + 5 # float + int = float
4.0 + 5.3 # float + float = float

9

9.0

9.3

MultiplicationSimilar to Addition and Subtraction, multiplication will only return a float if at least one of the numbers is a float
4 * 5 # int * int = int
4 * 5.0 # int * float = float

20

20.0

Division

Dividing an integer by another integer results in floor division: meaning the remainder of division
is never returned. Note that this is not the same as rounding to the nearest integer.

To include the remainder, at least one of the numbers needs to be a float

4 / 5 # int / int = int
4 / 5.0 # int / float = float

0

0.8


Operators and Functions

Python offers a wide range of operators that can be used to perform calculations on numeric values. Additionally, there are many functions that can perform specific operations to numeric values. The tables below contain some of the most common operations and functions, but this is not an exhaustive list. 

Operators
OperatorDescriptionExampleTypes that use the Operator
+

Used to find the sum of two numeric values

Can also be used to concatenate string values

5 + 1 = 6

"Hello " + " World"

Numeric Types, Strings

-Used to find the difference of two values
5 - 1 = 4

Numeric Types

*Used to find the product of two values
5 * 2 = 10
Numeric Types
/Used to find the quotient of two values
4 / 2 = 2

Numeric Types

%

Modulus operator. When used with numeric types, returns the remainder of the quotient of two values

The operator is also seen in string formatting.

5 % 2 = 1

Numeric Types, Strings

**Used to find a value to the power of another value
5 ** 2 = 25
Numeric Types
Functions
FunctionDescriptionExample



int(x)Converts x to an integerint(4.8) = 4
long(x)Converts x to a longlong(5.7) = 5
float(x)Converts x to a floatfloat(4) = 4.0
abs(x)Absolute value of xabs(-4.6)=4.6
round(x[, n])Rounds x to n digits. If n is not specified, default is 0

round(5.674)=6.0

round(5.674, 2)=5.67



  • No labels