Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shorter way to check if a string is not isdigit()

Tags:

python

For the sake of learning, is there a shorter way to do: if string.isdigit() == False :

I tried: if !string.isdigit() : and if !(string.isdigit()) : which both didn't work.

like image 947
Nick Rutten Avatar asked May 02 '13 10:05

Nick Rutten


2 Answers

Python's "not" operand is not, not !.

Python's "logical not" operand is not, not !.

like image 121
Meoiswa Avatar answered Oct 06 '22 23:10

Meoiswa


In python, you use the not keyword instead of !:

if not string.isdigit():
    do_stuff()

This is equivalent to:

if not False:
    do_stuff()

i.e:

if True:
    do_stuff()

Also, from the PEP 8 Style Guide:

Don't compare boolean values to True or False using ==.

Yes: if greeting:

No: if greeting == True

Worse: if greeting is True:

like image 36
TerryA Avatar answered Oct 06 '22 23:10

TerryA