Python program to print element in an array

In this python tutorial, you will learn about the Python program to print element in an array also we will check:

  • Python program to print the smallest element in an array
  • How to print the largest element in an array in Python?
  • Python program to print the number of elements present in an array
  • Python program to print the elements of an array in reverse order
  • Python program to print the duplicate elements of an array
  • Python program to print unique elements in an array
  • Python program to print the elements of an array present on an even position
  • Python program to print the middle element from the array

Python program to print smallest element in an array

Let see python program to print the smallest element in an array.

  • Firstly, we need to initialize an array
  • Now, we will store the first element of the array in the variable min.
  • Loop through the array from 0 to the length of the array.
  • if (my_arr[i]<min) condition is used to compare the value of min with the elements of an array.
  • If any element is less than min, then the min will hold the value of that element.
  • And in the end, the min will be the smallest element in the array and it will be printed.

Example:

my_arr = [10, 8, 5, 12, 19];
min = my_arr[0];
for i in range(0, len(my_arr)):
    if (my_arr[i]<min):
        min = my_arr[i];
print("The smallest element in the array is:" +str(min));

To get the output, I have used print(“The smallest element in the array is:” +str(min)). You can refer to the below screenshot for the output.

Python program to print the smallest element in an array
Python program to print the smallest element in an array

This is the program to print smallest element in an array in Python.

You may like Python Array with Examples and Groupby in Python Pandas.

Python program to print largest element in an array

Now, we will see python program to print the largest element in an array.

  • Firstly, we need to initialize an array
  • Now, we will store the first element of the array in the variable max.
  • Loop through the array from 0 to the length of the array.
  • if (my_arr[i]>max) condition is used to compare the value of max with the elements of an array.
  • If any element is greater than max, then the max will hold the value of that element.
  • And in the end, the max will be the largest element in the array and it will be printed.
my_arr = [10, 8, 5, 12, 19];
max = my_arr[0];
for i in range(0, len(my_arr)):
    if (my_arr[i]>max):
        max = my_arr[i];
print("The largest element in the array is:" +str(max));

To get the output, I have used print(“The largest element in the array is:” +str(max)). You can refer to the below screenshot for the output.

Python program to print the largest element in an array
Python program to print the largest element in an array

This is the program to print largest element in an array in Python.

You may like, Create an empty array in Python and How to add two variables in Python.

Python program to print number of elements present in an array

Here, we will see python program to print number of elements present in an array

  • Firstly, I have initialized an array
  • Now, we will use the built-in function length to calculate the length of an array
  • And at the end, we have used str(len(my_arr)) to print the length of an array.

Example:

my_arr = [10, 20, 30, 40, 50, 60];   
print("Number of elements present in the given array: " + str(len(my_arr)));

To get the output, I have used print(“Number of elements present in the given array: ” + str(len(my_arr))). You can refer to the below screenshot for the output.

Python program to print the number of elements present in an array
Python program to print the number of elements present in an array

This is how to print number of elements present in an array in Python.

Also, read Python concatenate arrays and How to subtract two numbers in Python.

Python program to print elements of an array in reverse order

Let’s see python program to print the elements of an array in reverse order.

  • Firstly, I have initialized an array
  • Now, we will loop through the array in reverse order and the loop will start from (length of array – 1) and ends at 0 by decreasing the value of i by 1.
  • Print the element using (my_arr[i]) in each iteration.

Example:

my_arr = [11, 12, 13, 14, 15];     
print("Original array: ");    
for i in range(0, len(my_arr)):    
    print(my_arr[i])  
print("Array in reverse order: ");      
for i in range(len(my_arr)-1, -1, -1):     
    print(my_arr[i])   

To get the output, I have used print(my_arr[i]). You can refer to the below screenshot for the output.

Python program to print the elements of an array in reverse order
Python program to print the elements of an array in reverse order

This is how to print elements of an array in reverse order in Python.

