Is there any way to create a font that transforms depending on the word?

Is it possible to create not only ligatures, but also, for example, to change the appearance of the first letter A when entering a word in which the first letter is A and the last letter is B?

Theoretically yes, but if you don’t know the length of the word, you will need to write rules for each possible length. This can get out of hand very quickly. You could limit yourself to say 8 letters max, then you need 9 rules like

sub A' B by A.alt;
sub A' @letter B by A.alt;
sub A' @letter @letter B by A.alt;
sub A' @letter @letter @letter B by A.alt;
sub A' @letter @letter @letter @letter B by A.alt;
sub A' @letter @letter @letter @letter @letter B by A.alt;
sub A' @letter @letter @letter @letter @letter @letter B by A.alt;
sub A' @letter @letter @letter @letter @letter @letter @letter B by A.alt;
sub A' @letter @letter @letter @letter @letter @letter @letter @letter B by A.alt;

where @letter is a group containing all other glyphs that you allow to appear between A and B.

Can I use python for concise coding?

Sure, you could generate the feature code via python.

In this example here I just used the convenience of a text editor like VSCode (duplicate lines, etc).

You can use Python in order to generate the feature code text, but you cannot integrate Python into the font file.

You are thus limited by the OpenType feature code, which essentially only substitutes specific glyphs with other glyphs.

Thank you!