Python Practice › If-Else Programs › Program 20
Check if a number is negative
Write a Python program to check whether a number is negative and print True or False.
Input
-45, 12
Output
-45 True 12 False
Approach
- Compare with 0 using
<.
Solution
for n in [-45, 12]: is_negative = n < 0 print(n, is_negative)Output
-45 True 12 False
Line by line
Line 2
A comparison already produces a boolean, so storing it directly is the simplest form of this if-else.
Learn the concept
New to this topic? Read the lesson, then come back.