Chapter 1: Thinking Like Python

From ABAP mindset to Python mindset without pretending your SAP experience does not matter

Chapter Goal

The Shift

In ABAP, the SAP system gives you a lot of structure. You have tables, data elements, domains, function modules, classes, packages, transports, and built-in reporting tools like ALV.

In Python, you build more of that structure yourself. That can feel strange at first, but it also means you can create small tools, reports, dashboards, automations, and cloud-ready apps without waiting for every piece to live inside the SAP system.

Important: This chapter is not asking you to abandon ABAP thinking. It is helping you translate it. Your ABAP experience is an advantage.

Concept Map: ABAP to Python

ABAP ConceptPython ConceptPlain-English Translation
Internal tableList of dictionaries, pandas DataFrameA collection of rows
Work areaDictionary or objectOne row or one structured record
SELECT from database tableSQL query through a database connectorRead records from a database
ALV gridBokeh DataTable, HTML table, Streamlit tableInteractive output for users
Function moduleFunction, module, package, API callReusable logic or a reusable service
Transport requestGit commit / deployment pipelineA controlled way to move changes

Internal Tables vs Python Lists

ABAP developers often start with internal tables because almost everything in reporting eventually becomes a table of records. Python can represent table-like data in different ways. The simplest is a list of dictionaries.

ABAP

DATA: lt_materials TYPE TABLE OF mara, ls_material TYPE mara. ls_material-matnr = '10000001'. ls_material-mtart = 'FERT'. APPEND ls_material TO lt_materials.

Python

materials = [] material = { "matnr": "10000001", "mtart": "FERT" } materials.append(material)

A Python list of dictionaries is flexible and easy to understand. Later, when we want filtering, grouping, exporting, or charting, we will often convert this kind of data into a pandas DataFrame.

Work Area vs Dictionary

A work area usually represents one record. A Python dictionary can serve the same purpose.

ABAP

DATA: ls_material TYPE mara. ls_material-matnr = '10000001'. ls_material-maktx = 'Demo Material'.

Python

material = { "matnr": "10000001", "maktx": "Demo Material" }
ABAP Mindset: Think of a dictionary as a lightweight structure where each field has a name and a value.

SELECT Statements: You Have More Than One Python Path

The first version of this chapter jumped from ABAP SELECT statements straight to APIs. That is a valid modern path, but it is not the only path. Many developers first need to understand how Python reads data from familiar places: databases, CSV files, Excel files, and then APIs.

Better mental model: ABAP usually reads data from SAP tables. Python reads data from wherever the data lives.

Path A: ABAP SELECT vs Python SELECT from PostgreSQL

If you are not ready to think in APIs yet, start with a relational database. The idea is familiar: connect, run SQL, receive rows, display or process the result.

ABAP

DATA: lt_materials TYPE TABLE OF mara. SELECT matnr, mtart, matkl FROM mara INTO TABLE @lt_materials UP TO 100 ROWS.

Python + PostgreSQL

import psycopg2 connection = psycopg2.connect( host="localhost", database="demo", user="postgres", password="your_password" ) cursor = connection.cursor() cursor.execute( "SELECT matnr, mtart, matkl " "FROM materials " "LIMIT 100" ) rows = cursor.fetchall() for row in rows: print(row) cursor.close() connection.close()

For PostgreSQL, one common package is psycopg2.

python -m pip install psycopg2-binary

In production environments, your team may prefer a specific driver or SQLAlchemy, but this is enough to understand the basic idea.

The connection is the database session. The cursor is the object that sends the SQL and receives the result. The rows are your internal table equivalent.

Path B: ABAP SELECT vs Python SELECT with pandas

For reporting work, pandas is often more convenient because it gives you a table-like object called a DataFrame. This feels closer to working with an internal table that also has built-in analysis functions.

ABAP

SELECT matnr, mtart, matkl FROM mara INTO TABLE @DATA(lt_materials) UP TO 100 ROWS.

Python + pandas

import pandas as pd import psycopg2 connection = psycopg2.connect( host="localhost", database="demo", user="postgres", password="your_password" ) sql = ( "SELECT matnr, mtart, matkl " "FROM materials " "LIMIT 100" ) df = pd.read_sql(sql, connection) print(df.head()) connection.close()
Python Translation: A pandas DataFrame is not exactly an internal table, but for reporting and analysis, it will often feel like your most natural bridge from ABAP.

