Learn Python Programming with Examples — Post#4

Python control with execute repeat operation of statements.

Ganapathy Narayanan
Operations Research Bit

--

One can write small python programs withoutControl’ 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:

  1. There must be a ‘comparison’ or ‘logical’ condition after the ‘if’ keyword;
  2. 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;
  3. All program statements that are part of the ‘if’ or ‘elif’ or ‘else’ statement are indented by four blank spaces;
  4. There must be a ‘Blank line’ to indicate to python the end of the ‘if-elif-else’ block of statements.

Syntax of ‘for’ statement

--

--