Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære None og Binære Data | Interaksjoner Mellom Ulike Datatyper
Datatyper i Python

bookNone og Binære Data

1234567
result = None email = None print(result is None) # True print(email is None) # True if result is None: print("No result yet")
copy
123
value = 0 print(not value) # True print(value is None) # False
copy
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
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
1234
b1 = b"hello" b2 = bytes([72, 105]) buf = bytearray(b"abc") buf[0] = 65
copy
123
text = "café" data = text.encode("utf-8") back = data.decode("utf-8")
copy
123456
try: b"ID:" + "123" except TypeError as e: print(e) ok = b"ID:" + "123".encode("utf-8")
copy
123
ch = "é" len(ch) # 1 len(ch.encode()) # 2
copy
question mark

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 4. Kapittel 3

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

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 common mistakes to avoid when working with None and binary data?

bookNone og Binære Data

Sveip for å vise menyen

1234567
result = None email = None print(result is None) # True print(email is None) # True if result is None: print("No result yet")
copy
123
value = 0 print(not value) # True print(value is None) # False
copy
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
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
1234
b1 = b"hello" b2 = bytes([72, 105]) buf = bytearray(b"abc") buf[0] = 65
copy
123
text = "café" data = text.encode("utf-8") back = data.decode("utf-8")
copy
123456
try: b"ID:" + "123" except TypeError as e: print(e) ok = b"ID:" + "123".encode("utf-8")
copy
123
ch = "é" len(ch) # 1 len(ch.encode()) # 2
copy
question mark

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 4. Kapittel 3
some-alt