Python Numbers

Python Numbers

There are three numeric types in Python:

Variables of numeric types are created when you assign a value to them:

Numeric Types

x = 1    # int
y = 2.8  # float
z = 1j   # complex

print(type(x)) print(type(y)) print(type(z))


1. Int

int, or integer, is a whole number, positive or negative, without decimals, of unlimited length.

x = 1
y = 35656222554887711
z = -3255522

2. Float

float, or "floating point number" is a number, positive or negative, containing one or more decimals. Floats can also be scientific numbers with an "e" to indicate the power of 10.

x = 1.10
y = -87.7e100

3. Complex

Complex numbers are written with a "j" as the imaginary part. They are mostly used in advanced mathematical computing.

x = 3+5j
y = 5j

Type Conversion

You can convert from one type to another with the int(), float(), and complex() methods. Note: You cannot convert complex numbers into another number type.


Exercise

?

Which letter is used to represent the imaginary part of a complex number in Python?