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.
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 if statement in Python:
Æ’ python command
luggage_weight = float(input('Enter
your luggage weight: '))
if luggage_weight <= 100:
if luggage_weight <= 50:
cost = 5
else:
cost = 5 + (luggage_weight - 50)/10
print("Your luggage will cost $ %d." % cost)
else:
print("Maximum luggage weight exceeded.")
√ output
Enter your luggage weight: 33
Your luggage will cost $ 5.
Enter your luggage weight: 77
Your luggage will cost $ 7.
Enter your luggage weight: 111
Maximum luggage weight exceeded
Sign up here with your email

ConversionConversion EmoticonEmoticon