Python Numbers
Python numeric types store allow you to play with numebers, numbers in python are immutable, which means changing the value of a number makes a new numeric object.
Number objects
Numbers in most programming languages are the simplest data to work with (well technically boolean is simpler, but lets just keep going :), so you can create a number by just an assignment, bingoooooo For example
coder = 5 coderme = 7 com = 3
You can also delete a number variable like you delete anything in Python, by using del statement.
# this will delete com variable from earlier del com
Python numeric types
Python has support to three different types of numbers.
- int (signed integers) − They are often called just integers or ints. They are positive or negative whole numbers with no decimal point. Unlike most programming languages, Integers in Python are of unlimited size.
- float (floating point real values) − Also called floats, they represent real numbers and are written with a decimal point dividing the integer and the fractional parts. Floats may also be in scientific notation, with E or e indicating the power of 10 (2.5e2 = 2.5 x 102 = 250).
- complex (complex numbers) − are of the form x + yJ, where x and y are floats and J (or j) represents the square root of -1 (which is an imaginary number). The real part of the number is x, and the imaginary part is y. Complex numbers are used in maths )
Number Type Conversion
Python converts numbers internally in an expression containing mixed types to a common type for evaluation. Sometimes, you need to coerce a number explicitly from one type to another to satisfy the requirements of an operator or function parameter.
- Convert float to int using int(num)
x = 1.1 y = int(x)
- Convert int to float using float(num):
y = 1 x = float(y)
- Convert int to complex by using complex(num):
y = 1 x = complex(y)
- Convert float to complex by using complex(num):
y = 1.1 x = complex(y)