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
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

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

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

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

Mixing Text and Bytes

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

Length Differences

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

Files

# 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 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

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

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

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

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

Mixing Text and Bytes

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

Length Differences

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

Files

# 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