「Pythonの基礎 - 条件分岐」の版間の差分

提供:MochiuWiki - SUSE, Electronic Circuit, PCB
ナビゲーションに移動 検索に移動
(Wiki がページ「条件分岐(Python)」を「Pythonの基礎 - 条件分岐」に、リダイレクトを残さずに移動しました)
(文字列「<source lang="python">」を「<syntaxhighlight lang="python">」に置換)
7行目: 7行目:
== if文とif-else文の条件分岐 ==
== if文とif-else文の条件分岐 ==
条件式Aが成立するなら実行コードAが実行され、成立しないならば実行コードBが実行される。<br>
条件式Aが成立するなら実行コードAが実行され、成立しないならば実行コードBが実行される。<br>
  <source lang="python">
  <syntaxhighlight lang="python">
  if 条件式A:
  if 条件式A:
     実行コードA
     実行コードA
14行目: 14行目:
  </source>
  </source>
<br>
<br>
  <source lang="python">
  <syntaxhighlight lang="python">
  hungry = True
  hungry = True
  if hungry:
  if hungry:
33行目: 33行目:
<br>
<br>
<u>'''Pythonには他のプログラミング言語にあるswitch文が存在しない。Pythonでは、switch文と同じような処理をelif文を使用する。'''</u><br>
<u>'''Pythonには他のプログラミング言語にあるswitch文が存在しない。Pythonでは、switch文と同じような処理をelif文を使用する。'''</u><br>
  <source lang="python">
  <syntaxhighlight lang="python">
  if 条件式A:
  if 条件式A:
     実行コードA
     実行コードA
42行目: 42行目:
  </source>
  </source>
<br>
<br>
  <source lang="python">
  <syntaxhighlight lang="python">
  code = "Swift"
  code = "Swift"
  if code == "Python":
  if code == "Python":
61行目: 61行目:
条件の変数を2つ用意(複数条件)して、if文を入れ子構造で記述することができる。<br>
条件の変数を2つ用意(複数条件)して、if文を入れ子構造で記述することができる。<br>
<u>'''入れ子のインデントの位置を間違えると、ブロックと認識されないので注意すること。'''</u><br>
<u>'''入れ子のインデントの位置を間違えると、ブロックと認識されないので注意すること。'''</u><br>
  <source lang="python">
  <syntaxhighlight lang="python">
  code = "Swift"
  code = "Swift"
   
   

2021年11月24日 (水) 17:50時点における版

概要

ここでは、Pythonにおけるif文を使用した条件分岐について記載する。

条件式をコロンで終了し、ブロック部分は半角スペース4つ分のインデントで字下げして記述するという特徴がある。


if文とif-else文の条件分岐

条件式Aが成立するなら実行コードAが実行され、成立しないならば実行コードBが実行される。

<syntaxhighlight lang="python">
if 条件式A:
    実行コードA
else:
    実行コードB
</source>


<syntaxhighlight lang="python">
hungry = True
if hungry:
    print('I want to eat something!')
else:
    print('I am full.')

# 出力
I want to eat something!
</source>



elif文を使用した条件分岐

C/C++/C#等ではelse ifだが、Pythonではelifと記述する。

条件式Aが成立すれば実行コードA、成立していなければ条件式Bが検証され成立していれば実行コードB、いずれの条件も成立していなければ実行コードCが実行される。
検証したい条件が更に必要であれば、elif文を必要な分だけ記述する。

Pythonには他のプログラミング言語にあるswitch文が存在しない。Pythonでは、switch文と同じような処理をelif文を使用する。

<syntaxhighlight lang="python">
if 条件式A:
    実行コードA
elif 条件式B:
    実行コードB
else:
    実行コードC
</source>


<syntaxhighlight lang="python">
code = "Swift"
if code == "Python":
    print('I want to learn Django!')
elif code == "PHP":
    print('I want to learn WordPress!')
elif code == "Swift":
    print('I want to learn how to make iPhone apps.')
else:
    print('I\'m going out for a walk.')

# 出力
I want to learn how to make iPhone apps.
</source>



if文の入れ子構造

条件の変数を2つ用意(複数条件)して、if文を入れ子構造で記述することができる。
入れ子のインデントの位置を間違えると、ブロックと認識されないので注意すること。

<syntaxhighlight lang="python">
code = "Swift"

if code == "Python":
    print('I want to learn Django!')
else:
    if code == "PHP":
        print('I want to learn WordPress!')
    else:
        if code == "Swift":
            print('I want to learn how to make iPhone apps.')
        else:
            print('I\'m going out for a walk.')

# 出力
I want to learn how to make iPhone apps.
</source>