LoginSignup
0
0

More than 1 year has passed since last update.

型とキャスト

Posted at

型の確認

qiita.py
#  3. 型とキャスト
#  3-1 type関数: 型を調べることができる
a = 4
# print(type(a))

#  3-2 
six = 6
pi = 3.14
text = 'text'
is_false = False

#  型の確認ができる
print(type(six)) # int
print(type(pi)) # int
print(type(text)) # float : 実数のこと
print(type(is_false)) # bool

キャスト

qiita.py
#  3-3  キャスト : 型の変換ができる
#  型の確認
three = 3
print(type(three))

print('int → str に変換中')

#  str(対象)で型の変換が可能
three_str = str(three)
print(type(three_str))

print('str → int に変換中')

#  int型に変換する
three_int = int(three_str)
print(type(three_int))

#  比較演算子を用いて確認する
print('threeとthree_strの型は',three == three_str) # False
print('threeとthree_intの型は',three == three_int) # True

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