Lesson 1 of 81985–today7 min
Values and types
On 25 February 1991 a Patriot battery at Dhahran, Saudi Arabia, failed to intercept an incoming Scud missile, which hit a US Army barracks and killed 28 soldiers. The battery had been running for about 100 hours.
The US General Accounting Office traced the failure to arithmetic. The system counted time in tenths of a second and converted it to a real number held in a 24-bit register. One tenth has no exact binary representation, so every conversion lost a sliver. After 100 hours the clock was off by about 0.34 seconds, enough to look for the Scud in the wrong part of the sky. A type, and what it can and cannot represent, is not a detail.
A value has a type
A type is a set of values plus the operations allowed on them. The integer 7, the float 7.0, the string "7" and the boolean True look related to a human, but to the machine they are stored differently and behave differently: 7 + 1 is 8, "7" + "1" is "71", and "7" + 1 is an error. Python is dynamically typed: names carry no type, values do, and the check happens while the program runs. It is also strongly typed: it refuses to mix a string and an integer silently. C and Java are statically typed: you declare int count; and the compiler rejects a wrong use before the program ever runs.
print(type(7), type(7.0), type("7"), type(True))
# <class 'int'> <class 'float'> <class 'str'> <class 'bool'>
print(7 / 2, 7 // 2, 7 % 2) # 3.5 3 1
print(-7 // 2, -7 % 2) # -4 1: floor division rounds down
print(2 ** 100) # Python ints never overflow
print(0.1 + 0.2 == 0.3) # False
import math
print(math.isclose(0.1 + 0.2, 0.3)) # True: compare with a tolerance
print(int("42") + 1, str(42) + "1") # 43 421: explicit conversionsUnderneath, everything is bits
Integers are stored in binary. In a fixed-width type such as C's 32-bit int, negative numbers use two's complement: to get −5 in 8 bits, write 5 as 00000101, invert every bit (11111010) and add one (11111011). The range of an n-bit signed integer is −2ⁿ⁻¹ to 2ⁿ⁻¹ − 1, so a 32-bit int stops at 2,147,483,647 and the next addition overflows. Python's int grows as needed and never overflows, at the cost of speed.
0 to 65,535
Bit weights: … 128 · 64 · 32 · 16 · 8 · 4 · 2 · 1
−202 in 8-bit two's complement: only for 1 to 128
Python's float is an IEEE 754 binary64 number: a sign bit, 11 exponent bits and 52 fraction bits, good for about 15 to 17 significant decimal digits. Just as 1/3 has no finite decimal expansion, 1/10 has no finite binary one, so 0.1 is stored as the nearest representable value and small errors accumulate. Never compare floats with ==; use a tolerance, or use integers (cents, not euros) or the decimal module when exact decimal arithmetic matters.
Worked exercise
Integer and float division in Python
Give the value and the type of each expression: (a) 17 // 5, (b) 17 % 5, (c) -17 // 5, (d) -17 % 5, (e) 17 / 1.
Try it first, then reveal the solution one step at a time.
Key terms
- Type
- A set of values and the operations allowed on them.
- Dynamic typing
- Types are checked while the program runs; names can refer to values of any type.
- Strong typing
- The language refuses to mix incompatible types without an explicit conversion.
- Two's complement
- The standard binary encoding of signed integers: invert the bits and add one to negate.
- Floating point
- A number stored as sign, exponent and fraction, with limited precision (IEEE 754).
Check what you learned
1.What does
-7 // 2evaluate to in Python?2.Why does
0.1 + 0.2 == 0.3return False in Python?3.Which 8-bit two's complement pattern represents −1?
Practise it for the exam
- Tech+ · Software Development ConceptsQuestions on this topic
- Tech+ · IT Concepts and TerminologyQuestions on this topic
Sources
- Built-in Types — Python documentation
- Floating-Point Arithmetic: Issues and Limitations — Python documentation
- IEEE 754-2019: Standard for Floating-Point Arithmetic — IEEE Standards Association
- Patriot Missile Defense: Software Problem Led to System Failure at Dhahran, Saudi Arabia (IMTEC-92-26) — U.S. General Accounting Office