Question

In: Computer Science

Using the Python Program. Can an array hold a mixture of types i.e. int, float, string,...

Using the Python Program. Can an array hold a mixture of types i.e. int, float, string, array within the same array?

Show a code segment to remove the last element from a 15 element array named myStuff[]. Please explain the code.

Solutions

Expert Solution

By arrays, I’m assuming you meant Python lists since there are no built in arrays in Python. And YES, the python lists supports all kind of objects to be added to an array. For example, consider the below code.

arr=[1,2,3,'Food',5.5,True]

The arr array/list has 6 elements of type integers, string, float and boolean. There is absolutely no problem with the above code.

Show a code segment to remove the last element from a 15 element array named myStuff[].

This can be done in many ways. Some of them are listed below.

a) myStuff.pop(14)

This statement will remove element at index 14 (15th element) from myStuff, if you are sure that the array has 15 elements.

b) myStuff.pop(len(myStuff)-1)

len() returns the size of an array, subtracting one will get you the index of last element in the array, so the pop method removes the element at that index.

c) myStuff.pop(-1)

This is one of the best things that I like about python. It allows negative indicing. If you pass a negative value n as the index to pop method, it will remove n th element from rear. So in this case, this will remove 1st element from back, or in other words, the last element from front.

Let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks


Related Solutions

binarySearchLengths(String[] inArray, String search, int start, int end) This method will take in a array of...
binarySearchLengths(String[] inArray, String search, int start, int end) This method will take in a array of strings that have been sorted in order of increasing length, for ties in the string lengths they will be in alphabetical order. The method also takes in a search string and range of indexes (start, end). The method will return a String formatted with the path of search ranges followed by a decision (true or false), see the examples below. Example 1: binarySearchLengths({"a","aaa","aaaaa"},"bb",0,2) would...
The assignment is to build a program in Python that can take a string as input...
The assignment is to build a program in Python that can take a string as input and produce a “frequency list” of all of the wordsin the string (see the definition of a word below.)  For the purposes of this assignment, the input strings can be assumed not to contain escape characters (\n, \t, …) and to be readable with a single input() statement. When your program ends, it prints the list of words.  In the output, each line contains of a...
Write a java method that takes a string and returns an array of int that contains...
Write a java method that takes a string and returns an array of int that contains the corresponding alphabetic order of each letter in the received string: An illustration: the method takes: "Sara" the method returns: {4,1,3,2} another illustration: the method takes: "hey" the method returns: {2,1,3}
Write a program in C that declares the following array: int. array[] = { 1, 2,...
Write a program in C that declares the following array: int. array[] = { 1, 2, 4, 8, 16, 32 } Then write some code that accepts a number between 0 and 5 from the user and stores it in a variable called "index". Write some code that retrieves the item specified by the index, like this: int item = array[index]; Then write code that outputs the corresponding array entry based on the number the user entered. Example output: The...
Using python: Given a string x, write a program to check if its first character is...
Using python: Given a string x, write a program to check if its first character is the same as its last character. If yes, the code should output "True"
Use python There is a revenue list, how to covert it from string to int? Here...
Use python There is a revenue list, how to covert it from string to int? Here is the list ['$8,120,000,000', '$8,085,200,000', '$7,703,000,000', '$7,000,000,000', '$5,410,000,000', '$4,212,000,000', '$3,741,400,000', '$3,500,000,000', '$3,000,000,000', '$2,800,000,000', '$2,800,000,000', '$2,529,000,000', '$2,087,600,000', '$2,000,000,000', '$1,900,000,000', '$1,520,000,000', '$1,500,000,000', '$1,500,000,000', '$1,350,000,000', '$1,300,000,000', '$1,400,000,000', '$1,400,000,000', '$1,395,000,000', '$1,200,000,000', '$1,000,000,000', '$1,000,000,000', '$842,000,000', '$887,000,000', '$860,000,000', '$825,000,000', '$799,000,000', '$757,000,000', '$751,000,000', '$701,600,000', '$660,000,000', '$600,000,000', '$577,000,000', '$559,000,000', '$540,000,000', '$532,000,000', '$518,400,000', '$500,000,000', '$500,000,000', '$437,000,000', '$400,000,000', '$350,000,000', '$300,000,000', '$277,000,000'].
Activity 12: Array What is an Array? An array is a special variable, which can hold...
Activity 12: Array What is an Array? An array is a special variable, which can hold more than one value at a time. If you have a list of items (a list of car names, for example), storing the cars in single variables could look like this: var car1 = "Saab"; var car2 = "Volvo"; var car3 = "BMW"; However, what if you want to loop through the cars and find a specific one? And what if you had not...
Write a Python program which prompts the user to input a string. Then, print the string...
Write a Python program which prompts the user to input a string. Then, print the string in reverse to the terminal Sample output Please enter a word: "zeus" The reverse of zeus is suez Hint: There are several ways to accomplish this. Recall that a string is an itterable object and therefore can be used with a for loop
class Main { public static void main(String[] args) {        int[] array = {1,2,3,4,5};   ...
class Main { public static void main(String[] args) {        int[] array = {1,2,3,4,5};        //Complexity Analysis //Instructions: Print the time complexity of method Q1_3 with respect to n=Size of input array. For example, if the complexity of the //algorithm is Big O nlogn, add the following code where specified: System.out.println("O(nlogn)"); //TODO }    public static void Q1_3(int[] array){ int count = 0; for(int i = 0; i < array.length; i++){ for(int j = i; j < array.length;...
c++ language Create a file program that reads an int type Array size 10; the array...
c++ language Create a file program that reads an int type Array size 10; the array has already 10 numbers, but your job is to resize the array, copy old elements of array to the new one and make it user input and add an additional 5 slots in the array, and lastly do binary search based on user input. close the file.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT