None and Binary Data
Real programs handle missing values and binary data. Use None to mark "no value", and bytes/bytearray for raw binary content. Know when each is appropriate and how to convert safely between text and bytes.
None for "No Value"
None is a single object meaning "nothing here".
1234567result = None email = None print(result is None) # True print(email is None) # True if result is None: print("No result yet")
Use is None instead of truthiness checks, since 0 and "" are also falsey.
123value = 0 print(not value) # True print(value is None) # False
Defaults and Fallbacks
None is used as a clear marker that a value is intentionally missing. It lets you distinguish between "no value provided" and valid values like 0 or "", making defaults and fallbacks safer and more predictable.
1234567x = None print(x or "unknown") # 'unknown' print("unknown" if x is None else x) x = 0 print(x or "unknown") # 'unknown' (maybe wrong) print("unknown" if x is None else x) # 0
Functions and Parameters
This example shows how a function uses a parameter set to None as a signal that no tag was provided. It allows the function to assign a safe default while still letting the caller override it when needed.
1234567def add_tag(text, tag=None): if tag is None: tag = "general" return f"[{tag}] {text}" print(add_tag("hello")) # [general] hello print(add_tag("hello", "news")) # [news] hello
bytes and bytearray for Binary Data
str holds text, bytes and bytearray hold raw byte values.
1234b1 = b"hello" b2 = bytes([72, 105]) buf = bytearray(b"abc") buf[0] = 65
Encoding and Decoding
Encoding converts text into bytes so it can be stored or transferred reliably, while decoding restores those bytes back into readable text. Using a defined encoding like UTF-8 ensures characters are preserved correctly.
123text = "cafΓ©" data = text.encode("utf-8") back = data.decode("utf-8")
Mixing Text and Bytes
Text (str) and bytes (bytes) can't be combined directly because they represent different data types. To mix them safely, you must convert the text into bytes first using an explicit encoding.
123456try: b"ID:" + "123" except TypeError as e: print(e) ok = b"ID:" + "123".encode("utf-8")
Length Differences
Some characters take one text element but multiple bytes, so their length in str and in encoded form can differ. This happens because encodings like UTF-8 may use more than one byte to represent a single character.
123ch = "Γ©" len(ch) # 1 len(ch.encode()) # 2
Files
Binary files must be opened in "rb" mode so their raw bytes are read exactly as stored. This prevents Python from trying to interpret the data as text.
# with open("example.png", "rb") as f:
# blob = f.read()
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Can you explain more about when to use None versus other default values?
How do I safely convert between text and bytes in Python?
What are some common mistakes when handling binary data and missing values?
Awesome!
Completion rate improved to 3.45
None and Binary Data
Swipe to show menu
Real programs handle missing values and binary data. Use None to mark "no value", and bytes/bytearray for raw binary content. Know when each is appropriate and how to convert safely between text and bytes.
None for "No Value"
None is a single object meaning "nothing here".
1234567result = None email = None print(result is None) # True print(email is None) # True if result is None: print("No result yet")
Use is None instead of truthiness checks, since 0 and "" are also falsey.
123value = 0 print(not value) # True print(value is None) # False
Defaults and Fallbacks
None is used as a clear marker that a value is intentionally missing. It lets you distinguish between "no value provided" and valid values like 0 or "", making defaults and fallbacks safer and more predictable.
1234567x = None print(x or "unknown") # 'unknown' print("unknown" if x is None else x) x = 0 print(x or "unknown") # 'unknown' (maybe wrong) print("unknown" if x is None else x) # 0
Functions and Parameters
This example shows how a function uses a parameter set to None as a signal that no tag was provided. It allows the function to assign a safe default while still letting the caller override it when needed.
1234567def add_tag(text, tag=None): if tag is None: tag = "general" return f"[{tag}] {text}" print(add_tag("hello")) # [general] hello print(add_tag("hello", "news")) # [news] hello
bytes and bytearray for Binary Data
str holds text, bytes and bytearray hold raw byte values.
1234b1 = b"hello" b2 = bytes([72, 105]) buf = bytearray(b"abc") buf[0] = 65
Encoding and Decoding
Encoding converts text into bytes so it can be stored or transferred reliably, while decoding restores those bytes back into readable text. Using a defined encoding like UTF-8 ensures characters are preserved correctly.
123text = "cafΓ©" data = text.encode("utf-8") back = data.decode("utf-8")
Mixing Text and Bytes
Text (str) and bytes (bytes) can't be combined directly because they represent different data types. To mix them safely, you must convert the text into bytes first using an explicit encoding.
123456try: b"ID:" + "123" except TypeError as e: print(e) ok = b"ID:" + "123".encode("utf-8")
Length Differences
Some characters take one text element but multiple bytes, so their length in str and in encoded form can differ. This happens because encodings like UTF-8 may use more than one byte to represent a single character.
123ch = "Γ©" len(ch) # 1 len(ch.encode()) # 2
Files
Binary files must be opened in "rb" mode so their raw bytes are read exactly as stored. This prevents Python from trying to interpret the data as text.
# with open("example.png", "rb") as f:
# blob = f.read()
Thanks for your feedback!