Check if a number is negative

EasyIf-Else Programs · 20 of 64

Write a Python program to check whether a number is negative and print True or False.

Advertisement
Input
-45, 12
Output
-45 True
12 False

Approach

  1. Compare with 0 using <.

Solution

Python
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.

More if-else programs

← All 64 If-Else Programs

Leave a Comment