2.1 Numbers (Integers, Floats)

2.1 Numbers (Integers, Floats)

A deep dive into Python's fundamental numeric types, integers (int) and floating-point numbers (float), and their basic properties.

Numbers are the cornerstone of any computational process. In Python, the two most common and basic numeric types are integers and floating-point numbers, which represent decimal numbers. Let's explore them in detail.

Integers (int)

Integers are whole numbers, either positive or negative, without any decimal part. One of Python's most powerful features is that integers can be arbitrarily large, limited only by your computer's available memory. This is a significant difference from other programming languages that have fixed limits for integers.
For readability, Python allows using underscores (_) to separate digits in large numbers without affecting their value. For example, `1_000_000` is the same as `1000000`.

Floating-point numbers (float)

Floating-point numbers, or simply floats, are numbers that contain a decimal part, denoted by a period (.). They are used to represent real numbers and scientific measurements. They can also be expressed in scientific notation, using the letter `e` or `E` to denote the power of 10.
# Examples of Integers and Floats

# Integer numbers
positive_integer = 42
negative_integer = -10
zero = 0
large_integer = 1_000_000_000

# Floating-point numbers
positive_float = 3.14159
negative_float = -0.001
float_with_zero_decimal = 5.0
scientific_notation = 6.022e23

# type(x).__name__ returns the type name as a string
print(f"Value: {positive_integer}, Type: {type(positive_integer).__name__}")
print(f"Value: {large_integer}, Type: {type(large_integer).__name__}")
print(f"Value: {positive_float}, Type: {type(positive_float).__name__}")
print(f"Value: {float_with_zero_decimal}, Type: {type(float_with_zero_decimal).__name__}")
print(f"Value: {scientific_notation}, Type: {type(scientific_notation).__name__}")

Explore More with AI

Use AI to generate new examples, delve deeper into theory, or get your questions answered.