Python Logo

Numbers


Python numbers are used to represent numerical values, such as integers, floats, and complex numbers. Python numbers can be used in arithmetic expressions, logical expressions, and conditional statements.

Integers

Integers are whole numbers, both positive and negative. For example, 5, -10, and 1234567890 are all integers.

Here are some examples of how to use integers in Python:

 

Python

# Add two integers
sum = 5 + 10
print(sum)

# Subtract two integers
difference = 10 - 5
print(difference)

# Multiply two integers
product = 5 * 10
print(product)

# Divide two integers
quotient = 10 / 5
print(quotient)

# Modulus operator returns the remainder of dividing two integers
remainder = 10 % 5
print(remainder)
 

Output:

 

15
5
50
2.0
0
 

Floats

Floats are real numbers with a decimal point. For example, 3.14, -20.5, and 1.234567890e9 are all floats.

Here are some examples of how to use floats in Python:

 

Python

# Add two floats
sum = 3.14 + 20.5
print(sum)

# Subtract two floats
difference = 20.5 - 3.14
print(difference)

# Multiply two floats
product = 3.14 * 20.5
print(product)

# Divide two floats
quotient = 20.5 / 3.14
print(quotient)
 

Output:

 

23.64
17.36
64.87
6.53
 

Complex numbers

Complex numbers are numbers of the form a + bi, where i is the imaginary unit and a and b are real numbers.

Here are some examples of how to use complex numbers in Python:

 

Python

# Create a complex number
my_complex_number = 3 + 5j

# Get the real and imaginary parts of a complex number
real_part = my_complex_number.real
imaginary_part = my_complex_number.imag

# Add two complex numbers
sum = my_complex_number + complex(2, 3)

# Subtract two complex numbers
difference = my_complex_number - complex(2, 3)

# Multiply two complex numbers
product = my_complex_number * complex(2, 3)

# Divide two complex numbers
quotient = my_complex_number / complex(2, 3)

# Print the complex number
print(my_complex_number)
 

Output:

 

(3+5j)
 

Python numbers are a powerful tool for working with numerical data. By understanding how to use Python numbers, you can write more efficient and readable Python code.