Path C: Reading CSV or Excel Instead of a Database

Many useful Python tools start with exported data. Maybe someone gives you a CSV file, an Excel workbook, or a flat file from another system. You can still build something valuable without connecting directly to SAP or another database.

CSV

import pandas as pd df = pd.read_csv("materials.csv") print(df.head())

Excel

import pandas as pd df = pd.read_excel("materials.xlsx") print(df.head())

Filter

fert = df[df["mtart"] == "FERT"] print(fert)

You can practice Python reporting without dealing with credentials, firewalls, SAP authorizations, or network access. That makes CSV and Excel perfect for early projects.

Path D: SELECT vs API

Once you are comfortable with files and databases, APIs become easier to understand. Instead of selecting from a table, you request data from an endpoint.

ABAP

SELECT matnr, mtart, matkl FROM mara INTO TABLE @DATA(lt_materials) UP TO 100 ROWS.

Python + API

import requests url = "https://example.com/api/materials" response = requests.get(url) response.raise_for_status() materials = response.json() for material in materials: print(material["matnr"])

With an API, you usually do not control the SQL. The service decides what data is available, how it is filtered, and how it is returned. Your Python program becomes the consumer of that service.

Looping Through Data

Looping is one of the easiest concepts to translate.

ABAP

LOOP AT lt_materials INTO DATA(ls_material). WRITE: / ls_material-matnr, ls_material-mtart. ENDLOOP.

Python

for material in materials: print(material["matnr"], material["mtart"])
Common mistake: Python indentation matters. In ABAP, keywords close your blocks. In Python, indentation defines the block.

IF Conditions

Conditional logic is also very similar once you get used to Python syntax.

ABAP

IF ls_material-mtart = 'FERT'. WRITE: / 'Finished product'. ELSE. WRITE: / 'Other material'. ENDIF.

Python

if material["mtart"] == "FERT": print("Finished product") else: print("Other material")

Functions: FORM, Function Module, or Method?

Python functions are small reusable blocks of logic. Do not make this more complicated than it needs to be at first.

ABAP

FORM format_material USING iv_matnr TYPE matnr CHANGING cv_text TYPE string. cv_text = |Material: { iv_matnr }|. ENDFORM.

Python

def format_material(matnr): return f"Material: {matnr}" text = format_material("10000001") print(text)
Python Translation: Start with functions. Later, when your project grows, you can move into modules, classes, and packages.

ALV vs Bokeh

ABAP developers love ALV because it gives users a usable table quickly. Python does not have ALV, but it has several ways to create interactive output. For this book, our first ALV-like path will use Bokeh.

ABAP ALV

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY' EXPORTING i_structure_name = 'MARA' TABLES t_outtab = lt_materials.

Python Bokeh DataTable

from bokeh.models import ColumnDataSource, DataTable, TableColumn from bokeh.io import show source = ColumnDataSource(df) columns = [ TableColumn(field="matnr", title="Material"), TableColumn(field="mtart", title="Material Type"), TableColumn(field="matkl", title="Material Group") ] table = DataTable(source=source, columns=columns, width=800) show(table)

Bokeh can create interactive browser-based output without requiring a full web application at the beginning. That makes it a friendly bridge from classic report output to modern dashboard output.

Mini Exercise: Translate This in Your Head

Before going to the first full project, try to mentally translate this ABAP pattern:

SELECT matnr, mtart FROM mara INTO TABLE @DATA(lt_materials) WHERE mtart = 'FERT'. LOOP AT lt_materials INTO DATA(ls_material). WRITE: / ls_material-matnr. ENDLOOP.
import pandas as pd df = pd.read_csv("materials.csv") fert_materials = df[df["mtart"] == "FERT"] for _, material in fert_materials.iterrows(): print(material["matnr"])
Checkpoint: You do not need to memorize every package yet. You only need to understand the shape of the translation: read data, store it in a table-like structure, filter it, loop through it, and display it.

Chapter 1 Checkpoint

Next: In Chapter 2, we build the first useful project: an ABAP-style material table report rebuilt in Python with pandas and Bokeh.
End of Chapter 1