Python: if else decision making
In python Decision making codes are like any other language but addition to that, we have quit easy syntax which make it fun writing python codes and make it easy to troubleshoot as well for anyone working or upgrading codes after encounter some issues in same.
we just need to care few things as we already discussed once in syntax post for same, let’s see once bit more about python with some conditions making codes.
Basic type of decision making code in Python
- Simple if statement
- if else statement
- if elif statement
- Nested if statement
Simple if statement
This is simple if statement which make things possible for simple one decision through if condition, that could do deploy series of statements if condition evaluate.
>>> a=1 >>> if isinstance(a,int): ... print a, " is integer vaue" ... 1 is integer vaue
So in above code we can see two important things, that need to understand.
- Every code statement after conditions should have proper indentation
- colon (:) after Every statement header statement like after if, else, elif
That rule will work throughout Python codes.
If else statement
These statements has provision to have one condition with one more alternative in case condition is not evaluate.
>>> a 8 >>> b 20 >>> if a > b: ... print "a is bigger than b" ... else: ... print "a is smaller than b" ... a is smaller than b
As we wrote above code we could also used complex if,else statement , which has some more conditions used to define decision in python code, let see one complex if-else.
if elif statement
Through this way we can write multiple condition and wait for evaluation of any of them which later follow below statements. One thing more once any conditions evaluate all other conditions of same if will ignored or skiped
>>> def number(num): ... if not isinstance(num,int): ... print num, "is not integer" ... elif num < 0: ... print num, "is negative number" ... elif num == 0: ... print num, "is zero" ... elif num > 0: ... print num, "is positive number" ... else: ... print "Get Out" ... >>> number("yahoo") yahoo is not integer >>> number(67) 67 is positive number >>> number(-11) -11 is negative number >>> number(0) 0 is zero >>>
In above code we tried to write function that has complex if else statement.then further tried to call it with argument and functions accordingly very well.
Nested if statement
we can also write if statement within another if statement code block that make things possible for more complex problems.see below example
>>> def number(num): ... if num < 0: ... print num, "is negative number" ... elif num == 0: ... print num, "is zero" ... elif num > 0: ... if not isinstance(num,int) and not isinstance(num,float): ... print num," is not integer or float" ... elif isinstance(num,str): ... print num," is String" ... else: ... print num," is integer or float and greater than zero" ... else: ... print "Get Out" ... >>> number("qjjqj") qjjqj is not integer or float >>> number(8789) 8789 is integer or float and greater than zero >>> number(0) 0 is zero >>> number(-12) -12 is negative number
In above example we saw there is nested if statement inside another previous if statement, which try to compute something. These make things more possible and feasible for complex problems.
Leave a Reply