Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn None and Binary Data | Cross-Type Interactions
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Data Types in Python

bookNone 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".

1234567
result = None email = None print(result is None) # True print(email is None) # True if result is None: print("No result yet")
copy

Use is None instead of truthiness checks, since 0 and "" are also falsey.

123
value = 0 print(not value) # True print(value is None) # False
copy

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.

1234567
x = 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
copy

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.

1234567
def 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
copy

bytes and bytearray for Binary Data

str holds text, bytes and bytearray hold raw byte values.

1234
b1 = b"hello" b2 = bytes([72, 105]) buf = bytearray(b"abc") buf[0] = 65
copy

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.

123
text = "cafΓ©" data = text.encode("utf-8") back = data.decode("utf-8")
copy

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.

123456
try: b"ID:" + "123" except TypeError as e: print(e) ok = b"ID:" + "123".encode("utf-8")
copy

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.

123
ch = "Γ©" len(ch) # 1 len(ch.encode()) # 2
copy

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()
question mark

Which check correctly detects a missing value?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 4. ChapterΒ 3

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

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?

bookNone 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".

1234567
result = None email = None print(result is None) # True print(email is None) # True if result is None: print("No result yet")
copy

Use is None instead of truthiness checks, since 0 and "" are also falsey.

123
value = 0 print(not value) # True print(value is None) # False
copy

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.

1234567
x = 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
copy

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.

1234567
def 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
copy

bytes and bytearray for Binary Data

str holds text, bytes and bytearray hold raw byte values.

1234
b1 = b"hello" b2 = bytes([72, 105]) buf = bytearray(b"abc") buf[0] = 65
copy

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.

123
text = "cafΓ©" data = text.encode("utf-8") back = data.decode("utf-8")
copy

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.

123456
try: b"ID:" + "123" except TypeError as e: print(e) ok = b"ID:" + "123".encode("utf-8")
copy

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.

123
ch = "Γ©" len(ch) # 1 len(ch.encode()) # 2
copy

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()
question mark

Which check correctly detects a missing value?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 4. ChapterΒ 3
some-alt