Numerical Data Types

Python includes a range of numerical data types to capture the range of numbers you require.

Integers

These are created when a variable is assigned a number without a decimal point.
Other numerical types can be converted into integers with the int() function, though care should be taken as accuracy maybe lost.
HouseNo = 87
Seconds = -1243
Shoesize = 11
Rating = 2.3 # note this is a float
print (HouseNo)
print (int(Rating))
Returns:
87
2

Long

These are long integers of unlimited size these behave much like integers though they are indicated with a capital L, to signal a long number.

Float

These numbers feature a decimal point.
HouseSize = 4.3
ratio = 0.1243
Percentage = -11.02
items = 2 # note this is an integer
print (HouseSize)
print (float(items))
Returns:
4.3
2.0
Care should be taken when rounding floating point numbers as 2.5 may be rounded down due to the way the computer stores this is memory in binary.

Complex

Complex number can be created by specifying a real and an imaginary part, denoted in the for a + bj.

var1 = 45 + 3j
var2 = 4j
print (var1)
print (var2)
Returns:
(45+3j)
4j

Numerical Operators

The notation for common mathematical functions are stated below.
Example                  Meaning
a + b                    Addition
a - b                    Subtraction
a * b                    Multiplication
a / b                    Division
a % b                    Modulus
a ** b                   Exponent
a // b                   Floor Division
a = 5
b = 12

print(a + b)
print(a - b)
print(a * b)
print(a / b)
print(a % b)
print(a ** b)
print(a // b)
Returns:
17
-7
60
0.4166666666666667
5
244140625
0

Comparisons

The notation for common mathematical functions are stated below. These return true or false, allowing the flow to be controlled.
Example                  Meaning
a == b                   Equal to
a != b                   Not Equal to
a > b                    Greater Than
a < b                    Less Than
a >= b                   Greater or Equal to
a <= b                   Less or Equal to
a = 5
b = 12

print(a == b)
print(a != b)
print(a > b)
print(a < b)
print(a >= b)
print(a <= b)
Returns:
False
True
False
True
False
True
Note that == is not the same as =, = is defining a variable where as == is comparing.



Related Sections

Python - Learn the python from the basics up. This fast track example code course will get you creating powerful python programs in no time.
Tkinter - Learn the key features of the tkinter to allow you to create user interfaces for your python programs.


Related Pages

Pickle - The pickle library allows a program to save data in a binary file.
Python - List Comprehension - Create powerful list comprehension expressions.
Python - Generators - Learn about generator statements.
Python - Data types - The learn about the key datatypes in the python language.
Tkinter - Combo Box - This is a short example showing how to use combo boxes in a GUI.
Tkinter - Radio Buttons - A short example of how to create a GUI with radio buttons.
Tkinter - Canvas - Canvas allows for a range of shapes to be plotted, such as circles, squares, lines