Skip to main content


Python - fun with if ... else statement


I had a lot of fun playing around with if - else statement which actually is very much responsible for the program to get up running.

As for instance... like this code here:-
1
2
3
4
5
6
7
8
9
>>> a = 2
>>> b = 6
>>> if a!=0:
 print("bla bla")
 print (b/a)

 
bla bla
3.0

Obviously you do not see "else" yet. But wait we will get to that....
So what have I done is I took two variables and cast a spell saying if "a" is not equal to 0 then do something. Till then couldn't figure out what should it be so let my place holder of my wish would be "bla bla". And dividing them as well.

Now next:-

1
2
3
4
5
6
7
>>> a = 2
>>> b = 6
>>> if a=0:
 print("bla bla")
 print (b/a)
 
SyntaxError: invalid syntax

Now I just removed the "!" i.e. "NOT SIGN" and I get a wonderful screwed up Error i.e. SyntaxError. Why? Because I remove this "!" but it has to be replaced by something like .... < , >, = etc.
Cannot be empty.

Therefore the next code:-

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
>>> a = 2
>>> b = 6
>>> if a==0:
 print("bla bla")
 print (b/a)
else:
 print("there is something")

 
there is something

As you can see the mistake has been corrected now we have a==0 which means that "a" is equal to Zero and nothing else... But as the variable has a = 2 that means its not Zero. Therefore it will follow the else statement saying "there is something".

Now we will again change the if statement from "=" to "!"

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
>>> a = 2
>>> b = 6
>>> if a!=0: #if "a" is not equal to "0"
 print("Yes 'a' has some value!")
 print (b/a)
else:
 print("'a' is blank. Shame on you 'a'....!")

 
Yes 'a' has some value!
3.0

Which in the code clearly means #if "a" is not equal to "0".
Therefore it will not choose the else statement again.

Moving on and changing back from "!" to "=" lets see what it says :-

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
>>> a = 2
>>> b = 6
>>> if a==0: #if "a" is not equal to "0"
 print("Yes 'a' has some value!")
 print (b/a)
else:
 print("'a' is blank. Shame on you 'a'....!")

 
'a' is blank. Shame on you 'a'....!

Now it will choose the else statement. Because we know "a" has a value. "if" says it does not have. "if" is lying. Therefore liar liar burns on fire.

Ok... what just happened???

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
>>> a = 0
>>> b = 6
>>> if a==0: #if "a" is not equal to "0"
 print("Yes 'a' has some value!")
 print (b/a)
else:
 print("'a' is blank. Shame on you 'a'....!")

 
Yes 'a' has some value!
Traceback (most recent call last):
  File "<pyshell#56>", line 3, in <module>
    print (b/a)
ZeroDivisionError: division by zero

When I made a = 0 it gave me an Error... Why??? Lets test it out.

Testing:-

1
2
3
4
5
>>> 6/0
Traceback (most recent call last):
  File "<pyshell#57>", line 1, in <module>
    6/0
ZeroDivisionError: division by zero

OKKKKKKKKKK 6 / 0 not working out... So, lets crack this up.

There you go Cracked it:-

1
2
3
4
5
6
7
>>> try:
 6/0
except:
 print ("get me some other number")

 
get me some other number

We can over come any obsticles only if we try and except what ever comes with it. So no Error. Problem solved.

Now... combining all in one code:-

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
>>> a = 0
>>> b = 6
>>> if a==0: #if "a" is not equal to "0"
 print("Yes 'a' has some value!")
 try:
  print (b/a)
 except:
  print("get me some other number")
else:
 print("'a' is blank. Shame on you 'a'....!")

 
Yes 'a' has some value!
get me some other number
>>> 

There you go. Bit by bit. Piece by piece you can get your code up and running in no time. Now it will not give our any error because its solved by "try" section. Giving out clean output.

NOW SOME BONUS CODE TIPS:- (Try this yourself)

1
2
3
4
>>> a,b = 3,2
>>> a,b = a+b,a-b
>>> print(a,b)
5 1

Maximizing it:-

1
2
3
4
>>> a,b,c,d = 4,3,2,1
>>> a,b,c,d = a+b+c+d,a-b-c-d,a*b*c*d,a/b/c/d
>>> print(a,b,c,d)
10 -2 24 0.6666666666666666

Hope you like the tutorial.


Python is simple and fun to play with and yet very powerful. 🔥


🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍

Popular Posts

Python underline string, Python underline text, Underline python print

Python pip - Installing modules from IDLE (Pyton GUI) for python 3.7

Top 40 Python - String Processing in Python | Working with String in Python

Python Program - When was I born? / Date of Birth / MY BIRTHDAY (using Python3+)

Top 11 Essential Python Tips and Tricks





Subscribe to our Channel


Follow us on Facebook Page

Join our python facebook groups



Join us on Telegram