Computer Science

Predict the output of the following code fragments:

x ='apple, pear, peach, grapefruit'
y = x.split(', ')
for z in y:
    if z < 'm':
        print(str.lower(z))
    else:
        print(str.upper(z))

Python

Python Funda

42 Likes

Answer

apple
PEAR
PEACH
grapefruit

Working

x.split(', ') breaks up string x into a list of strings so y becomes ['apple', 'pear', 'peach', 'grapefruit']. The for loop iterates over this list. apple and grapefruit are less than m (since a and g comes before m) so they are converted to lowercase and printed whereas pear and peach are converted to uppercase and printed.

Answered By

17 Likes


Related Questions