Python Numbers: int, float, complex (With Examples) (2024)

Python supports three numeric types to represent numbers: integers, float, and complex number. Here you will learn about each number type.

Int

In Python, integers are zero, positive or negative whole numbers without a fractional part and having unlimited precision, e.g. 0, 100, -10. The followings are valid integer literals in Python.

Example: Python Numbers

#integer variablesx = 0print(x)x = 100print(x)x = -10print(x)x = 1234567890print(x)x = 5000000000000000000000000000000000000000000000000000000print(x)

Try it

Integers can be binary, octal, and hexadecimal values.

Example: Python Numbers

b = 0b11011000 # binaryprint(b)o = 0o12 # octalprint(o)h = 0x12 # hexadecimalprint(h)

Try it

All integer literals or variables are objects of the int class. Use the type() method to get the class name, as shown below.

Example: Python Numbers

print(type(100))x=1234567890print(type(x))y=5000000000000000000000000000000000000000000000000000000print(type(y))

Try it

Note: Leading zeros in non-zero integers are not allowed in Python, e.g. 000123 is invalid number and 0000 becomes 0.

x=001234567890 #syntaxError: invalid token

Python does not allow comma as number delimiter. Use underscore _ as a delimiter instead.

Note that integers must be without a fractional part (decimal point). It it includes a fractional then it becomes a float.

Example: Python Numbers

x=5print(type(x)) #output: <class 'int'>x=5.0print(type(x)) #output: <class 'float'>

Try it

The int() function converts a string or float to int.

Example: int() Function

x = int('100')print(x) #output: 100y = int('-10')print(y) #output: -10z = int(5.5)print(z) #output: 5n = int('100', 2)print(n) #output: 4

Try it

Binary

A number having 0b with eight digits in the combination of 0 and 1 represent the binary numbers in Python. For example, 0b11011000 is a binary number equivalent to integer 216.

Example: Binary Numbers

x = 0b11011000print(x)x = 0b_1101_1000print(x)print(type(x))

Try it

Octal

A number having 0o or 0O as prefix represents an octal number. For example, 0O12 is equivalent to integer 10.

Example: Octal Numbers

x = 0o12print(x)print(type(x))

Try it

Hexadecimal

A number with 0x or 0X as prefix represents hexadecimal number. For example, 0x12 is equivalent to integer 18.

Example: Hexadecimal Numbers

x = 0x12print(x)print(type(x))

Try it

Float

In Python, floating point numbers (float) are positive and negative real numbers with a fractional part denoted by the decimal symbol . or the scientific notation E or e, e.g. 1234.56, 3.142, -1.55, 0.23.

Example: Float Numbers

f = 1.2print(f) #output: 1.2print(type(f)) #output: <class 'float'>f=123_42.222_013 #output: 12342.222013print(f)f=2e400print(f) #output: inf

Try it

As you can see, a floating point number can be separated by the underscore _. The maximum size of a float is depend on your system. The float beyond its maximum size referred as inf, Inf, INFINITY, or infinity. For example, a float number 2e400 will be considered as infinity for most systems.

Scientific notation is used as a short representation to express floats having many digits. For example: 345.56789 is represented as 3.4556789e2 or 3.4556789E2

Example: Scienticific Numbers

f = 1e3print(f) #output: 1000.0f = 1e5print(f) #output:100000.0f = 3.4556789e2print(f) #output:print(type(f)) #output:345.56789

Try it

Use the float() function to convert string, int to float.

Example: float() Function

f=float('5.5')print(f) #output: 5.5f=float('5')print(f) #output: 5.0f=float(' -5')print(f) #output: -5.0f=float('1e3')print(f) #output: 1000.0f=float('-Infinity')print(f) #output: -inff=float('inf')print(f) #output: infprint(type(f)) #output:<class 'float'>

Try it

Complex Numbers

A complex number is a number with real and imaginary components. For example, 5 + 6j is a complex number where 5 is the real component and 6 multiplied by j is an imaginary component.

Example: Complex Numbers

a = 5+2jprint(a)print(type(a))

Try it

You must use j or J as imaginary component. Using other character will throw syntax error.

Example: Invalid Complex Numbers

a=5+2ka=5+ja=5i+2j

Try it

Arithmetic Operators

The following table list arithmetic operators on integer values:

Operator Description Example
+ (Addition) Adds operands on either side of the operator. a,b = 10, 20
print(a+b) #30
- (Subtraction) Subtracts the right-hand operand from the left-hand operand. a,b = 10, 20
print(a-b) #-10
* (Multiplication) Multiplies values on either side of the operator. a,b = 10, 20
print(a*b) #200
/ (Division) Divides the left-hand operand by the right-hand operand. a,b = 10, 20
print(b/a) #2.0
% (Modulus) Returns the remainder of the division of the left-hand operand by right-hand operand. a,b = 10, 22
print(a+b) #2
** (Exponent) Calculates the value of the left-operand raised to the right-operand. a=3
print(a**2) #9
print(a**3) #27
// (Floor Division) The division of operands where the result is the quotient in which the digits after the decimal point are removed. But if one of the operands is negative, the result is floored, i.e., rounded away from zero (towards negative infinity): a,b = 9, 2
print(a//b) #4

Try it

Arithmetic Operations on Complex Numbers

Addition and subtraction of complex numbers is straightforward. Real and imaginary parts are added/subtracted to get the result.

Example: Arithmetic Operation on Complex Numbers

a=6+4jb=3+2jprint("a+2=",a+2)print("a*2=",a*2)print("a/2=",a/2)print("a**2=",a**2)print("a+b=",a+b)print("a-b=",a-b)

Try it

As you can see in the above example, the arithmetic operators can also be used with two complex numbers.

The process of multiplying these two complex numbers is very similar to multiplying two binomials. Multiply each term in the first number by each term in the second number.

Example: Multiply Complex Numbers

a=6+4j b=3+2j c=a*b print(c)c=(6+4j)*(3+2j) print(c)c=(18+12j+14j+8*1)print(c)

Try it

Numeric Functions

A numeric object of one type can be converted in another type using the following functions:

Built-in Function Description
int Returns the integer object from a float or a string containing digits.
float Returns a floating-point number object from a number or string containing digits with decimal point or scientific notation.
complex Returns a complex number with real and imaginary components.
hex Converts a decimal integer into a hexadecimal number with 0x prefix.
oct Converts a decimal integer in an octal representation with 0o prefix.
pow Returns the power of the specified numbers.
abs Returns the absolute value of a number without considering its sign.
round Returns the rounded number.
Python Numbers: int, float, complex (With Examples) (2024)

References

Top Articles
Latest Posts
Article information

Author: Laurine Ryan

Last Updated:

Views: 6063

Rating: 4.7 / 5 (57 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Laurine Ryan

Birthday: 1994-12-23

Address: Suite 751 871 Lissette Throughway, West Kittie, NH 41603

Phone: +2366831109631

Job: Sales Producer

Hobby: Creative writing, Motor sports, Do it yourself, Skateboarding, Coffee roasting, Calligraphy, Stand-up comedy

Introduction: My name is Laurine Ryan, I am a adorable, fair, graceful, spotless, gorgeous, homely, cooperative person who loves writing and wants to share my knowledge and understanding with you.