39.3k views
3 votes
Write Python expressions corresponding to these statements:

2.22 Write an expression involving string s containing the last and first name of a person— separated by a blank space—that evaluates to the person’s initials. If the string contained my first and last name, the expression would evaluate to 'LP'.

2.23 The range of a list of numbers is the largest difference between any two numbers in the list. Write a Python expression that computes the range of a list of numbers lst. If the list lst is, say, [3, 7, -2, 12], the expression should evaluate to 14 (the difference between 12 and -2).

2.24 Write the relevant Python expression or statement, involving a list of numbers lst and using list operators and methods for these specifications:

(a) An expression that evaluates to the index of the middle element of lst

(b) An expression that evaluates to the middle element of lst

(c) A statement that sorts the list lst in descending order

(d) A statement that removes the first number of list lst and puts it at the end

User Norio
by
5.8k points

1 Answer

5 votes

Answer:

The code with explanatory comments are given in Python below

Step-by-step explanation:

#2.22

s = "Lionel Pogba" #string s with names having LP as initials

names = s.split() #splits the string into individual characters

initial = "" #we are initialising the initials as an empty string

for i in names:

initial += i[0] #making the initials of the string

print(initial)

#2.23

l = [3, 7, -2, 12]

range = (max(l) - min(l)) #declare and initialise variable range for the range

print(range) #output of the range

#2.24

l1 = [3, 7, 17, -2, 12]

#(a)

midIndex = int(len(l1)/2) #evaluates half the length of the list

#which corresponds to the middle index of the list

print(midIndex)

#(b)

print(l1[midIndex]) #assigns the value of the middle index of the list

#corresponding to the middle element

#(c)

l1.sort(reverse=True) #reverses the elements of the list to descending order

print(l1)

#(d)

firstValue = l1[0] #firstValue variable representing the first element

l1 = l1[1:] + [firstValue] #assigning the location to the last position

print(l1)

User Benizi
by
5.6k points