0
0

Python 構文編

Posted at

Python 構文編

Pythonの構文について解説します。

目次

  1. 文(Statement)
  2. 式(Expression)
  3. リテラル(Literal)
  4. 条件分岐(Conditional Statements)
  5. ループ(Loop)
  6. 再帰(Recursion)

1. 文(Statement)
文は、Pythonコードの最小の実行単位です。文は通常、一つのアクションを表します。

Statement.py
x = 10  # これは代入文です
print(x)  # これは関数呼び出し文です

2. 式(Expression)
式は、値を生成するコードの一部です。例えば、計算や関数呼び出しが含まれます。

Expression.py
y = 3 + 5  # 3 + 5 は式です
print(y)  # Output: 8

3. リテラル(Literal)
リテラルは、ソースコード中に直接記述された値です。

Literal.py
num = 42  # 数値リテラル
text = "Hello"  # 文字列リテラル

4. 条件分岐(Conditional Statements)
条件分岐は、条件に基づいて異なるコードブロックを実行するための構文です。

Conditional Statements.py
x = 10
if x > 5:
    print("x is greater than 5")
else:
    print("x is 5 or less")
# Output: x is greater than 5

5. ループ(Loop)
ループは、コードブロックを繰り返し実行するための構文です。

Loop.py
# for ループ
for i in range(5):
    print(i)
# Output: 0 1 2 3 4

# while ループ
count = 0
while count < 5:
    print(count)
    count += 1
# Output: 0 1 2 3 4

6. 再帰(Recursion)
再帰は、関数が自分自身を呼び出すことです。

Recursion.py
def factorial(n):
    if n == 1:
        return 1
    else:
        return n * factorial(n - 1)

print(factorial(5))  # Output: 120

最後までお読みいただきありがとうございました。

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