Python Conditional Statements
Conditional Statements in Python
Conditional Statements in Python perform conditional execution of a statement or group of statements, Python checks if a conditional statement evaluates to true or false. Conditional statements are handled by if statements in Python.
What are Conditional Statements in Python ?
Python programming language provides following types of decision making or branching statements in Python . Python programming language provided decision making statements or branching statements as follows .
if statement
An if statement consists of a boolean expression followed by one or more statements .
if else statement
An if statement can be followed by an optional else statement , which executes when the boolean expression is false .
if elif else statement (nested if statement)
You can use one if elif else statements as nested if statements in other programming languages.
What is if Statement in Python ?
An if statement consists of a boolean expression followed by one or more statements. Python if statement is used for decision making operations. It contains a code which executes when the condition is true. The if statement is written by using the if keyword.
In other words the if statement is used to create conditional statements (if statements), and allows you to execute a block of code only if a condition is True.
If the condition is true, then statements block is executed. If condition is false, then statements block is skipped and not executed. Note that the colon (:) following condition is required. Some programming languages require statements block to be enclosed in parentheses. Python does not require parentheses for block of statements. Block of statements must be indented.
Python if Statement Example
# IF STATEMENT EXAMPLE x = 7 if x > 1: print(str(x) + " is greater than 1")
What is else Statement in Python ?
The else statement is used in conditional statements (if else statements), and decides what to do if the condition is False. The else statement can also be use in try except blocks. The else statement in a try except block to define what to do if no errors were raised.
Python else Statement Example
# ELSE STATEMENT EXAMPLE a = 7 if a == 1: print('One') else: print('else statement executed')
What is elif Statement in Python ?
The elif statement is used in conditional statements in Python. It is same as else if statement. Any number of elif statements can be specified. The else clause is optional.
Python elif Statement Example
# ELIF STATEMENT EXAMPLE a = 7 if a == 1: print('One') elif a == 7: print('It is Seven') else: print('Something else')
What is and Operator in Python ?
The and keyword is a logical operator. and, or, not are the logical operators in Python. Logical operators are used to combine conditional statements. The return value will only be True if both statements return True, otherwise it will return False.
Python and Operator Example
# AND OPERATOR EXAMPLE x = 7 y = 8 if x > 3 and y < 10: print("Both statements are True") else: print("At least one statement is False")
What is or Operator in Python ?
The or keyword is the logical operator in Python. The or keyword used to combine conditional statements. The or keyword will return True if any of the operands is True, otherwise it will return False.
Python or Operator Example
# OR OPERATOR EXAMPLE x = 7 if x > 1 or x < 10: print("x is greater than 1 or less than 10") else: print("Condition Fails")