Python Numbers: A Comprehensive Guide with Examples

Article by: Manish Methani

Last Updated: October 7, 2021 at 8:04am IST
5 min 9 sec read

  1. Numeric Data Types in Python Python has three main numeric data types:
  • Integers (int): Whole numbers, positive or negative, without a decimal point.
  • Floating-point numbers (float): Numbers with a decimal point or an exponent notation.
  • Complex numbers (complex): Numbers with a real and imaginary part.

Here are some examples:

# Integers
x = 5
y = -10
z = 0

# Floating-point numbers
a = 3.14
b = 1.23e-5

# Complex numbers
c = 3 + 4j
d = complex(1, -2)

      2. Basic Arithmetic Operations

Python supports all basic arithmetic operations for numerical data types. Here are some examples:

x = 5
y = 3

# Addition
z = x + y  # Output: 8

# Subtraction
z = x - y  # Output: 2

# Multiplication
z = x * y  # Output: 15

# Division
z = x / y  # Output: 1.6666666666666667

# Floor Division
z = x // y  # Output: 1

# Modulo
z = x % y  # Output: 2

# Exponentiation
z = x ** y  # Output: 125

       3. Type Conversion

Python allows you to convert numerical data types from one to another. Here's an example:

x = 5
y = 3.14

# Convert x to a floating-point number and add it to y
z = float(x) + y

print(z)  # Output: 8.14

In this example, we convert the integer "x" to a floating-point number using the float() function and add it to the floating-point number "y".

       4. Built-in Functions Python has several built-in functions for working with numbers. Here are some examples:

# Absolute value
x = abs(-10)  # Output: 10

# Round to a certain number of decimal places
y = round(3.14159, 2)  # Output: 3.14

# Maximum and minimum values
z = max(1, 2, 3)  # Output: 3
a = min(4, 5, 6)  # Output: 4

      5. Math Module

Python also has a math module that provides additional mathematical functions. Here's an example:

import math

# Square root
x = math.sqrt(25)  # Output: 5.0

# Trigonometric functions
y = math.sin(0)  # Output: 0.0
z = math.cos(math.pi)  # Output: -1.0

      6. Floats

Floats are used to represent real numbers with decimal points. They are also known as floating-point numbers. In Python, float is represented by float class. We can create a float variable by assigning a value with a decimal point to it.

Here's an example:

# Example of float
x = 3.14
y = 2.5
print(x)
print(y)

Output:

3.14
2.5

We can also use mathematical operations with floats:

# Example of float with mathematical operations
x = 3.14
y = 2.5
z = x + y
print(z)

Output:

5.64

Complex Numbers

Complex numbers are used to represent numbers with a real and imaginary part. In Python, complex numbers are represented by complex class. We can create a complex number by specifying the real and imaginary parts separated by a + or - sign.

Here's an example:

# Example of complex number
x = 2 + 3j
y = 4 - 2j
print(x)
print(y)

Output:

(2+3j)
(4-2j)

We can also use mathematical operations with complex numbers:

# Example of complex number with mathematical operations
x = 2 + 3j
y = 4 - 2j
z = x + y
print(z)

Output:

(6+1j)

Type Conversion

We can convert one type of number into another using type conversion functions. Here are some type conversion functions in Python:

  • int(x) converts x to an integer.
  • float(x) converts x to a floating-point number.
  • complex(x) converts x to a complex number with a zero imaginary part.
  • complex(x, y) converts x and y to a complex number with real part x and imaginary part y.

Here are some examples:

# Example of type conversion
x = 10
y = 3.14
z = 2 + 3j

# convert x to float
a = float(x)

# convert y to int
b = int(y)

# convert z to float
c = float(z)

print(a)
print(b)
print(c)

Output:

10.0
3
(2+3j)

We have covered the basics of Python Numbers in this tutorial.

Watch Video Tutorials at Codzify YouTube Channel:

Codzify Logo

Terms and Conditions    Cookie Policy   Refund Policy   Adsense Disclaimer

Contact: teamcodzify@gmail.com