- 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)
- By python : use simple functions and listsarrow_forwardplease solve this in pythonarrow_forwardPython question Follow these steps:● create:o A function that returns the largest number in a list of integers takenas an argument.o This needs to be solved recursively without using loops.Examples of input and output:largest_number([1, 4, 5, 3])=> 5largest_number([3, 1, 6, 8, 2, 4, 5])=> 8 NB please add comments so Ican follow alongarrow_forward
- def 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_forwardMust show it in Python: Please show step by step with comments. Please show it in simplest form. Please don't use any functions Please don't use any def func ()arrow_forwardIn python don't import librariesarrow_forward
- Using PYTHON: Function takes the width as a positive integer and returns the width of the row as a string. Each character has a 5/6 chance to be one of the movement symbols, (MOVEMENT_SYMBOLS = '><v^'), and 1/6 chance to be an empty symbol (EMPTY_SYMBOL = '.'). * not able to use random.choice or lists, please use random.randint and/or random.random onlyarrow_forwardIn pythonarrow_forwardSolve in Python: Write a program that removes all digits from the given input. Ex: If the input is: 1244Crescent the output is: Crescent The program must define and call the following function that takes a string as parameter and returns the string without any digits. def remove_digits(user_string)arrow_forward
- Submission: Submit python code Problem: If n is a positive integer, then n factorial (written n!) is the product of the numbers from 1 through n. Write a recursive function to calculate n factorial. Hints: n! = (n – 1) · (n– 2) 2: 1 As written, n! can be calculated iteratively with a for loop-however, when rewritten as n! = n · ((n – 1) · (n – 2) 3 · 2 · 1) = n · (n – 1)! n! is expressed in terms of (n – 1)! and can be calculated recursively with n = 1 as the base case. | Example input and output: >>> factorial(5) 120 >>>arrow_forwardJAVA requirements: pls write down source code: test cases: input: screenshot output: screenshot thanks1 Practice 2 1. (Sorted?) Write the following method that returns true if the list is already sorted in increasing order. public static boolean isSorted(int[] list) Write a test program that prompts the user to enter a list and displays whether the list is sorted or not. Here is a sample run. Note that the first number in the input indicates the number of the elements in the list. This number is not part of the list.arrow_forwardpython code. instructions are given below. Run function is already done. 1) def binary_search_rec(x, t, start, end): """ Return the left most index of x if x is in t indexed between [start, end] inclusive Otherwise return -1 You may assume that both start and end are valid index for t. Use Recursion, Do Not Use loops Your algorithm should be logarithmic to number of elements between [start, end] regardless of how many duplicates of x is. """ return 0 2) def binary_search_loop(x, t, start, end): """ Return the left most index of x if x is in t indexed between [start, end] inclusive Otherwise return -1 You may assume that both start and end are valid index for t. Use loop, Do Not Use Recursion Your algorithm should be logarithmic to number of elements between [start, end] regardless of how many duplicates of x is. """ return 0arrow_forward
- C++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology Ptr