if statements in Python - making programs smarter

avatar

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.



0
0
0.000
1 comments
avatar

Congratulations @filler! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

You published more than 60 posts. Your next target is to reach 70 posts.

You can view your badges on your Steem Board and compare to others on the Steem Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

To support your work, I also upvoted your post!

Do not miss the last post from @steemitboard:

SteemFest Meet The Stemians Contest - The mysterious rule revealed
SteemFest⁴ - Meet the Steemians Contest
Vote for @Steemitboard as a witness to get one more award and increased upvotes!
0
0
0.000