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?

数値・文字列・ブール値の扱い方とよくあるエラー【Day 5】

0
Last updated at Posted at 2025-12-04

Qiita Advent Calendar 2025 のパイソニスタの一人アドカレ Day5 の記事です。

数値・文字列・ブール値の扱い方とよくあるエラー

Python の基本的なデータ型である「数値」「文字列」「ブール値」の使い方と、初心者がつまずきやすいエラーをまとめて紹介します。

数値(int / float)

a = 10      # int(整数)
b = 3.14    # float(小数)
print(a + b)

数値のよくあるエラー

文字列を数値として足してしまう

age = "20"
print(age + 5)   # エラー

原因: "20" は文字列。
対処:

print(int(age) + 5)

文字列(str)

name = "Alice"
message = "Hello, " + name
print(message)

よくあるエラー

数値と文字列を足す

print("年齢は" + 20)  # エラー

対処:

print("年齢は" + str(20))

ブール値(True / False)

条件分岐でよく使います。

is_admin = True
print(is_admin)

ブール値は内部では数値扱いもできます。

print(True + 1)  # 2

ワンポイント:型を確認する type()

x = "100"
print(type(x))  # <class 'str'>

よくある混乱まとめ

やりたいこと OK NG
数値同士の計算 10 + 5 -
文字列の結合 "A" + "B" "A" + 1
文字 → 数値 int("20") int("abc")
数値 → 文字 str(20) -

まとめ

  • 数値・文字列・ブール値の区別は超重要
  • エラーの多くは「型が違う」ことが原因
  • 困ったら type() を使って確認する
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?