基礎練習問題② 型と文字列 問題23 答え

問題23:3、3.0、"3"、True の4つの値について、それぞれの型を出力するプログラムを作成してください。

出力例

<class 'int'>
<class 'float'>
<class 'str'>
<class 'bool'>
スポンサーリンク

問題23:答え

print(type(3))
print(type(3.0))
print(type("3"))
print(type(True))

解説

type() を使うと、値の型がわかります。

  • int:整数
  • float:小数
  • str:文字列
  • bool:True か False のどちらか

同じ「3」でも、書き方によって型が変わるのがポイントです。計算がおかしいときは、まず type() で確かめてみましょう!


次は問題24です! 問題の一覧に戻って、問題24に挑戦する

error: Content is protected !!
タイトルとURLをコピーしました