if statements in Python - making programs smarter

filler(58)
Published in
#python
Words
230
Reading
2 min
7y
Listen to this post
~2 min

What is an 'if'?

It is a condition.

If I am thirsty
I drink water

If I am moody
I listen to music

These are some examples that show us how and where an 'if' works in normal languages.

The above specified us doing certain things if the condition is true. What if the condition was false, then we move on to the next one that is true.

For example :

I am at a snack bar
if I want a small bite
I order a samosa
otherwise if I want a mini lunch
I order vada paav
otherwise I order bhelpuri

(samosa, vada paav and bhel puri are north Indian snacks)

Now look closely at the screenshots which show examples of True and false

True

Capture.JPG

False

false.JPG
Nothing is printed because the statement is false.

Else statement

Else just means otherwise

What if we used the else statement?
See what happens

else.JPG

What if we use an 'or' in between to specify one or more conditions? we need to just write the 'or' condition. Work it out with many examples to understand it. it really is easy to grasp.

if true.JPG

and what if we write the statements are false

either or.JPG

and

The next condition is 'and'

Look at the screenshots below to understand and play with the code

and true.JPG

and false

and false.JPG

All images are screenshots of work done on colab.