# Python Numeric Types

Python has several built-in numeric types for representing integers, floating point numbers, and complex numbers. Here are the most commonly used numeric types:

## Integers

Integers are whole numbers that can be positive, negative, or zero. In Python, integers are represented using the int type. Here are some examples of integer literals:

x = 10 # a positive integer y = -20 # a negative integer z = 0 # zero

You can perform arithmetic operations on integers just like you would in mathematics. For example:

# Addition a = 10 + 20 print(a) # 30 # Subtraction b = 20 - 10 print(b) # 10 # Multiplication c = 10 * 20 print(c) # 200 # Division d = 20 / 10 print(d) # 2.0 # Modulus (remainder) e = 21 % 10 print(e) # 1 # Exponentiation f = 2 ** 3 print(f) # 8

## Floating Point Numbers

Floating point numbers, also known as "floats," are numbers with a decimal point. In Python, floats are represented using the float type. Here are some examples of floating point literals:

x = 10.5 # a positive float y = -20.75 # a negative float z = 0.0 # zero You can perform arithmetic operations on floats just like you would with integers. For example: ```python # Addition a = 10.5 + 20.75 print(a) # 31.25 # Subtraction b = 20.75 - 10.5 print(b) # 10.25 # Multiplication c = 10.5 * 20.75 print(c) # 218.875 # Division d = 20.75 / 10.5 print(d) # 1.9818181818181818 # Exponentiation e = 2.5 ** 3 print(e) # 15.625

## Complex Numbers

Complex numbers are numbers with both a real and an imaginary part. In Python, complex numbers are represented using the complex type. The real part is followed by the imaginary part, which is denoted by the letter j or J. Here are some examples of complex literals:

x = 10 + 5j # a positive complex number y = -20 - 10j # a negative complex number z = 0 + 0j # zero You can perform arithmetic operations on complex numbers just like you would with real numbers. For example: ```python # Addition a = (10 + 5j) + (20 + 10j) print(a) # (30+15j) # Subtraction b = (20 + 10j) - (10 + 5j) print(b) # (10+5j) # Multiplication c = (10 + 5j) * (20 + 10j) print(c) # (150+200j) Type Conversion You can convert values from one numeric type to another using the built-in functions int(), float(), and complex(). For example: ```python # Convert an integer to a float x = 10 y = float(x) print(y) # 10.0 # Convert a float to an integer x = 10.5 y = int(x) print(y) # 10 # Convert an integer to a complex number x = 10 y = complex(x) print(y) # (10+0j) # Convert a float to a complex number x = 10.5 y = complex(x) print(y) # (10.5+0j) ## Binary, Octal, and Hexadecimal Notation In addition to the standard decimal notation, you can also represent integers using binary, octal, or hexadecimal notation. To do this, you can use the bin(), oct(), and hex() functions, respectively. ```python # Binary notation x = 0b1010 # 10 in binary print(x) # 10 # Octal notation x = 0o12 # 10 in octal print(x) # 10 # Hexadecimal notation x = 0xA # 10 in hexadecimal print(x) # 10

You can also use these functions to convert integers to their binary, octal, or hexadecimal representation.

# Convert an integer to binary x = 10 y = bin(x) print(y) # 0b1010 # Convert an integer to octal x = 10 y = oct(x) print(y) # 0o12 # Convert an integer to hexadecimal x = 10 y = hex(x) print(y) # 0xA

## Special Values

Python has several special values for representing infinity and not-a-number (NaN). These values are float('inf') for infinity and float('nan') for NaN. You can use these values as you would any other float.

# Positive infinity x = float('inf') print(x) # inf # Negative infinity x = -float('inf') print(x) # -inf # Not-a-number x = float('nan') print(x) # nan