Given this Airflow DAG snippet, what will be the output logged when the python_task runs?
from airflow import DAG from airflow.operators.python import PythonOperator from datetime import datetime def greet(): print('Hello from Airflow!') default_args = {'start_date': datetime(2024, 1, 1)} dag = DAG('greet_dag', default_args=default_args, schedule_interval='@daily') python_task = PythonOperator( task_id='greet_task', python_callable=greet, dag=dag )
Think about what the print statement inside the python_callable does.
The PythonOperator runs the greet function, which prints 'Hello from Airflow!'. This output appears in the task logs.
Which option correctly passes arguments to a Python function using PythonOperator?
from airflow import DAG from airflow.operators.python import PythonOperator from datetime import datetime def add_numbers(a, b): print(a + b) default_args = {'start_date': datetime(2024, 1, 1)} dag = DAG('add_dag', default_args=default_args, schedule_interval='@daily')
Check how keyword arguments are passed to the callable in Airflow.
op_kwargs is the correct way to pass named arguments to the Python function in PythonOperator. Options A and C use positional arguments but only op_args is valid for positional, and option A calls the function immediately which is incorrect.
What error will Airflow raise if python_callable is set to None in PythonOperator?
from airflow import DAG from airflow.operators.python import PythonOperator from datetime import datetime default_args = {'start_date': datetime(2024, 1, 1)} dag = DAG('error_dag', default_args=default_args, schedule_interval='@daily') python_task = PythonOperator(task_id='error_task', python_callable=None, dag=dag)
Think about what happens when Python tries to call None as a function.
Setting python_callable=None means Airflow tries to call None as a function, which raises a TypeError because NoneType is not callable.
Given these two PythonOperator tasks in a DAG, which option shows the correct order of execution?
from airflow import DAG from airflow.operators.python import PythonOperator from datetime import datetime def task1(): print('Task 1') def task2(): print('Task 2') default_args = {'start_date': datetime(2024, 1, 1)} dag = DAG('order_dag', default_args=default_args, schedule_interval='@daily') t1 = PythonOperator(task_id='task1', python_callable=task1, dag=dag) t2 = PythonOperator(task_id='task2', python_callable=task2, dag=dag) t1 >> t2
Look at the dependency operator >> between tasks.
The operator >> means Task 1 must finish before Task 2 starts, so Task 1 runs first, then Task 2.
Which option correctly captures the return value of a Python function executed by PythonOperator for use in downstream tasks?
from airflow import DAG from airflow.operators.python import PythonOperator from airflow.operators.python import get_current_context from datetime import datetime def generate_value(): return 42 def print_value(): context = get_current_context() value = context['ti'].xcom_pull(task_ids='generate_task') print(f'Received value: {value}') default_args = {'start_date': datetime(2024, 1, 1)} dag = DAG('xcom_dag', default_args=default_args, schedule_interval='@daily') generate_task = PythonOperator(task_id='generate_task', python_callable=generate_value, dag=dag) print_task = PythonOperator(task_id='print_task', python_callable=print_value, dag=dag) generate_task >> print_task
Think about how Airflow shares data between tasks.
Airflow uses XComs to pass data between tasks. Returning a value from a PythonOperator stores it in XCom automatically. The downstream task can retrieve it using xcom_pull.