0% found this document useful (0 votes)
12 views2 pages

Simple Python Array Programs

program in array

Uploaded by

Dilip Kumar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views2 pages

Simple Python Array Programs

program in array

Uploaded by

Dilip Kumar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Programs Based on Array

1. Write a Python program to convert an array to an ordinary list


with the same items
2. Write a Python program to find sum of elements in an array
3. Write a Python program to find largest element in an array
4. Write a Python program to Split the array and add the first
part to the end
5. Write a Python program for Find reminder of array
multiplication divided by n
6. Write a Python program to remove the first occurrence of a
specified element from an array.
7. Write a Python program to Reconstruct the array by replacing
arr[i] with (arr[i-1]+1) % M
8. Write a Python program to create an array of 5 integers and
display the array items. Access individual element through
indexes.
9. Write a Python program to append a new item to the end of
the array
10. Write a Python program to reverse the order of the items in
the array.
11. Write a Python program to get the number of occurrences of
a specified element in an array.
12. Write a Python program to append items from inerrable to
the end of the array.
13. Write a Python program to append items from a specified
list.
14. Write a Python program to insert a new item before the
second element in an existing array.
15. Write a Python program to remove a specified item using
the index from an array.

Common questions

Powered by AI

To count occurrences of a specific element in an array, use the `count()` method. For example, `arr = [1, 2, 3, 2, 4]`, to count `2`s, call `arr.count(2)`, resulting in `2` as the count because the number `2` appears twice in the array .

To find the largest element in an array in Python, you can use the built-in `max()` function. For example, given an array `arr = array('i', [2, 3, 1, 5, 4])`, you can determine the largest element by calling `max(arr)`, which would return `5`. This function compares each element in the array and returns the largest one .

To reverse the order of items in an array, apply the `reverse()` method or list slicing. For `arr = [1, 2, 3]`, use `arr.reverse()` to get `[3, 2, 1]`. Alternatively, slicing with `arr = arr[::-1]` achieves the same result, rearranging elements in reverse order .

To find the remainder of the multiplication of elements in an array divided by a number `n`, iterate through the array to calculate the product, then use the modulus operator with `n`. For example, for `arr = [1, 2, 3]` and `n = 3`, compute the product `product = 1 * 2 * 3 = 6`, and then `remainder = product % n`, which gives `remainder = 6 % 3 = 0` .

To reconstruct an array replacing each element with `(arr[i-1] + 1) % M`, iterate through each element, applying the transformation. Assume `arr = [3, 5, 9]` and `M = 10`; create a new list where `new_arr[i] = (arr[i-1] + 1) % M`. Start `new_arr[0] = arr[0]`. Continue with the transformation: `new_arr = [3, 4, 6]` applying the formula successively on each element .

To append items from an iterable to an array in Python, use the `extend()` method. For example, if `arr = [1, 2, 3]` and you have an iterable `more_items = [4, 5]`, use `arr.extend(more_items)` to produce `arr = [1, 2, 3, 4, 5]`. This method appends each item from the iterable to the end of the array .

To append an item to the end of an array, use the `append()` method. If `arr = [1, 2, 3]` and you append `4` using `arr.append(4)`, it becomes `[1, 2, 3, 4]`. This increases the total number of elements by one, shifting the last index from 2 to 3, and the newly appended item takes this new last index position .

To split an array and move the first part to the end, you can use slicing. For instance, consider `arr = [1, 2, 3, 4, 5]` and you want to split and reposition after index `k`. Applying slices, you can arrange it as `arr = arr[k:] + arr[:k]`, thus splitting after the third element: `k=3`, resulting in `arr = [4, 5, 1, 2, 3]`. This operation divides the array at the specified index and reorders it .

To remove the first occurrence of a specified element from an array, you can use the `remove()` method. For instance, if you have `arr = [1, 2, 3, 2, 4]` and want to remove the first `2`, call `arr.remove(2)`. This will modify `arr` to `[1, 3, 2, 4]`, removing the first `2` it encounters .

To convert an array to a list in Python, you can use the `list()` function. For example, if you have an array `arr = array('i', [1, 2, 3, 4])`, you can convert it to a list using `lst = list(arr)`. This will produce a list `lst` containing the same elements in the same order as the array.

You might also like