Q6
How do you read environment variables using `os.environ` and `os.getenv()`? What is the difference?
💬Answer
os.environ["VAR"]: Directly accesses the environment variable dictionary. If"VAR"is missing, it raises aKeyErrorand crashes.os.getenv("VAR", default): Safely queries the variable. If missing, it returnsNone(or the specified default value) without crashing.
import os
# Safe check with fallback value
db_port = os.getenv("DB_PORT", "5432")
Related Python Questions
View All PythonQuestions →
Created by
Apurv Gujjar
DevOps & Cloud Engineer
Specialized in:DevOpsAWSGCPKubernetesTerraformDocker
View Portfolio