In the heart of programming logic, we have the if statement in Python.
The if statement is a conditional that, when it is satisfied, activates some part of code. Often partnered with the if statement are else if and else.
The if statement is a conditional that, when it is satisfied, activates some part of code. Often partnered with the if statement are else if and else.
However, Python's else if is shortened into elif. If statements are generally coupled with variables to
produce more dynamic content.
The best way to learn new things is to take a
practical approach of the things you want to learn. Here is a fragment of code that demonstrates the use of the if statement in Python:
Æ’ python command
temperature = float(input('What is
the temperature now: '))
if temperature > 25:
print('Wear shorts.')
else:
print('Wear long pants.')
print('Get some exercise outside.')
√ output
What is the temperature now: 30
Wear shorts.
Get some exercise outside.
Sign up here with your email

ConversionConversion EmoticonEmoticon