← Practical Python
01 / 10 · 23 MIN

Data, types, and decisions

Understand your values before turning them into decisions.

Meaningful values

A Python name refers to an object. The object has a type: an integer represents a count, a string represents text, and a boolean represents a condition. Reading "20" from a file is different from receiving the integer 20. Convert and validate data at the program boundary.

Compare before acting

Use == to compare values and is to compare identity. An empty list is false in a boolean context, but the string "False" is true because it is nonempty. When validating input, distinguish absence, zero, and empty text according to the business rule.

Guided workplace application

An automation receives counts and options as text. Define what is allowed first: count may be zero but not None or negative; enabled accepts only true and false after trimming surrounding whitespace and normalizing case. Convert the count and handle failures without silently replacing them with zero. For the option use an explicit mapping: bool("false") is true because it is nonempty text. In support, a configuration error should identify the key and failure category without exposing credentials. Do not use eval to interpret a simple option. Try valid, empty, missing, and out-of-bound inputs and decide the expected result before running the batch.

Integers and booleans at the boundary

If a contract accepts only native integers, type(value) is int excludes booleans and subclasses. isinstance(value, int) accepts True because bool derives from int. Choose the rule from the contract before conversion; do not infer types from truthiness. After type validation, apply bounds and meaning. Zero may be a valid quantity even when False must be rejected.

Validate the unit before conversion

For a protocol limit measured in bytes, measure the encoded bytes that will be sent; len(str) counts code points, not UTF-8 size. Precomposed ação has four code points and six UTF-8 bytes. Likewise, int(3.9) does not establish that input was integral: it discards the fraction toward zero. Define acceptance before conversion to avoid silently transforming invalid input into a seemingly valid quantity.

raw_count = "20"
count = int(raw_count)
if count > 10:
 print("Review required")
IN PRACTICE

A script receives the pending-operation count as text. Check whether the value can be converted and is nonnegative before raising an alert. Conversion failure needs an explicit response.

Common pitfalls

Confusing text false with False; rejecting zero when only None is absence.

Related topics: Collections without surprises · Small functions, clear contracts

Take this idea with you

Validate and convert inputs at the boundary; compare values with == and identity with is.

Create account

Reference: Python 3.14: Built-in types · Python 3.14; DR Python 2026.3