Python Array Exercises: Practice for Array Manipulation

thumb_up 1  ·  sell Python array exercises, Array manipulation exercises in Python, Array manipulation practice in Python

Example 1

Python program to find the largest number in an array −

 
import array as arr a = arr.array('i', [10,5,15,4,6,20,9]) print (a) largest = a[0] for i in range(1, len(a)): if a[i]>largest: largest=a[i] print ("Largest number:", largest)

It will produce the following output −

array('i', [10, 5, 15, 4, 6, 20, 9])
Largest number: 20

Example 2

Python program to store all even numbers from an array in another array −

 
import array as arr a = arr.array('i', [10,5,15,4,6,20,9]) print (a) b = arr.array('i') for i in range(len(a)): if a[i]%2 == 0: b.append(a[i]) print ("Even numbers:", b)

It will produce the following output −

array('i', [10, 5, 15, 4, 6, 20, 9])
Even numbers: array('i', [10, 4, 6, 20])

Example 3

Python program to find the average of all numbers in a Python array −

 
import array as arr a = arr.array('i', [10,5,15,4,6,20,9]) print (a) s = 0 for i in range(len(a)): s+=a[i] avg = s/len(a) print ("Average:", avg) # Using sum() function avg = sum(a)/len(a) print ("Average:", avg)

It will produce the following output −

array('i', [10, 5, 15, 4, 6, 20, 9])
Average: 9.857142857142858
Average: 9.857142857142858

Exercise Programs

  • Python program find difference between each number in the array and the average of all numbers

  • Python program to convert a string in an array

  • Python program to split an array in two and store even numbers in one array and odd numbers in the other.

  • Python program to perform insertion sort on an array.

  • Python program to store the Unicode value of each character in the given array.

 

The End! should you have any inquiries, we encourage you to reach out to the Vercaa Support Center without hesitation.

Was this answer helpful?

Related Articles

description

Exploring Python's Key Characteristic

Python is a feature rich high-level, interpreted, interactive and object-oriented scripting language. This tutorial will list down some of…

arrow_forward
description

Comparing Python and C++

Both Python and C++ are among the most popular programming languages. Both of them have their advantages and disadvantages. In this…

arrow_forward
description

Creating a Python Hello World Program

This tutorial will teach you how to write a simple Hello World program using Python Programming language. This program will make use of…

arrow_forward
description

Python's Versatile Application Domains

Python is a general-purpose programming language. It is suitable for development of wide range of software applications. Over last few…

arrow_forward
description

Understanding the Python Interpreter

Python is an interpreter-based language. In a Linux system, Python's executable is installed in /usr/bin/ directory. For Windows, the…

arrow_forward
arrow_back « Back