Numbers and arithmetic

Numbers and arithmetic

This section covers the core concepts of Python, including language fundamentals, built-in data types, and variables. Understanding these basics is crucial for anyone looking to build a solid foundation in Python programming.

6 audio · 1:32

Nortren·

How does integer division work in Python 3?

0:17
Python 3 uses two division operators. The single slash performs true division and always returns a float. The double slash performs floor division and returns the floor of the result, with the type matching the operands. So seven divided by two equals three point five, while seven double-slash two equals three.

What is the difference between int and float?

0:15
An int is an integer of arbitrary precision, limited only by available memory. A float is a double-precision floating point number following the IEEE 754 standard, the same as a C double. Floats have limited precision and can produce small rounding errors in arithmetic.

Why does 0.1 plus 0.2 not equal 0.3 in Python?

0:18
This is a property of binary floating point arithmetic, not specific to Python. The decimal numbers 0.1 and 0.2 cannot be represented exactly in binary, so their sum has a tiny rounding error. For exact decimal arithmetic, use the Decimal class from the decimal module or Fraction from the fractions module.

What is the Decimal type in Python?

0:12
Decimal is a class in the decimal module that provides arbitrary-precision decimal arithmetic. It avoids the rounding errors of binary float and is essential for financial and monetary calculations where exact decimal representation matters.

How does Python handle very large integers?

0:15
Python integers have arbitrary precision and can grow to any size limited only by available memory. There is no overflow as in fixed-width integer types. The interpreter automatically uses an optimized representation for small integers and switches to a larger one as needed.

What is the difference between true division and floor division for negative numbers?

0:15
Floor division rounds toward negative infinity, not toward zero. So negative seven divided by two using floor division equals negative four, not negative three. This is consistent with Python's modulo operation, which always returns a result with the same sign as the divisor. ---