Sitemap

Conditional Statements in Python

3 min readApr 17, 2022

--

https://www.google.com

Decision making is as important as life in any programming language. Decision making in programming languages ​​is automated using conditional statements. Conditional statements evaluate Python to see if it meets the specified conditions. The condition is evaluated and processed as true or false. If this is the case, the program will run as needed. If the condition turns out to be false, the statement following the If condition is executed.

Decision making statements are also called as Conditional Statements.

Get Syed Mohd Nazar Ali’s stories in your inbox

Join Medium for free to get updates from this writer.

In Python there are Six different conditional statements used for Decision making.

  1. if
  2. if-elif
  3. if-else
  4. if-elif-else
  5. nested-if
  • if statement : if statement is most important for decision making in python , in which first if statement (condition) is executes and if statement is True then block statement is executes , if statement (condition) is False then block statement is not executes.

Statement preceding block generally ends with a colon(:)

if expression:
Statement
  • if-elif statement : Creating an elif statement is identical to an if statement, with one difference, you use the elif keyword instead. You’re able to have multiple conditions for each elif as well. All elif statements are connected to one original if statement, so that once a single conditional is True, the rest do not run. Let’s try it:
Press enter or click to view image in full size
Untitled3 — Jupyter Notebook
  • if-else statement: Like an elif statement, the else clause needs to always be associated with an original if statement. The else clause covers all other possibilities, so you don’t need to write a condition at all; you just need to provide the keyword “else” followed by an ending colon. Remember that an else clause will run the code inside of it if the program reaches the statement. Try the following:
Press enter or click to view image in full size
  • Nested -if : we can have a if..elif..else…statement inside another statment if..elif..else…statement . In Nested If statements, the indentation (whitespace at the beginning) to determine the scope of each statement should take precedence.
Press enter or click to view image in full size
Press enter or click to view image in full size
Untitled3 — Jupyter Notebook

--

--

Syed Mohd Nazar Ali
Syed Mohd Nazar Ali

Written by Syed Mohd Nazar Ali

Data Analyst -Data Science| Machine Learning Enthusiast | Statistics , Masters in Computer Science | Aligarh Muslim University.