Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen None- und Binärdaten | Interaktionen Zwischen Datentypen
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Datentypen in Python

bookNone- und Binärdaten

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

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

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 4. Kapitel 3

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

bookNone- und Binärdaten

Swipe um das Menü anzuzeigen

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

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

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 4. Kapitel 3
some-alt