Learn Python Programming with Examples — Post#4
Python control with execute repeat operation of statements.
One can write small python programs without ‘Control’ and ‘For’ repeat operation of statements. However, many algorithms designed for ‘automation’ of programs needs a certain type of ‘control’ using the ‘if-elif-else’ statement and ‘repeat’ operations using the ‘for’ or the ‘while’ statement. Read below few simple examples of using these three statements in Python, and then one more example of an application program using these statements.
Syntax of ‘if-elif-else’ statement
The syntax of using the Python keywords is very important for the Python kernel to produce the desired operation in a program. The following syntax is expected for using the ‘if-elif-else’ keyword in a python program:
- There must be a ‘comparison’ or ‘logical’ condition after the ‘if’ keyword;
- There must be a ‘Colon’ (‘:’) symbol at the end of the ‘if’ statement after the ‘comparison’ or ‘logical’ condition; in addition, there must be a ‘colon’ (‘:’) symbol after the ‘elif’ and the ‘else’ keywords;
- All program statements that are part of the ‘if’ or ‘elif’ or ‘else’ statement are indented by four blank spaces;
- There must be a ‘Blank line’ to indicate to python the end of the ‘if-elif-else’ block of statements.