0
0
Pythonprogramming~10 mins

Environment variables usage in Python - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the module needed to access environment variables.

Python
import [1]
Drag options to blanks, or click blank then click option'
Asys
Bconfig
Cenv
Dos
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'sys' instead of 'os' because it also deals with system info.
Trying to import a non-existent module like 'env' or 'config'.
2fill in blank
medium

Complete the code to get the value of the environment variable named 'HOME'.

Python
home_dir = os.[1]('HOME')
Drag options to blanks, or click blank then click option'
Agetenvvalue
Bgetenvs
Cgetenv
Dgetenv_var
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect function names like 'getenvs' or 'getenv_var'.
Trying to access environment variables as dictionary keys without importing os.
3fill in blank
hard

Fix the error in the code to set an environment variable 'API_KEY' to '12345'.

Python
os.[1]['API_KEY'] = '12345'
Drag options to blanks, or click blank then click option'
Aenviron
Bsetenv
Cputenv
Dset
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'setenv' which is a function but not used like this in Python.
Trying to call 'putenv' as a dictionary.
Using 'set' which is not a valid method here.
4fill in blank
hard

Fill both blanks to create a dictionary of environment variables whose names start with 'APP_'.

Python
app_vars = {key: os.environ[key] for key in os.environ if key [1] 'APP_'}
Drag options to blanks, or click blank then click option'
Astartswith
Bendswith
Ccontains
Dequals
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'endswith' which checks the end of the string.
Using 'contains' which is not a string method.
Using 'equals' which checks full equality.
5fill in blank
hard

Fill all three blanks to safely get an environment variable 'PORT' as an integer with default 8080.

Python
port = int(os.getenv('[1]', [2])) if os.getenv('[3]') else 8080
Drag options to blanks, or click blank then click option'
APORT
B'8080'
D'80'
Attempts:
3 left
💡 Hint
Common Mistakes
Using integer 8080 directly as default in getenv, which expects a string.
Using different variable names inconsistently.
Not checking if the environment variable exists before converting.