Casio Python - type()

Casioグラフ関数電卓の Python を使ってみる
- リファレンス
<目次>
type()初版:2020/07/05
[対応モデル] - fx-CG50 OS3.20 以降、fx-9750GIII / fx-9860GIII OS3.21 以降
オブジェクトを与えると、オブジェクトの型を返します。
スクリプト例:
print(type(1234)) #整数型 (int型)
print(type(12.34)) #浮動小数点型 (float型)
print(type('abcd')) #文字列型 (str型)
print(type(['a', 'b', 1, 2])) #リスト型 (list型)
print(type(1, 2, 3, 4)) #タプル型 (tuple型)
print(type({'one':'eins', 'two':'zwei', 'three':'drei'})) #辞書型 (dict型)
print(type({1, 2, 3, 4})) #集合型 (set型)
if type(1234) is int: #整数型なら 'int' と出力
print('int')
if type(12.34) is float: #不動小数点型なら 'float' と出力
print('float')
if type('abcd') is str: #文字列型なら 'str' と出力
print('str')
if type(['a', 'b', 1, 2]) is list: #リスト型なら 'list' と出力
print('list')
if type((1, 2, 3, 4)) is tuple: #タプル型なら 'tuple' と出力
print('tuple')
if type({'one':'eins', 'two':'zwei', 'three':'drei'}) is dict: #辞書型なら'dict'と出力
print('dict')
if type({1, 2, 3, 4}) is set: #集合型なら 'set' と出力
print('set')
print(type(12.34)) #浮動小数点型 (float型)
print(type('abcd')) #文字列型 (str型)
print(type(['a', 'b', 1, 2])) #リスト型 (list型)
print(type(1, 2, 3, 4)) #タプル型 (tuple型)
print(type({'one':'eins', 'two':'zwei', 'three':'drei'})) #辞書型 (dict型)
print(type({1, 2, 3, 4})) #集合型 (set型)
if type(1234) is int: #整数型なら 'int' と出力
print('int')
if type(12.34) is float: #不動小数点型なら 'float' と出力
print('float')
if type('abcd') is str: #文字列型なら 'str' と出力
print('str')
if type(['a', 'b', 1, 2]) is list: #リスト型なら 'list' と出力
print('list')
if type((1, 2, 3, 4)) is tuple: #タプル型なら 'tuple' と出力
print('tuple')
if type({'one':'eins', 'two':'zwei', 'three':'drei'}) is dict: #辞書型なら'dict'と出力
print('dict')
if type({1, 2, 3, 4}) is set: #集合型なら 'set' と出力
print('set')
動作:
実行結果は、下記になります。
<class 'int'>
<class 'float'>
<class 'str'>
<class 'list'>
<class 'tuple'>
<class 'dict'>
<class 'set'>
int
float
str
list
tuple
dict
set
Casio Python の組込データ型、int型(整数型)、float型(浮動小数点型)、str型(文字列型)、list型(リスト型)、tuple型(タプル型)、dict型(辞書型)、set型(集合型)の判定を行っています。
前半は print() を使って、データ型をそのまま表示しています。
後半は、is ステートメントを使った判定を行っています (実際によく使う例だと思います)。
<class 'int'>
<class 'float'>
<class 'str'>
<class 'list'>
<class 'tuple'>
<class 'dict'>
<class 'set'>
int
float
str
list
tuple
dict
set
Casio Python の組込データ型、int型(整数型)、float型(浮動小数点型)、str型(文字列型)、list型(リスト型)、tuple型(タプル型)、dict型(辞書型)、set型(集合型)の判定を行っています。
前半は print() を使って、データ型をそのまま表示しています。
後半は、is ステートメントを使った判定を行っています (実際によく使う例だと思います)。
応援クリックをお願いします。励みになるので...
- 関連記事
-
-
Casio Python - for 文 2020/07/10
-
Casio Python - range() 2020/07/09
-
Casio Python - type() 2020/07/05
-
Casio Python - chr() 2020/06/30
-
Casio Python - input() 2020/06/30
-