Here's a solution that makes use of the general number highlighting approach presented in Listings: color numbers only out of keywords.
We need to make some changes to the OutputOther hook from that solution, because in this case you want to override the default style no matter whether you are in a comment or not. The new hook looks like:
\lst@AddToHook{OutputOther}{%
\lst@ifparsenumbers
\expandafter\@hook@ifnumber\the\lst@token\@end {%
\let\orig@thestyle=\lst@thestyle
\def\lst@thestyle{\orig@thestyle\lst@numbersstyle}%
}{}%
\fi
}
Also to your \lstset command the following lines have to be added:
parsenumbers=true,
numbersstyle=\upshape
Wherever a number is then parsed by listings in the input, the numbersstyle is added to the currently active style.
Full example code:
\documentclass{book}
\usepackage{xcolor}
\usepackage{textcomp,listings}%
\makeatletter
%%% Copied from https://tex.stackexchange.com/a/500690/23765
% Some conditional tests
\def\@genericif#1{#1\expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi}
\def\@ifdigit#1{\@genericif{\ifnum1<1\noexpand#1\relax}}
\def\@ifempty#1{\@genericif{\if\relax\detokenize{#1}\relax}}
% The main parsing macros
\def\parse@num#1{%
\@ifempty{#1}%
{\parse@num@false}%
{\@genericif{\parsesign}%
{\parse@num@sign#1{}\@end}%
{\parse@num@dig#1{}\@end}%
}%
}
% Parse sign
\def\parse@num@sign#1#2\@end{%
\@genericif{\ifx\parse@num@minus#1}%
{\@ifempty{#2}{\parse@num@false}{\parse@num@dig#2\@end}}%
{\@genericif{\ifx\parse@num@plus#1}%
{\@ifempty{#2}{\parse@num@false}{\parse@num@dig#2\@end}}%
{\parse@num@dig#1#2\@end}%
}%
}
% Parse first digit
\def\parse@num@dig#1#2\@end{%
\@ifdigit{#1}%
{\@ifempty{#2}{\parse@num@true}{\parse@num@digs#2\@end}}%
{\parse@num@false}%
}
% Parse optional following digits
\def\parse@num@digs#1#2\@end{%
\@ifdigit{#1}{%
\@ifempty{#2}%
{\parse@num@true}%
{\parse@num@digs#2\@end}%
}{%
\@genericif{\parsefloat}{%
\@genericif{\ifx\parse@num@point#1}%
{\@ifempty{#2}{\parse@num@false}{\parse@num@decs#2\@end}}%
{\parse@num@false}%
}{\parse@num@false}%
}%
}
% Parse decimal places
\def\parse@num@decs#1#2\@end{%
\@ifdigit{#1}{%
\@ifempty{#2}%
{\parse@num@true}%
{\parse@num@decs#2\@end}%
}{\parse@num@false}%
}
% User interface
\newcommand\ifnumber[4][]{%
\begingroup
\let\parsesign=\iftrue
\let\parsefloat=\iftrue
\let\parse@num@minus=-%
\let\parse@num@plus=+%
\let\parse@num@point=.%
#1%
\def\parse@num@true{\endgroup#3}%
\def\parse@num@false{\endgroup#4}%
\parse@num{#2}%
}
%%% Additions to the listings package
\lst@Key{numbersstyle}{}{\def\lst@numbersstyle{#1}}
\lst@Key{parsenumbers}{false}[t]{\lstKV@SetIf{#1}\lst@ifparsenumbers}
\lst@AddToHook{OutputOther}{%
\lst@ifparsenumbers
\expandafter\@hook@ifnumber\the\lst@token\@end {%
\let\orig@thestyle=\lst@thestyle
\def\lst@thestyle{\orig@thestyle\lst@numbersstyle}%
}{}%
\fi
}
\def\@hook@ifnumber#1#2\@end{%
\@genericif{\ifx\lst@nolig#1}%
{\@hook@ifnumber@{#2}}%
{\@hook@ifnumber@{#1#2}}%
}
\def\@hook@ifnumber@{%
\ifnumber[\expandafter\let\expandafter\parse@num@minus\csname lst@um-\endcsname]%
}
\makeatother
\lstnewenvironment{python}[1][]{%
\lstset{%
mathescape=false,%
language=python,%
basicstyle=\ttfamily\normalsize,%
otherkeywords={*,\{, \} },%
keywordstyle=\color{black},%
stringstyle=\color{black},%
showstringspaces=false,%
emph={class, pass, in, for, while, if, is, elif, else, not, and, or,%
def, print, exec, break, continue, return},%
emphstyle=\color{black}\bfseries,%
emph={[3]True, False, None, self},%
emphstyle=[2]\color{black!10},%
emph={[3]from, import, as},%
emphstyle=[3]\color{black},%
upquote=true,%
morecomment=[s]{"""}{"""},%
commentstyle=\color{gray}\slshape,%
aboveskip=12pt,belowskip=12pt,xleftmargin=-2pt,xrightmargin=3pt,framexleftmargin=20pt,framextopmargin=1pt,%
rulesepcolor=\color{gray},#1,
parsenumbers=true,
numbersstyle=\upshape
}}{}%
\begin{document}
\begin{python}
In [7]: np.searchsorted(X, 0.5)
Out[7]: 4998210 # This is 1 for test 1000
"""
Some text 1234
"""
\end{python}
\end{document}

EDIT: The problem of the 0 not showing up in upright font is due to the fact that listings parses 0 and , as one unit (as both have codes digit or other). The OutputOther hook then tries to parse 0, as a number which fails and thus doesn't give the expected font.
As a workaround you can add the following line to the \lstset command:
literate={,}{{\char`\,}}{1}
which outputs

This makes the parser break before it reads the comma. In this special case there should be no side-effects, because commas shouldn't be part of keywords or other special syntax elements. Unfortunately, listings makes it hard to add the processing of new syntax elements, so working around one problem here often raises another one there.
roman font), but in the commented text it comes initalic, but I need those to be inroman– MadyYuvi Aug 16 '19 at 05:39