Problem Part A

Write a single-line Python code that reads a string containing comma-separated first-name, last-name, and the city in which a person lives from the Python interpreter command line, and simultaneously, in the same line of Python code, removes all white-space characters from the input string, and converts all letters of the input variables to lower-case, and converts the string to a tuple and saves in a tuple (first,last,city), such that, for example,

Enter the first name, last name, and the city of the person (comma-separated): Amir, Shahmoradi, Austin

would display,

(first,last,city)
('amir', 'shahmoradi', 'austin')

Hint:
Use the input function for this purpose. The output of input is a string, which can be manipulated repeatedly on the same line, using multiple string methods that you learned about in the previous lectures.

Problem Part B

The one-line if-expression syntax does not provide a functionality like elif keyword as in the if-statement syntax. Our goal here is to learn how to convert a Python if-statement containing elif to a one-line Python expression. Convert the following if-block to a single line if-expression. Modify the if-block inside the following function to one-line if-expression,

def dummy(i):
    if i==0:
        j=0
    elif i==1:
        j=1
    elif i==2:
        j=2
    else: j = 'j is not in [0,1,2]' 
    return j

Solution Part A

(first,last,univ) = ((input('Enter the first name, last name, and the city of the person (comma-separated): ').replace(' ','')).lower()).split(',')

Solution Part B

def dummy(i):
    j = 0 if i==0 else (1 if i==1 else (2 if i==2 else 'j is not in [0,1,2]') )
    return j

Comments