Python for Beginners: Numeric Types
Python, like most programming languages, has built-in numeric data types. Integers and floats are the two common numeric types in Python. Python supports other types of numbers such as Decimal, Fraction, and complex numbers. But in this mini-tutorial, we’ll focus on learning about integers and floats.
Number values help us represent all kinds of information. Numbers can be data from a visualization, the score in a video game, web application data, and so on. Let’s take a look at the operators available for numeric data types.
Python supports the order of operations. Parentheses can be used to alter the order of operations. The following table lists all the math operators in Python from highest to lowest precedence.
Operator | Operation | Example | Result |
---|---|---|---|
** | Exponent | 2 ** 2 | 4 |
% | Modulus/Remainder | 10 % 3 | 3 |
// | Integer Division | 18 // 3 | 6 |
/ | Division | 100 / 25 | 4 |
* | Multiplication | 2 * 3 | 6 |
- | Subtraction | 9 - 5 | 4 |
+ | Addition | 4+ 4 | 8 |
Here are some expressions using the operators:
>>> 2 - 10 + 32
24
>>> (3 * (4 - 2)) / 1
6.0
>>> 2 ** 5
32
Integers⌗
Integers are whole numbers. They have no decimal or fractional part. Integers include both negative and positive whole numbers, and the number zero. Examples of integers are: 0, -1, -199, 2, 3, 99, -2.
In Python, an integer has type int. Take careful notice when using the / and // operators. The // operator should be used when you want an integer result. Here are examples of expressions involving integers:
>>> 22 // 9
2
>>> x = 8 % 6
2
>>> y = 9 * x
9
Floats⌗
Floats are numbers with a decimal point, such as 2.71828. Float is short for floating-point number. This term is used in other programming languages. Examples of floats are: -1.0, 3.14, 0.3333333, 0.25, 0.0.
Floats have type float. If an expression contains mixed type operands, then the result will always be a float. Here are examples to illustrate:
>>> 2.0 + 4.2
6.2
>>> 1 + 2 + 3 + 4.0
10.0
>>> 0.0 + 100
100.0
Miscellaneous Tidbits⌗
Built-in Functions⌗
The int()
and float()
are built-in functions for converting a number or string into the respective type.
>>> int("22")
22
>>> int(3.14)
3
>>> float("2.7")
2.7
String Concatenation⌗
If you try to concatenate a number to a string, the following error will occur:
>>> "Hello " + 20
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "int") to str
The number needs to be explicitly converted to a string because Python doesn’t do this automatically—unlike other programming languages like Java or JavaScript.
Infinity⌗
Use the math
module or float()
for infinity or negative infinity.
>>> import math
>>> math.inf > 9999
True
>>> float("-inf") < 0
True
Useful Packages⌗
Here are some numeric and mathematical related packages that every Python developer should know:
- numpy is the fundamental package for scientific computing
- pandas is a data analysis and manipulation tool
- The Python Standard Library
Summary⌗
This mini-tutorial should give you a better understanding of the int and float types in Python. Floats should be used when precision is an absolute concern. Integers, however, are used when the fractional part is not needed for computation or manipulation.
Here are additional resources to help you get started learning Python:
- Automate The Boring Stuff With Python
- Python Crash Course
- Fluent Python
- Learn Python - Full Course For Beginners
- Google’s Python Class
Python Challenges:
- Implement a script that adds three integers and returns the floating-point representation
- Implement a script that takes in a user’s numeric input from the command line and prints the integer representation
- Implement a number guessing game
Thanks for reading! Let me know what you think of this post on Twitter.