An Introduction to the Art of Computer Programming Using Python in the Age of Generative AI

II. Numbers and Arithmetic

Understanding how to work with numbers and perform arithmetic operations is fundamental to programming. Python provides a variety of numeric types and a rich set of operators for working with them. By mastering these core concepts, you'll be able to handle everything from simple calculations to more advanced mathematical tasks.

Integers and Floats

Python natively supports integers (whole numbers, positive or negative) and floating-point numbers (numbers with a decimal point). Integer values in Python can be arbitrarily large, limited only by your system's memory, while floats typically follow the IEEE 754 double-precision standard. You can use basic arithmetic operators like +, -, *, and / to perform operations on them.


# Integer arithmetic
result = 5 + 7
print(result)

# Float arithmetic
result = 10.5 - 3.2
print(result)
        

Python automatically promotes integers to floats if an operation requires floating-point arithmetic. You can also convert between types explicitly:


# Converting from float to int (truncates decimals)
x = 3.9
print(int(x))

# Converting from int to float
y = 4
print(float(y))
        

Division and Floor Division

In Python 3, using the / operator always results in a floating-point number (even if both operands are integers). If you want to round the result down to the nearest integer, use the floor division operator //. This is particularly useful when you need integer-based results, such as calculating how many times a value fits into a range.


# Regular division
result = 8 / 3
print(result)

# Floor division
result = 8 // 3
print(result)
        

Note that floor division // behaves differently with negative numbers because it rounds toward negative infinity. For instance, -8 // 3 yields -3, not -2.

Modulus and Exponents

Two other operators are the % (modulus) and ** (exponent) operators. The modulus operator gives the remainder of dividing one integer by another, while the exponent operator raises a number to a given power.


# Modulus
remainder = 8 % 3
print(remainder)

# Exponent
result = 2 ** 3
print(result)
        

These operators also work with floats, enabling you to handle decimal results. However, when mixing floats and integers, be mindful of floating-point precision issues in certain cases.

Complex Numbers

Python supports complex numbers natively through the letter j (or J) to denote the imaginary part. A complex number takes the form a + bj, where a is the real part and b is the imaginary part. You can perform arithmetic with them just like real numbers.


# Working with complex numbers
z = 1 + 2j
print(z.real)  # Real part
print(z.imag)  # Imaginary part

z2 = 2 + 3j
sum_complex = z + z2
print(sum_complex)
        

Additional Numeric Tools

For more advanced numerical computations, Python offers built-in libraries and modules:

As you progress, these tools can become vital for everything from simple data analysis to high-precision calculations and specialized domains like finance and scientific research.


import math

# Examples using the math module
print(math.sqrt(16))  # prints 4.0
print(math.ceil(3.2)) # prints 4
        
Back to Home