Wednesday 15 August 2018

Python if Statement

Lets write a Python code where we see how if statement is used

-----------------------------------
num = 3
if num > 0:
    print(num, "is a positive number.")
print("This is always printed.")

num = -1
if num > 0:
    print(num, "is a positive number.")
print("This is also always printed.")

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

The output of above code will be below

-----------------------------------
3 is a positive number.
This is always printed.
This is also always printed.
-----------------------------------

Explanation of the code :- Watch how we use num variable 3 and print it as positive, notice the second time positive is not printed because -1 is negative not > 0

No comments:

Post a Comment