Question

In: Computer Science

python 3 countries = ['mexico,'eseft,'ry,',ft,'us'] Write a statement that sorts the countries alphabetically and another statement...

python 3

countries = ['mexico,'eseft,'ry,',ft,'us']

Write a statement that sorts the countries alphabetically and another statement that prints the last country in the sorted nations list.

Solutions

Expert Solution

Answer:

To sort the given list in the alphabetically order there is the pre- defined function in the python 3 that is the sorted function which automatically sorts the list in the alphabetically order.

To print the last country of the sorted list there is 2 methods:

1. The first method is to directly pass the index of the last element of the list in the print statement.( The list will always start with the index 00.

2. The second method to find out with the help of the length of the list in the print statement.The length of the list can be find out by the pre-defined len function.

Here is the code of the program:

countries=['mexico','eseft','ry','ft','us']

#statement that prints the countries in the alphabetically order by the predefined function sorted.

sorted_countries=sorted(countries)

print("Sorted list :",sorted_countries)

# statement that prints the last country in the sorted list

print("By Index method:",sorted_countries[4]) # index will be 4 because the index of the string is started from 0

# Another method by which we can print the last country in the sorted list

length=len(countries)

# here the 1 is subtracted from the length because the index is started from 0

print("By length method:",sorted_countries[length-1])

* Please refer to the screenshot of the code for proper indentation:

Here is the screenshot of the code:

Here is the output of the code:


Related Solutions

Two countries: US and Mexico. Two goods: Airplane and car. A US worker's MPL is 20...
Two countries: US and Mexico. Two goods: Airplane and car. A US worker's MPL is 20 times higher than a Mexican worker in making an airplane. A US worker's MPL is 5 times higher than a Mexican worker in making a car. Which country has absolute advantage in making cars and why? Which country has comparative advantage in making cars and why?
Python 3 Function which takes the head Node of a linked list and sorts the list...
Python 3 Function which takes the head Node of a linked list and sorts the list into non-descending order. PARAM: head_node The head of the linked list RETURNS: The node at the head of the sorted linked list. ''' def sort(head_node): #Code goes here ''' Test code goes here '' ' if __name__ == '__main__':
3. The labor supply and demand equations in Mexico and the US are Ndmex = 140...
3. The labor supply and demand equations in Mexico and the US are Ndmex = 140 – 2 Wmex and Nsmex = 80 NdUS = 600 – 4 WUS and NsUS = 260 (Notice: To make the exercise simple, we are assuming that the labor supply curves are perfectly vertical at 80 in México and at 260 in the US). where Ndmex and NdUS are the number of workers demanded in Mexico and the US (in millions of workers). Wmex...
use python 1. Write a program that a. defines a list of countries that are members...
use python 1. Write a program that a. defines a list of countries that are members of BRICS (Brazil, Russia, India, China, Sri Lanka) b. Check whether a country is a member of BRICS or not Program run Enter the name of country: Pakistan Pakistan is not a member of BRICS Enter the name of country : India India is a member of BRICS 2. Write a program to create a list of numbers in the range of 1 to...
IN PYTHON: Write a program to study countries and their capitals. The program will store country:capital...
IN PYTHON: Write a program to study countries and their capitals. The program will store country:capital information and allow the user to quiz themselves. These two functions are required for this assignment. You may add other functions that you deem appropriate: - menu()   This function, which displays all the user options to the screen, prompts the user for their choice and returns their choice. This function will verify user input and ALWAYS return a valid choice. - addCapital(dict) This function...
for Python 3 Write a python program that Creates a list that has these values in...
for Python 3 Write a python program that Creates a list that has these values in this order, 'Python', 'JavaScript', and 'PHP' Define a displayMyClasses function that sorts the list and then displays each item on the list, one per line, in sorted order. Also, number the displayed list as shown in the "Running a Sample Program" shown below. Define a guessNext function that selects a random element from the list and returns it to the call of the function....
PYTHON Program Problem Statement: Write a Python program that processes information related to a rectangle and...
PYTHON Program Problem Statement: Write a Python program that processes information related to a rectangle and prints/displays the computed values. The program will behave as in the following example. Note that in the two lines, Enter length and Enter width, the program does not display 10.0 or 8.0. They are values typed in by the user and read in by the program. The first two lines are text displayed by the program informing the user what the program does. This...
USE Python 3, please type thanks! Write a Python 3 program to calculate the Body Mass...
USE Python 3, please type thanks! Write a Python 3 program to calculate the Body Mass Index (BMI) of a person if they enter their weight and their height to your program. Check out the formula here: http://www.freebmicalculator.net/calculate-bmi.php Your program should first print "Body Mass Index Calculator" The program will then ask the user if they want to enter Metric Units or English Units. Using the appropriate formula (see link above) calculate their BMI. Depending on their BMI show their...
#Write a program in Python that given a list of positive integers, return another list where...
#Write a program in Python that given a list of positive integers, return another list where each element corresponds to the sum of the digits of the elements o#f the given list. #Example: # input # alist=[124, 5, 914, 21, 3421] # output # sum=[7, 5, 14, 3, 18]
Write a program in C that sorts 3 given numbers, defined as (a,b,c) from highest to...
Write a program in C that sorts 3 given numbers, defined as (a,b,c) from highest to lowest. I already have this code finding the maximum. Need to just add the sorting code onto it. int A, B, C; A = 4; B=3; C=7; Max = A If (B > max) { Max = B; } Else if (c > max) { Max = c; }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT