Conditionals - Python Programming
Python’s Conditional statements allow programs to make decisions based on different conditions.In Python, conditional statements are essential for controlling the flow of the program, helping it make logical choices depending on certain conditions. The most common conditional statements in Python are if, else, elif, and the newer match-case structure.
Syntax:
if condition: # code executed if condition is Trueelif another_condition: # executed if first is False but this is Trueelse: # executed if none of the above is TrueINDENTATION IN PYTHON:
Section titled “INDENTATION IN PYTHON:”Indentation plays a vital role in Python’s syntax, especially when writing conditional statements. Unlike other programming languages that use curly braces {} to define code blocks, Python uses indentation (whitespace) to define the scope of code.
The level of indentation tells the interpreter which statements belong to which control structure, such as if, else, and loops.
Why is indentation important?
Section titled “Why is indentation important?”• Structure Definition: Indentation in Python defines the structure of the code, especially for loops, conditionals, and function definitions.
• No Braces: Unlike other languages that use braces , Python uses indentation to indicate blocks of code.
• Syntax Requirement: Proper indentation is required for the code to execute without errors; missing or incorrect indentation will lead to an IndentationError.
Example:
age = int(input("Enter your age: "))
if age >= 18: print("You can vote.")else: print("You cannot vote.")Nested If Statements
Section titled “Nested If Statements”An if statement can also be nested inside another if statement. This is useful when multiple layers of conditions need to be checked.
num = 36print ("num = ", num)
if num % 2 == 0: if num % 3 == 0: print ("Divisible by 3 and 2")print("....execution ends....")num = 36Divisible by 3 and 2....execution ends....Short Hand If-Else
Section titled “Short Hand If-Else”- If we have only one statement to execute, we can put it on the same line as the if statement.
if a > b: print("a is greater than b")
- If you have only one statement to execute, one for if, and one for else, you can put it all on the same line:
a = 2b = 330print("A") if a > b else print("B")
This technique is known as Ternary Operators, or Conditional Expressions.
- We can also have multiple else statements on the same line:
a = 330b = 330print("A") if a > b else print("=") if a == b else print("B")
All these may look cool, but its not recommended to write code like this. As it reduces code readbility may confuse other developers!
Match-Case Statement
Section titled “Match-Case Statement”-
With the release of
Python 3.10, a pattern matching technique called match-case has been introduced, which is similar to the switch-case construct available inC/C++/Javaetc. Its basic use is to compare a variable against one or more values. -
It takes expression and compares its value to successive patterns given as one or more case blocks. Only the first pattern that matches gets executed.
-
The match-case statement provides a more readable alternative to long chains of if-elif-else conditions, especially for comparing the value of a variable against different patterns.
Syntax:
Section titled “Syntax:”match variable: case value1: # code block for value1 case value2: # code block for value2 case _: # default case (similar to 'else')Example
Section titled “Example”day = input("Enter a day: ")
match day: case "Monday": print("Start of the work week.") case "Friday": print("End of the work week.") case _: print("It's a regular day.")Enter a day: MondayStart of the work week.