Numbers in Python
Numbers represent quantity. In Python, everything is an object including numbers. Python supports integers, floating-point numbers and complex numbers.
Integer
An integer is a whole number with no decimal places. The name for the integer data type is int. Python 2 has two integer types — int and long. There is no ‘long integer’ in Python 3 anymore.
Floating-Point Number
A floating-point number, or float for short, is a number with a decimal place. They represent real numbers and are written with a decimal point dividing the integer and the fractional parts. The name of the floating-point data type is float.
Examples(Arithmetic)
Variable Assignment
We use equal sign to assign labels to variables.
Rules for Variable Name
- Names cannot start with a number.
- There can be no spaces in the name, use _ instead.
- Can’t use any of these symbols :’”,<>/?|\()!@#$%^&*~-+
- It’s considered best practice (PEP8) that names are lowercase.
- Avoid using the characters ‘l’ (lowercase letter el), ‘O’ (uppercase letter oh), or ‘I’ (uppercase letter eye) as single character variable names.
- Avoid using words that have special meaning in Python like “list” and “str”.
Numbers for Embedded Programmers
The numbers we deal with every day are of the decimal (base 10) number system. The embedded programmers need to work with binary (base 2), hexadecimal (base 16) and octal (base 8) number systems. In Python, these numbers are represented by placing a prefix before that number.
Python Built-in Library
The Python has built-in library to operate with numbers. The numbers module defines an abstract hierarchy of numeric types. The math and cmath modules contain various mathematical functions for floating-point and complex numbers. The decimal module supports exact representations of decimal numbers, using arbitrary precision arithmetic. For further details go to this link.
Practice Examples
Go to this link, where you will find examples on how to operate with numbers.