Search this site
Embedded Files
AIMD GPDS Courses
  • Home
  • Courses
  • Contact
AIMD GPDS Courses
  • Home
  • Courses
  • Contact
  • More
    • Home
    • Courses
    • Contact

日本語  ❯

Lesson 2    ❮    Lesson List    ❮    Top Page

2.1 for Loop

2.2 List Comprehension

2.3 Boolean Operators

❯  2.4  if Statement

2.5 Looping if Statement   

⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺
EXPECTED COMPLETION TIME
❲▹❳  Video   6m 59s
☷  Interactive readings   5m

Writing a Simple if Statements

An if statement is written by using the if keyword. Just like for, indentation and colon (:) is important here.

Making Conditional Statements with else

Sometimes we want to declare an action when the condition holds, and something else when it does not hold. You can use if-else syntax in this case. 

Chaining Conditions with if-elif-else

Many real-world situations have more than two possible conditions. In this case, this if-elif-else chain can be used.

In a long if chains, it is useful to put the conditions into variables.

WRITE BETTER CODE
Writing Conditions Neatly

The main focus in if statement is the condition. 

  • Don't compare boolean values to True or False using ==.

  • Split long conditions into few lines.

LEARN FROM ERRORS
SyntaxError when else has a Condition

else does not need a condition. If you want to use a second condition, use elif instead. Replace else with elif to fix this error.

©2023. All rights reserved.  Samy Baladram,
Graduate Program in Data Science - GSIS - Tohoku University
Google Sites
Report abuse
Page details
Page updated
Google Sites
Report abuse