Ha Khanh Nguyen (hknguyen)
if
Statement¶if
statement checks a condition that, if True
, evaluates the code in the block that follows:if x < 0:
print('It\'s negative!')
:
is needed at the if
statement, before the code block begins.:
MUST BE INDENTED.:
.if x < 0:
print('It\'s a negative')
elif x == 0:
print('Equal to zero')
elif 0 < x < 5:
print('Positive but smaller than 5')
else
Statement¶else
statement is used a catch-all block if all of the conditions above it are False
.if x < 0:
print('It\'s a negative')
elif x == 0:
print('Equal to zero')
elif 0 < x < 5:
print('Positive but smaller than 5')
else:
print('Positive and larger than or equal to 5')
True
, no further elif
or else
blocks will be reached.Exercise 1: We are given 2 numbers stored in variables a
and b
. Write a program to print the number with the larger value.
Exercise 2: We are given 2 strings s1
and s2
. If one is contained in the other, print "One is a substring of the other!". Otherwise, print "They are distinct strings!"