Check out, Python program to print pattern and How to calculate simple interest in Python.

Python program to print duplicate elements of an array

Now, we will see python program to print the duplicate elements of an array.

  • Firstly, I have initialized an array
  • The two for loop is used to find the duplicate elements.
  • The outer loop will iterate through the array from 0 to the length of an array.
  • The inner loop will be used to compare the selected element with the rest element
  • If the duplicate element is found then display the element.

Example:

my_arr = [1, 2, 3, 2, 4, 1, 5, 6, 3];     
print("Duplicate elements in the given array: ");    
for i in range(0, len(my_arr)):    
    for j in range(i+1, len(my_arr)):    
        if(my_arr[i] == my_arr[j]):    
            print(my_arr[j]);    

To get the output, I have used print(my_arr[j]). You can refer to the below screenshot for the output.

Python program to print the duplicate elements of an array
Python program to print the duplicate elements of an array

This is how to print duplicate elements of an array in Python.

Python program to print unique elements in an array

Let see python program to print unique elements in an array.

  • Firstly, I have defined a function def printUnique(arr, n)
  • Then arr.sort() is used to sort the array so that all occurrences become consecutive
  • Now, we will use for loop to traverse the sorted array
  • If there are duplicates then move the index ahead otherwise, print the current element.

Example:

def printUnique(arr, n): 
    arr.sort(); 
    for i in range(n): 
        if(i < n-1 and arr[i] == arr[i+1]): 
            while (i < n-1 and (arr[i] == arr[i+1])): 
                i=i+1; 
        else: 
            print(arr[i], end=" "); 
arr = [101, 102, 103, 104, 102, 105, 104]; 
n = len(arr); 
printUnique(arr, n); 

To get the output, I have used print(arr[i], end=” “). You can refer to the below screenshot for the output.

Python program to print unique elements in an array
Python program to print unique elements in an array

This is how to print unique elements in an array in Python.

Python program to print elements of an array present on an even position

Now we will see python program to print elements of an array present on an even position.

  • Firstly, we will initialize an array
  • Now, we will calculate the length of the declared array
  • Loop is used to initialize the value of a variable “i” to “1” because the first even position is 1, and then its value will be incremented by “2” till the last length.
  • And at the end, the elements present in an even position will be printed.

Example:

arr = [11, 12, 13, 14, 15];   
print("Elements of an array present on even position: ");  
for i in range(1, len(arr), 2):  
    print(arr[i]);   

To get the output, I have used print(arr[i]). You can refer to the below screenshot for the output.

Program to print the elements of an array present on an even position
Program to print the elements of an array present on an even position

This is how to print elements of an array present on an even position in Python.

You may like, How to swap two numbers in Python.

Python program to print middle element from an array

Here, we will see python program to print the middle element from the array

  • Firstly, we will initialize an array
  • The inbuilt method sort() is used to sort the element and it will print the sorted array
  • To print the middle element of the sorted array we will use “my_arr[int(len(my_arr)/2)]”
  • By dividing the value by 2 it will give the middle element.

Example:

my_arr = [7,13,12,9,10,4,1]
my_arr.sort()
print("Sorted array is: ",my_arr)
print("The middle value is: ",my_arr[int(len(my_arr)/2)])

To get the output, I have used print(my_arr[int(len(my_arr)/2)]). You can refer to the below screenshot for the output.

Python program to print the middle element from the array
Python program to print the middle element from the array

This is how to print middle element from an array in Python.

You may like the following Python tutorials:

In this Python tutorial, we have learned about the Python program to print the element in an array. Also, we covered these below topics:

  • Python program to print the smallest element in an array
  • Python program to print the largest element in an array
  • Python program to print the number of elements present in an array
  • Python program to print the elements of an array in reverse order
  • Python program to print the duplicate elements of an array
  • Python program to print unique elements in an array
  • Python program to print the elements of an array present on an even position
  • Python program to print the middle element from the array