- a.
Explanation of Solution
List method “index()”:
- • “index()” method in Python finds an element in the list and outputs the element’s index.
- • This method is useful in finding the position of the given element in the list.
- • The “index()” method takes an element as an input and searches the list for that element appearance and outputs its position.
- • If more than one occurrence of the element is present in the list, then the method outputs the lowest index.
- • If the element is not present in the list, then this method raises a “ValueError” exception.
Syntax:
In Python, “index()” has the following syntax,
element_index = List_name.index(element)
Explanation:
In the above code,
- • The variable “element_index” stores the return value from the “insert()” method.
- • The variable “List_name” represents the name of the list.
- • The variable “element” indicates the element that is going to be searched in the list.
Example program:
Consider the following example that demonstrates the “index()” method...
- b.
Explanation of Solution
List method “insert()”:
- • The “insert()” method in Python adds an element to the list.
- • This method places an element in the list at a given index.
- • This method takes an element and its position as input parameters and places the given element at the indicated position.
- • When the element is inserted in the list, the list size is increased to hold the given element.
- • The element at the current index and the elements following it are moved by one place to the list end.
- • This method never throws an exception even if the index is not a valid value.
- • If the index value that is greater than the list size is specified as element’s position, then this method inserts the element at the list end.
- • If the index value is negative and it indicates an invalid position, then this method places the element at the list beginning.
Syntax:
In Python, “insert()” has the following syntax,
List_name.insert(element_index, element)
Explanation:
In the above code,
- • The variable “List_name” represents the name of the list.
- • The variable “element_index” indicates the position where the “element” to be inserted in the list.
- • The variable “element” indicates the element that is going to be inserted in the list.
Example program:
Consider the following example that demonstrates the “insert()” method...
- c.
Explanation of Solution
List method “sort()”:
- • “sort()” method sorts the list elements.
- • This method arranges the elements in the list in the ascending order.
- • So that the elements in the list are arranged from lower to higher values.
- • This method does not receive any input and does not return a value.
Syntax:
In Python, “sort()” has the following syntax,
List_name.sort()
Explanation:
In the above code,
- • The variable “List_name” represents the list name that is to be sorted.
Example program:
Consider the following example that demonstrates the “sort()” method.
#Function definition
def main():
#Create a list to store the names
mynames_list = ['John', 'Tom', 'David', 'Kim']
#Print message to the user
;&#x...
- d.
Explanation of Solution
List method “reverse()”:
- • “reverse()” method reverses the list elements.
- • This method changes the elements’ orders in the list.
- • So that the list contains the elements in reverse order.
- • This method does not obtain any input and does not output a value.
Syntax:
In Python, “reverse()” has the following syntax,
List_name.reverse()
Explanation:
In the above code,
- • The variable “List_name” represents the list name that is to be reversed.
Example program:
Consider the following example that demonstrates the “reverse()” method.
#Function definition
def main():
#Create a list to store the numbers
mynumbers_list = [1, 15, 11, 21, 12]
#Print message to the user
print('The numbers list before r...
Want to see the full answer?
Check out a sample textbook solutionChapter 7 Solutions
Starting Out with Python (4th Edition)
- How can solve this problem using for looparrow_forwardWith number of array elements, a desired element will be searched in the string, and if it is found, its index will be returned, and if not found, the value -1 will be returned. (Use Pointer )arrow_forwardComplete my C++ program: Instructions: You have to continue on implementing your Array List namely the following functions: Example ArrayList: [10, 30, 40, 50] void addAt(int num, int pos) This method will add the integer num to the posth position of the list. Performing addAt(20, 2) in the example list will add 20 at the 2nd position and the array will now look like this: [10, 20, 30, 40, 50] When the value of pos is greater than the size + 1 or less than one, output "Position value invalid" void removeAt(int pos) Removes the number in the posth position of the list. Performing removeAt(3) in the example list will remove the 3rd element of the list and the updated array will be: [10, 30, 50] When the value of pos is greater than the size or less than one, output "Position value invalid" void removeAll(int num) Removes all instances of num in the array list. In this array [10, 10, 20, 30, 10], performing removeAll(10) will remove all 10's and the list will look like this: [20,…arrow_forward
- QUESTION 1 Write a program that computes and displays the five set of products of each three random numbers between 1 and 20 using only for loops (Don't use Arrays). Here is a sample run: The Product 1: of 2, 10, 8 is 160 The Product 2: of 11, 3, 20 is 660 The Product 3: of 5, 12, 8 is 480 The Product 4: of 3, 5, 7 is 105 The Product 5: of 12, 11, 18 is 2376arrow_forwardplease code in python Forbidden concepts: arrays/lists (data structures), recursion, custom classes You have been asked to take a small icon that appears on the screen of a smart telephone and scale it up so it looks bigger on a regular computer screen.The icon will be encoded as characters (x and *) in a 3 x 3 grid as follows: (refer image1 ) Write a program that accepts a positive integer scaling factor and outputs the scaled icon. A scaling factor of k means that each character is replaced by a k X k grid consisting only of that character. Input Specification:The input will be an integer such that 0 < k ≤ 10. Output Specification:The output will be 3k lines, which represent each individual line scaled by a factor of k and repeated k times. A line is scaled by a factor of k by replacing each character in the line with k copies of the character. [refer image2]arrow_forwarddef cuberoot(n): """Computes the cube root of float n by fixpoint iteration""" cuberoot: This function should use fixed-point iteration to return the cube root of a given n (positive or negative floating point number). Your implementation should ideally return a precise approximation, but must be correct to at least eight significant digits and must use fixpoint iteration.arrow_forward
- Give solution in characters. Character must be 7 to 8.arrow_forwardUsing “Def” keyword write a function that compares two arrays, if both are the same it will return true, else it will return false. (The function name will be AreArraysEqual) Not: Write some print functions to see the outputs inside each function. (Python programming language)arrow_forwardgvwsrearrow_forward
- Arrays Arrays- Lists In Pyhon Assignment: Create a program which does the following: 1. Asks the user for 5 numbers. 2. Prints out the numbers entered. 2. Prints out the average of those five numbers Cree un programa que haga lo siguiente: 1. Pide al usuario 5 números. 2. Imprime los números ingresados. 3. Imprime el promedio de esos cinco números An example of possible output is shown below: A continuación se muestra un ejemplo de salida posible: Output You entered the following numbers: 10 20 30 40 The average of the numbers is: 25.0arrow_forwardhelp with c++...paste indented code plzz Q3: Ask user enter size of array N, then ask user enter maximum number of array element X, then create an array size N, and assign each element of array to random number between 1~X. Print the array, and also find which element appeared most in the array, print all if there are multiple elements which are most at the same time. Sample input: Enter N: 20 Enter X: 10 Sample output: 8 7 10 8 1 7 4 3 4 7 5 6 4 3 1 10 1 9 9 10 1 4 7 appear mostarrow_forwardUsing “Def” keyword write a function that returns the sum of each element for the given array (The function name will be SumofArrayElements) Not: Write some print functions to see the outputs inside each function. (Python programming language)arrow_forward
- C++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology Ptr