\tcbuselibrary{listings}
\newlength\inwd
\setlength\inwd{1.5cm}

%\newcounter{ipythcntr}
%\renewcommand{\theipythcntr}{\texttt{[\arabic{ipythcntr}]}}
% ------------------------------------------------------------------------- New add
\newcounter{ipythcntr}
\newcounter{ipythcntrold}
\renewcommand{\theipythcntr}{%
    \texttt{[\arabic{ipythcntr}]}
    \setcounter{ipythcntrold}{\value{ipythcntr}}%
}
% -----------------------------------------------------------
\AddToHook{shipout/background}{\setcounter{ipythcntr}{0}}
\newtcblisting{pyin}[1][]{%
  sharp corners,
  enlarge left by=\inwd,
  width=\linewidth-\inwd,
  enhanced,
  boxrule=0pt,
  colback=light_gray,
  listing only,
  top=0pt,
  bottom=0pt,
  overlay={
    \node[
      anchor=north east,
      text width=\inwd,
      font=\footnotesize\ttfamily\color{mykey},
      inner ysep=2mm,
      inner xsep=0pt,
      outer sep=0pt
      ] 
      at (frame.north west)
      {\refstepcounter{ipythcntr}\label{#1}In \theipythcntr:};
  }
  listing engine=listing,
  listing options={
    aboveskip=1pt,
    belowskip=1pt,
    basicstyle=\ttfamily,
    language=Python,
    keywordstyle=\color{mykey},
    showstringspaces=false,
    stringstyle=\color{mygreen},
    tabsize=4,
  },
}
\newtcblisting{pyprint}{
  sharp corners,
  enlarge left by=\inwd,
  width=\linewidth-\inwd,
  enhanced,
  boxrule=0pt,
  colback=white,
  listing only,
  top=0pt,
  bottom=0pt,
  overlay={
    \node[
      anchor=north east,
      text width=\inwd,
      font=\ttfamily\color{mykey},
      inner ysep=2mm,
      inner xsep=0pt,
      outer sep=0pt
      ] 
      at (frame.north west)
      {};
  }
  listing engine=listing,
  listing options={
      aboveskip=1pt,
      belowskip=1pt,
      basicstyle=\ttfamily,
      language=Python,
      keywordstyle=\color{mykey},
      showstringspaces=false,
      stringstyle=\color{mygreen}
    },
}
\newtcblisting{pyout}[1][\theipythcntr]{
  sharp corners,
  enlarge left by=\inwd,
  width=\linewidth-\inwd,
  enhanced,
  boxrule=0pt,
  colback=white,
  listing only,
  top=0pt,
  bottom=0pt,
  overlay={
    \node[
      anchor=north east,
      text width=\inwd,
      font=\footnotesize\ttfamily\color{mykey},
      inner ysep=2mm,
      inner xsep=0pt,
      outer sep=0pt
      ] 
      at (frame.north west)
      %{\setcounter{ipythcntr}{\value{ipythcntr}}Out#1:};
  % ------------------------------------------------------------------------- New add
   {\ifnum\value{ipythcntr}=0
    \setcounter{ipythcntr}{\value{ipythcntrold}}
  \else
    \setcounter{ipythcntr}{\value{ipythcntr}}
  \fi
  Out#1:};
  %--------------------------------------------------------------    
  }
  listing engine=listing,
  listing options={
      aboveskip=1pt,
      belowskip=1pt,
      basicstyle=\ttfamily,
      language=Python,
      keywordstyle=\color{mykey},
      showstringspaces=false,
      stringstyle=\color{mygreen}
    },
}