Skip to main content

Introduction to Python Numbers

Python Numbers​

Numbers are a fundamental data type used to represent numeric values.

Python provides several types for working with numbers.

Here are the commonly used number types in Python:

Integers (int)​

Integers represent whole numbers without any fractional or decimal part.

They can be positive or negative.

x = 5
y = -10

Floating-Point Numbers (float)​

Floating-point numbers represent real numbers with a fractional part.

They are used to represent numbers with decimal values.

pi = 3.14
temperature = -2.5

Complex Numbers (complex)​

Complex numbers represent numbers with both a real part and an imaginary part.

They are expressed in the form a + bj, where a is the real part, b is the imaginary part, and j represents the imaginary unit (√-1).

z = 2 + 3j
w = -1j

Python provides arithmetic operations such as addition, subtraction, multiplication, and division that can be performed on numeric types. These operations can be applied to both integers and floating-point numbers.

Here are a few examples:

x = 10
y = 3

sum = x + y # Addition
difference = x - y # Subtraction
product = x * y # Multiplication
quotient = x / y # Division

print(sum) # Output: 13
print(difference) # Output: 7
print(product) # Output: 30
print(quotient) # Output: 3.3333333333333335

Python also provides additional operations like modulo (%) for finding the remainder and exponentiation (**) for raising a number to a power.

x = 10
y = 3

remainder = x % y # Modulo operation (finding the remainder)
power = x ** y # Exponentiation (x raised to the power of y)

print(remainder) # Output: 1
print(power) # Output: 1000

These are the basic number types in Python and some operations that can be performed on them.

Python's numeric types provide a wide range of functionality and flexibility for working with numbers in various contexts.

Type Conversion​

Type conversion, also known as type casting, is the process of converting one data type to another in Python.

Python provides built-in functions that allow you to convert variables from one type to another.

Here are some commonly used type conversion functions:

  • int(): Converts a value to an integer type.
x = 5.7
x_int = int(x)
print(x_int) # Output: 5
  • float(): Converts a value to a floating-point number type.
x = 5
x_float = float(x)
print(x_float) # Output: 5.0
  • str(): Converts a value to a string type.
x = 10
x_str = str(x)
print(x_str) # Output: "10"
  • bool(): Converts a value to a boolean type.
x = 0
x_bool = bool(x)
print(x_bool) # Output: False
  • list(), tuple(), set(), dict(): These functions are used to convert values to list, tuple, set, and dictionary types, respectively.
x = "hello"
x_list = list(x)
x_tuple = tuple(x)
x_set = set(x)
x_dict = dict(x)
print(x_list) # Output: ['h', 'e', 'l', 'l', 'o']
print(x_tuple) # Output: ('h', 'e', 'l', 'l', 'o')
print(x_set) # Output: {'e', 'l', 'o', 'h'}
print(x_dict) # Output: {'h': 'e', 'l': 'o'}