📊 Capítulo 04: Bitwise & F-Strings Avançadas
Especialização em Sistemas com Python
🗺️ Representação Interna de Strings e UTF-8 no CPython (PEP 393)
flowchart TD
STR["Objeto str no CPython (Flexible String Representation)"]
--> CHECK{"Qual é o maior code point Unicode da string?"}
CHECK --> |"<= 127 (Apenas ASCII)"| ASCII["PyASCIIObject: 1 byte por caractere (Memória mínima!) ⚡"]
CHECK --> |"<= 255 (Latin-1)"| LATIN["Compact 1 byte (ISO-8859-1)"]
CHECK --> |"<= 65535 (BMP)"| UCS2["Compact 2 bytes (UCS-2)"]
CHECK --> |"> 65535 (Emojis / Raros)"| UCS4["Compact 4 bytes (UCS-4)"]
style STR fill:#ede7f6,stroke:#512da8,stroke-width:2px
style CHECK fill:#fff3e0,stroke:#ff9800,stroke-width:2px
style ASCII fill:#e8f5e9,stroke:#4caf50,stroke-width:2px
style LATIN fill:#e1f5fe,stroke:#03a9f4,stroke-width:1.5px
style UCS2 fill:#fff9c4,stroke:#fbc02d,stroke-width:1.5px
style UCS4 fill:#c8e6c9,stroke:#2e7d32,stroke-width:2px
🎯 Slide 1: Formatação Avançada de F-Strings
valor = 12500.5
taxa = 0.0825
# Formatação monetária com separador de milhar:
print(f"Total: R$ {valor:,.2f}") # R$ 12,500.50
# Porcentagem automática:
print(f"Aliquota: {taxa:.1%}") # Aliquota: 8.3%
# Auto-documentação para depuração (Python 3.8+):
print(f"{valor=}") # valor=12500.5
🎯 Slide 2: Operações Bitwise e Máscaras
-
&(AND),|(OR),^(XOR),~(NOT),<<,>>. - Sinalizadores de Permissão (Flags): ```python READ = 0b001 WRITE = 0b010 EXEC = 0b100
permissoes = READ | WRITE tem_escrita = bool(permissoes & WRITE) # True ```
🎯 Slide 3: bytes vs str
str: Texto legível composto por caracteres Unicode.bytes: Sequência bruta de octetos binários.- Sempre use
.encode('utf-8')para enviar pela rede e.decode('utf-8')para ler!
Slide 1 de 1
Atalhos: ← → Navegar • Espaço Avançar • F Tela Cheia • Home Início • End Fim