None og Binære Data
1234567result = None email = None print(result is None) # True print(email is None) # True if result is None: print("No result yet")
123value = 0 print(not value) # True print(value is None) # False
1234567x = 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
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
1234b1 = b"hello" b2 = bytes([72, 105]) buf = bytearray(b"abc") buf[0] = 65
123text = "café" data = text.encode("utf-8") back = data.decode("utf-8")
123456try: b"ID:" + "123" except TypeError as e: print(e) ok = b"ID:" + "123".encode("utf-8")
123ch = "é" len(ch) # 1 len(ch.encode()) # 2
Alt var klart?
Takk for tilbakemeldingene dine!
Seksjon 4. Kapittel 3
Spør AI
Spør AI
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?
Fantastisk!
Completion rate forbedret til 3.45
None og Binære Data
Sveip for å vise menyen
1234567result = None email = None print(result is None) # True print(email is None) # True if result is None: print("No result yet")
123value = 0 print(not value) # True print(value is None) # False
1234567x = 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
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
1234b1 = b"hello" b2 = bytes([72, 105]) buf = bytearray(b"abc") buf[0] = 65
123text = "café" data = text.encode("utf-8") back = data.decode("utf-8")
123456try: b"ID:" + "123" except TypeError as e: print(e) ok = b"ID:" + "123".encode("utf-8")
123ch = "é" len(ch) # 1 len(ch.encode()) # 2
Alt var klart?
Takk for tilbakemeldingene dine!
Seksjon 4. Kapittel 3