0
0
Apache Airflowdevops~10 mins

Dynamic task generation with loops in Apache Airflow - Interactive Code Practice

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

Complete the code to create a dynamic task inside the DAG using a loop.

Apache Airflow
for i in range(3):
    task = PythonOperator(task_id=f'task_[1]', python_callable=my_function, dag=dag)
Drag options to blanks, or click blank then click option'
Ai
Bindex
Ctask
Dcount
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name not defined in the loop like 'index' or 'count'.
Forgetting to use the loop variable to create unique task IDs.
2fill in blank
medium

Complete the code to add tasks to the DAG dynamically using a loop and list comprehension.

Apache Airflow
tasks = [PythonOperator(task_id=f'task_[1]', python_callable=my_function, dag=dag) for [1] in range(5)]
Drag options to blanks, or click blank then click option'
Acount
Btask
Cindex
Di
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name not defined in the comprehension like 'task' or 'count'.
Not matching the loop variable in both places.
3fill in blank
hard

Fix the error in the dynamic task creation loop by completing the missing part.

Apache Airflow
for i in range(3):
    task = PythonOperator(task_id=f'task_[1]', python_callable=my_function, dag=dag)
    task.set_downstream(next_task)
Drag options to blanks, or click blank then click option'
Atask
Bi
Cnext_task
Dindex
Attempts:
3 left
💡 Hint
Common Mistakes
Using a fixed string for task_id causing duplicate task IDs.
Using an undefined variable in the f-string.
4fill in blank
hard

Fill both blanks to create tasks dynamically and set dependencies between them.

Apache Airflow
tasks = []
for [1] in range(3):
    task = PythonOperator(task_id=f'task_[1]', python_callable=my_function, dag=dag)
    tasks.append(task)
for i in range(2):
    tasks[i].[2](tasks[i+1])
Drag options to blanks, or click blank then click option'
Ai
Bset_upstream
Cset_downstream
Dappend
Attempts:
3 left
💡 Hint
Common Mistakes
Using set_upstream instead of set_downstream for dependencies.
Not using the loop variable for unique task IDs.
5fill in blank
hard

Fill all three blanks to create a dictionary of tasks dynamically with conditions.

Apache Airflow
tasks = {f'task_[1]': PythonOperator(task_id=f'task_[1]', python_callable=my_function, dag=dag) for [2] in range(5) if [3] % 2 == 0}
Drag options to blanks, or click blank then click option'
Ai
Bj
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variables inconsistently.
Not applying the modulo condition correctly.