Python Conditional statements

Conditional statements in python

Below are the list of control structures and statements in python

  1. if else and elif
  2. while loop
  3. for loop
  4. break, continue and pass

if else and elif statements

As long as the condition next to if is true, set of codes under if statement will be executed, else, if there is else block, code under else block will be executed.

Python course

In the above example we get a number from user as input, and check if the number is less than 10 using if condition and if it is true, we print out stating that the condition is true, else we state the opposite of condition and print it out under else statement.

And then we check if the number entered is divisible by 2 using if conditional statement and if the condition is true, we print out saying that number is divisible by 2, if not we test if the number is divisible by 3 using elif statement, if so we print out stating that number is divisible by 3, if above 2 conditions are false we print out saying that Entered number is not divisible by both 2 and 3. under else statement.

While loop

While loop will keep executing the statement under it as along as the condition associated with while is true.

Python course

In the above example we get numerical user input and then print numbers from 0 till that user input by testing the condition using while. once the condition associated with while loop is false, interpreter will exit the loop.

for loop

For-loops are typically used when the number of iterations is known before entering the loop. For-loops are the shorthand way to make loops when the number of iterations is known, as every for-loop could be written as a while-loop.

Python course

In the above example in the first for loop, we are iterating through numbers in a range from 0 to whatever the user input is, and we are printing out the values in each and every single iteration.

In the second for loop we are iterating through an array and adding all the elements in the array step by step. Array for iteration can also be a string of charcters.

Continue and Break statement

continue, will skip the current iteration of the loop when the interpreter come across it.

break, will come out of the loop permanently when the interpreter come across it.

Python course

In the above example I get singapore as user input and we iterate through each and every single character of our user input and we print them out one after another, during the iteration we have couple of if statements, under first if we have associated condition (a=='a') and under that if condition we have continue statement, which skips the current iteration and goes to next one, so we didn't print a in singapore.

Other if statement is associated with same condition, but under the condition we have break statement which will make the program to break out of the iteration once the condition associated with if statement is true, so the program printed out only till character g in Singapore.

pass statements.

pass statement mean, do nothing.

Python course