Computers, as befits their name, are made to perform calculations or computations with numbers. As a programming language for computers, a core capability of Python is to, well, perform calculations! Python has built-in capabilities that enable the language to perform simple and complex math equations. You may also find other uses for including numbers in your Python program such as collecting numeric input from a user or relying on a numeric value to determine what action should occur in your program.
Before you begin using Python to solve math equations, there are two number types that you should know: int and float.
int (short for integer) is a whole number. This means the number never shows a decimal point. Also, integers can be either positive, negative, or zero. Here are some examples of integers:
float is any number that contains a decimal point. Like integers, floats can also be positive, negative, or zero. Here are some examples of floats:
Given any number, the
type() function tells you whether it's an
int or a
Let's use the
type() function in IDLE to see the type of the following numbers:
<class 'int'> ...
Get Bite-Size Python now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.