Conditional statements

Conditional statements

Conditional statements help us run a block of code only if a specific condition is satisfied. We use if keyword for the same. If comparison operator returns a True then the block of code will run or else it will be skipped.

--------------------------------------------------

code:-

if 5 < 7:

     print('yes five is less than seven')

output:-

yes five is less than seven

----------------------------------------------------

Here 5 < 7 returned a true and so the code was executed.

*** Note that before print statement there are indentations that tell python that it is a part of that block code.***

We use else keyword to declare a block of code that will be executed if the previous if condition is not satisfied.

--------------------------------------------------

code:-

age = int(input("enter your age: "))

if age >= 18:

     print('yeah!!! You can drive.')

else:

     print("Sorry but you are too young to drive")

output:-

enter your age: 15

Sorry but you are too young to drive.

----------------------------------------------------


Comments

Post a Comment

Popular Posts