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
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
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
123text = "cafΓ©" data = text.encode("utf-8") back = data.decode("utf-8")
Mixing Text and Bytes
123456try: b"ID:" + "123" except TypeError as e: print(e) ok = b"ID:" + "123".encode("utf-8")
Length Differences
123ch = "Γ©" len(ch) # 1 len(ch.encode()) # 2
Files
# 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 when to use `None` versus other falsey values?
How do I safely convert between text and bytes?
What are common mistakes when mixing text and binary data?
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
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
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
123text = "cafΓ©" data = text.encode("utf-8") back = data.decode("utf-8")
Mixing Text and Bytes
123456try: b"ID:" + "123" except TypeError as e: print(e) ok = b"ID:" + "123".encode("utf-8")
Length Differences
123ch = "Γ©" len(ch) # 1 len(ch.encode()) # 2
Files
# with open("example.png", "rb") as f:
# blob = f.read()
Thanks for your feedback!