The answer is B:
if 4 > 4:
print('A')
elif 4 == 4:
print('B')
elif 4 <= 4:
print('C')
B
The condition 4 > 4
evaluates to False
, while the conditions 4 == 4
and 4 <= 4
both evaluate to True
.
However because in our code the program would meet elif 4 == 4
first, it would execute the body contained in that branch.
A program can only follow one branch in a conditional statement.
A conditional statement that will perform this task is as follows:
a = 5.0
b = 5.1
if abs(b - a) < 0.1 * abs(a):
#abs(a-b) is the absolute difference of a and b, and we want this to be less than 10% of a,
#which we can obtain via abs(a)/10, or 0.1*abs(a)
print('True')
else:
print('False')
True
If you may have a different order of a
and b
from you neighbour then you may see different results with certain pairs of values.
The plan is for us to store all the vowels in one string vowels
.
Then when we are given another string sentence
, we loop over the characters in sentence
to see if they are in vowels
, and thus are a vowel.
vowels = 'aeiouAEIOU'
sentence = 'Mary had a little lamb.'
count = 0 #this is how many vowels we found
for character in sentence:
if character in vowels:
#if we get to here, this character of sentence is also in vowels, so it's a vowel
count += 1
#we don't need an else, because we don't need to count the number of non-vowels
print("The number of vowels in this string is " + str(count))
The number of vowels in this string is 6
You can see the results below. You should notice that the statements are arranged into pairs, and they display a familiar pattern:
''
is considered False
True
[]
is considered False
True
0
is considered False
int
is considered True
if '':
print('empty string is true')
if 'word':
print('word is true')
if []:
print('empty list is true')
if [1, 2, 3]:
print('non-empty list is true')
if 0:
print('zero is true')
if 1:
print('one is true')
word is true non-empty list is true one is true