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?

Pythonの基本構文

Last updated at Posted at 2025-04-18

Pythonの基本的な構文まとめ

Javaばかり触っていたらPythonの構文を忘れてしまったので、個人用のメモ

1. 変数の代入

x = 10
name = "Alice"
is_active = True

2. 条件分岐(if文)

x = 5

if x > 0:
    print("正の数")
elif x == 0:
    print("ゼロ")
else:
    print("負の数")

:のあとにインデント(半角スペース4つ)が必要。

elif は else if の略。

3. 繰り返し(for文)

# リストの中身をループ
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
    print(fruit)

# 数値の繰り返し
for i in range(5):  # 0〜4まで
    print(i)

4. 関数の定義(def)

def greet(name):
    print(f"Hello, {name}!")

greet("Alice")

5. リスト(配列)

numbers = [1, 2, 3, 4]
numbers.append(5)
print(numbers[0])  # インデックスでアクセス(0から始まる)

6. 辞書(連想配列)

person = {"name": "Alice", "age": 30}
print(person["name"])

# 値の追加・更新
person["age"] = 31

7. while文(条件付きループ)

count = 0
while count < 3:
    print(count)
    count += 1

8. コメント

# これは1行コメント

"""
これは
複数行の
コメント
"""

9. モジュールのインポート

import math
print(math.sqrt(16))  # 平方根:4.0

10. ファイルの読み書き

# 書き込み
with open("test.txt", "w") as f:
    f.write("Hello, world!")

# 読み込み
with open("test.txt", "r") as f:
    content = f.read()
    print(content)
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?