数字と文字を使い分ける
例題
次のプログラムを実行しましょう。
(1) print(1 + 1)
(2) print(“1” + “1”)
数値型: 1
文字型: “1”
型(かた)
「1」という数字があるとき、それが数値なのか文字なのかはとても重要です。もし「1」が数値なら「1+1=2」となりますし、「1」が文字なら「1+1=11」となります。これを、その数字の型(かた)と呼びます。
Pythonでは、数字を「”」(ダブルクオーテーション)や「’」(シングルクオーテーション)で囲むと文字型、囲まないと数値型として扱うルールになっています。
確認問題
問題 Summary
0 of 3 questions completed
問題:
- 1
- 2
- 3
Information
You have already completed the 問題 before. Hence you can not start it again.
問題 is loading…
You must sign in or sign up to start the 問題.
You must first complete the following:
採点結果
採点結果
Time has elapsed
Categories
- Not categorized 0%
- 1
- 2
- 3
- 回答済み
- あとで見る
-
Question 1 of 3
1.
次のプログラムを実行しましょう。どのような現象が発生するでしょうか。また、この現象を解決するためには、どんな方法があるでしょうか。
print(“1” + 1)正解!不正解 -
Question 2 of 3
2.
次の2つのプログラムを実行しましょう。プログラムの動作を確認したら、「str」「int」について調べてみましょう。
①print(“1” + str(1))
②print(1 + int(“1”))正解!不正解 -
Question 3 of 3
3.
次の2つのプログラムを実行しましょう。プログラムの動作を確認したら、「int」と「float」の違いについて調べてみましょう。
①print(1.1 + float(“1.1”))
②print(1.1 + int(“1.1”))正解!不正解