Python Numbers

Python has three types of numbers.

  1. int
  2. float
  3. complex

Int

An integer is the number zero, a positive natural number or a negative integer with a minus sign.

num = 10

print(num)
print(type(num))

Float

Float is a positive or negative number, containing one or more decimals.

num = 12.5

print(num)
print(type(num))

Complex

Complex numbers are written with a "j" as the imaginary part

num = 12.5+2j

print(num)
print(type(num))

converting number types

we can convert from one type to another with the int(), float(), and complex() built-in functions.

  • convert int to float
num_int = 10
num_float = float(num_int)
print(num_float)
  • convert float to int
num_float = 25.4
num_int = float(num_float)
print(num_int)
  • convert int to complex
num_int = 25
num_comp = complex(num_int)
print(num_comp)

find sum of two numbers

Let's write a program to find the sum of numbers

num1 = 10
num2 = 20

print(num1 + num2)