A short python program
Question:
I am new to learning python and need to write two lines of code to get the result using comparison operators and not if blocks. Help would be greatly appreciated, thanks.
Using one of the comparison operators in Python, write a simple two-line program that takes the parameter n as input, which is an integer, and prints False if n is less than 100, and True if n is greater than or equal to 100.
Answers:
n = int(input())
print(n >= 100)
So it will print True
if n
is greater than or equal to 100
, and will print False
if not, which in this case is less than 100
. Easier to read:
n = int(input())
if n >= 100:
print(True)
else:
print(False)
or
n = int(input())
print(True if n>=100 else False)
As comparison operators return boolean value(True or False), this is explained in the value-comparisons section in the official documentation.
Taking what said before you should just print the comparison that will be True
, first taking input
and casting into an int
:
>>> number = int(input("Choose a number: "))
Choose a number: 100
>>> print(number >= 100)
True
the represented code will be 2 lines exclusing the input and the output printed:
number = int(input("Choose a number: "))
print(number >= 100)
This solution might be more readable:
number = int(input("Choose a number: "))
print(True if number >= 100 else False)
I am new to learning python and need to write two lines of code to get the result using comparison operators and not if blocks. Help would be greatly appreciated, thanks.
Using one of the comparison operators in Python, write a simple two-line program that takes the parameter n as input, which is an integer, and prints False if n is less than 100, and True if n is greater than or equal to 100.
n = int(input())
print(n >= 100)
So it will print True
if n
is greater than or equal to 100
, and will print False
if not, which in this case is less than 100
. Easier to read:
n = int(input())
if n >= 100:
print(True)
else:
print(False)
or
n = int(input())
print(True if n>=100 else False)
As comparison operators return boolean value(True or False), this is explained in the value-comparisons section in the official documentation.
Taking what said before you should just print the comparison that will be True
, first taking input
and casting into an int
:
>>> number = int(input("Choose a number: "))
Choose a number: 100
>>> print(number >= 100)
True
the represented code will be 2 lines exclusing the input and the output printed:
number = int(input("Choose a number: "))
print(number >= 100)
This solution might be more readable:
number = int(input("Choose a number: "))
print(True if number >= 100 else False)