0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

初心者の覚書 Python 「isdigit」の動き

Last updated at Posted at 2020-08-04

Python isdigitの動き

先生に教えてもらった内容が面白かったので、覚書です。
やり方だけ覚えて自分で検証した内容なので、「ここ間違ってる」と気づいた方はぜひご一報をば。

isdigitは「str型」の「整数」をTrue ,Falsで返す。

特徴
・str型の数字(整数)のみであればTrue。(0もTrue。ただし、0始まりの数字はエラー)
・str型の数字でも、負の数はFalse。(マイナスが文字とみなされている?)
・str型の数字でも、少数はFalse。(小数点以下が0でもFalse)
・str型の数字でも、文字や記号が混ざるとFalse。(演算記号もFalse)
・str型、int型共に、0始まりの数字はSyntaxError。
・’’はFalse。
・文字はFalse。
・int型の数字はSyntaxError。(計算結果含む)
・0以外の、0始まりの数字は、型によらずAttributeError。

’’’
print('1'.isdigit()) #True
print('100'.isdigit()) #True
print('0'.isdigit()) #True

print('a'.isdigit()) #False
print('10.0'.isdigit()) #False
print('2*3'.isdigit()) #False
print(''.isdigit()) #False
print('11a'.isdigit()) #False
print('-1'.isdigit()) #False

print(1.isdigit()) # SyntaxError: invalid syntax
print(5%3.isdigit()) # SyntaxError: invalid syntax
print(3+4.isdigit()) #SyntaxError: invalid syntax

print(01.isdigit()) #SyntaxError: invalid character in identifier
print('01'.isdigit()) #SyntaxError: invalid character in identifier

print('0.1'.isdigit()) # AttributeError: 'float' object has no attribute 'isdigit'
print(0.1.isdigit()) # AttributeError: 'float' object has no attribute 'isdigit'
’’’

以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?