Control flow in python:-Python supports various decision making contrl structures like If-elif-else, While /For Loops.
The colon character is used to delimit the beginning of a block of code and python uses concept of indented block to group block statements. In other words, python relies on the indentation
level to designate the extent of that block of code, or any nested blocks of code within. It is common for all control structures in python along with body of function and class.
What is indented block ? : Below code lines print("I am Indented for if a==2") and print("All the best") are after a tab space(highlighted in yellow) with respect to if a==2 so it is termed as indented, however print("I am not Indented for if a==2") is at same level with respect to if a==2
Conditional statement - if/elif:- Write a sample code to check whether input number is divisible by 2/divisible by 5 or some other case.
Here pay attention to colon after each conditional statement and indentation after each of that.It is beauty of python which abstain developer from putting opening and closing braces(and avoid verbosity).
While loop: Write a sample code to print 1 to 10. Here J is initialized to 1 and on each iteration j is compared with 1, if it is less it prints value of j and increment j.
For loop:- Python's For loop is mainly used for iterating through series of elements like List, set, dict, etc. Consider the syntax of for loop for the time being, we will use For loop in detail when we learn these data structures.
Python also support Break and Continue Statements. As all programming suggest to use these two control structures sparingly.
Python does not support switch/case as found in other languages. However we can simulate the same using dictionary and separate method call
Sample example using control statements:1. Convert decimal to Binary.
2. Find factorial of a number
3. Check even and odd number, read input from stdin
Previous : Python Data types Next: Python data structure