payment by upi: sinhamit@icici or payment by bank account name: amit kumar sinha, account number: 2646728782 IFSC code: KKBK0005660 SWIFT: KKBKINBB

Please support if you like my work by payment through upi: sinhamit@icici or payment by bank

account name: Amit Kumar Sinha,
account number: 2646728782
IFSC code: KKBK0005660
SWIFT: KKBKINBB


Conditional Blocks   in Category: Python   by amit

🕙 Posted on 2023-07-03 at 20:21:56     Read in Hindi ...


Control Structures

    Control structures are block of codes, which are used to perform specific tasks. Condit­ional statements, Loops, Functions and Classes are control structures. Till now, in Python tutorials, indent­ation is not used. However, writing control structures, you must be careful regarding indent­ation, and maintain levels of indent­ation in block of codes NESTED inside another block of code.

Conditional Statements

    There are three types of condit­ional statements − (1) if block, (2) if else block, and (3) if elif else block, which you can use in Python programs. All three are important, and syntax of these are described as follows.

    In following three examples, you can see different types of condit­ional statements (control structures), which will perform certain task (that is, execute some block of codes) when the condit­ional expre­ssion is True. In these three examples, only literal values of boolean data-type is provided to explain them in simple way.

    You can write as many independent if condit­ional block, which may be necessary in certain situations. Each if will check the condit­ional expre­ssion after it, and when it is evaluated as True, then only, block of codes below it, within indent­ation shall be executed. The indent­ation may be of 1, 2, 3, or 4 blank (white-) spaces.

if True:
  print( "Condition is True, and block will be executed." )

    In above code two indent­ation spaces have been provided in second line. Similarly, in following examples, after every condit­ional expre­ssion checking, that is, after the : (colon) symbol, two indent­ation spaces are provided in next line.

    The if else block of code will execute only one set of statements after either of : (colon) symbol. That is, when the condit­ional expre­ssion is True, the first set of statements (below if) will be executed, and when the condit­ional expre­ssion is False, the second set of statements (below else) will be executed.

    In following example, you can see that both if and else keywords are without indent­ation, and codes below them are specif­ically written after two blank (white-) spaces. You have to provide same level of indent­ation for each of blocks, and you should not mismatch the indent­ation (blank spaces), otherwise the Indentat­ionError may be displayed.

if False:
  print( "Condition is False, this statement will not be executed." )
else:
  print( "Statement below the else will be executed, when condition is False." )

    In PHP and JavaScript, to check many conditions, if elseif else or if else if else (a blank space between else if) condit­ional statements are used. But in Python, you should be careful, and only use if elif else control structure. elif is a reserved keyword and not short (or abbre­viated) form of else if or elseif. The function­ality of these control structures are same in all three program­ming languages, and other C based program­ming languages.

    In the following example, only one set of statements (inside either condit­ional block) will be executed. Herein below example, only one statement is written (to simply describe that how following example works,) in either if, elif, and else block, though as many statements can be written after the : (colon) symbol (from next line) within indent­ation.

    You can add as many elif conditions in between if and else blocks, but only one set of statements will be executed, when either any one condition is evaluated as True, or all conditions are evaluated as False, then the last set of statements after else block will be executed. You need not to provide the else block, and thence, when all conditions are evaluted as False, nothing in the if elif block will be executed.

if False:
  print( "Condition is False, this statement will not be executed." )
elif True:
  print( "Statement below elif will be executed, when if condition is False, and elif condition is True." )
else:
  print( "Statement below the else will be executed, when above two conditions are False." )

    In the following example, you can see that all three statements before the if elif else block of codes, are without indentation. In these three statements, that is condition1 = False, condition2 = True, and condition3 = False, three variable names have been assigned with boolean literals. These statements can be condit­ional expre­ssions, for example, 5 < 4, which is evaluated as False.

condition1 = False
condition2 = True
condition3 = True

# In the following if-elif-else block of codes,
# different indent­ation levels are color coded.
# Two blank spaces are colored yellow, and
# Four blank spaces are colored green.


if condition1:
  # block of codes to be executed.
  print("Hello from if condition.")
  # NESTED CONDIT­IONAL STATEMENTS MUST HAVE INDENT­ATION.
  if condition3:
    # second level of indentation
    print("Hello from Nested if condition")
elif condition2:
  # block of codes to be executed.
  print("Hello from elif condition.")
else:
  # block of codes to be executed.
  print("Hello from else (when all conditions are False).")

    With the help of these if, if else, and if elif else blocks, you can create complex CLI Application, or WEB-Development App, etc. In the advanced section, you will learn to build sophist­icated Astrology APP, which can be much more complex, than you have been learning in Basic Python Skills. In the following example, you will learn, how to determine whether a year is a leap year or not.

    There is complex calculation, as shown below unlike in school, we have been taught that any year which is divisible by 4, is a leap year. This is how you find out that if a particular year is a leap year:
(1) on every year that is evenly divisible by 4, but
(2) that year is not evenly divisible by 100,
(3) and when that year is evenly divisible by 100, is also evenly divisible by 400.

    In the following three examples, you can also see that different indent­ation levels are color-coded, that is two blank spaces are colored yellow, four blank spaces are colored green, and six blank spaces are colored cyan. To write above exercise that is, to determine a given number as leap year, you should first write following if else block of codes.

if year % 4 == 0:
  # When NESTED if condition (added below) is evaluated as false, then
  # year is leap year, otherwise another NESTED if else will be needed.
else:
  # When (year % 4 != 0) then following statement will be executed.
  print(year, " is not a leap year.")

    Then, insert the first NESTED if else block of codes as follows.

if year % 4 == 0:
  if year % 100 == 0:
    # When (year % 100 == 0) is evaluated as True, then
    # another NESTED if else (added below) will be evaluated.
  else:
    print(year, " is a leap year.")
else:
  print(year, " is not a leap year.")

    After that, you can insert another NESTED if else block of codes as shown in below (complete) example.

year = int( input( "Enter the year to check for leap year: " ) )
if year % 4 == 0:
  if year % 100 == 0:
    if year % 400 == 0:
      print(year, " is a leap year.")
    else:
      print(year, " is not a leap year.")
  else:
    print(year, " is a leap year.")
else:
  print(year, " is not a leap year.")

    Some years are shown below to explain you the concept of LEAP Year. When the first condition (image as a person, for example) is evaluated as True, then the year provided by the user will be a Leap Year, only when the NESTED condition (person's child) is evaluated as False. When the NESTED condition (child) is evaluated as False, then the another NESTED condition (person's grand-child) shall not be evaluated. But when the first NESTED condition (child) is evaluated as True, then that year will not be a Leap Year, except when the NESTED condition (grand-child) is also evaluated as True. When all three conditions are evaluated as True, then that year will be a Leap Year.
(2000%4==0) and (2000%100==0) and (2000%400==0) is a LEAP YEAR.
(2100%4==0) and (2100%100==0) and (2100%400==100) is NOT LEAP YEAR.
(1900%4==0) and (1900%100==0) and (1900%400==300) is NOT LEAP YEAR.
(2400%4==0) and (2400%100==0) and (2400%400==0) is a LEAP YEAR.
(2024%4==0) and (2024%100==24) and (2024%400 not necessary) is a LEAP YEAR.


Leave a Comment: