Table of Contents hide
2. Lists Array Programs in Python

Array Module Programs in Python

Example:  A Python Program to store and display the Static Values in an Array Module.
import array

# Create an integer array with static values
x = array.array('i', [10, 20, 30, 40, 50])

print("Array module elements are : ", x)

Output:
Array module elements are :  array('i', [10, 20, 30, 40, 50])

--------------  OR  ---------------

import array

# Create an integer array with static values
x = array.array('f', [10, 20, 30, 40, 50])

print("Array module elements are : ", x)

Output:
Array module elements are :  array('f', [10.0, 20.0, 30.0, 40.0, 50.0])

--------------  OR  ---------------

import array

# Create an integer array with static values
x = array.array('i', [10, 20, 30, 40, 50])

print("Array module elements are : ", x)

k=len(x)
print("Array Lengths are : ", k)

Output:
Array module elements are :  array('i', [10, 20, 30, 40, 50])
Array Lengths are :  5

--------------  OR  ---------------

import array

# Create an integer array with static values
x = array.array('i', [10, 20, 30, 40, 50])

print("Array module elements are : ", x)

x.append(60)
x.append(70)

print("Array module elements are : ", x)

Output:
Array module elements are :  array('i', [10, 20, 30, 40, 50])
Array module elements are :  array('i', [10, 20, 30, 40, 50, 60, 70])
Example : A Python Program to store and display the Dynamic/Run time/User given Values in an Array Module.
import array

# Create an empty integer array
x = array.array('i')

# Input values and add them to the array
for i in range(0,5):
    value = int(input(f"Enter value {i+1}: "))
    x.append(value)

print("Array module elements are :", x)

Output:
Enter value 1: 10
Enter value 2: 20
Enter value 3: 30
Enter value 4: 40
Enter value 5: 50
Array module elements are : array('i', [10, 20, 30, 40, 50])

--------------  OR  ---------------

import array

# Create an empty integer array
x = array.array('i')

# Input the number of elements
n = int(input("Enter the size/number of elements for an array module : "))

# Input values and add them to the array
for i in range(n):
    value = int(input(f"Enter value {i+1}: "))
    x.append(value)

print("Array module elements are :", x)

Output:
Enter value 1: 10
Enter value 2: 20
Enter value 3: 30
Enter value 4: 40
Enter value 5: 50
Array module elements are : array('i', [10, 20, 30, 40, 50])
Example : A Python Program to store and display the Sum Total of the Dynamic/Run time/User given Values in an Array Module.
import array

# Create an empty integer array
x = array.array('i')
sum=0
# Input values and add them to the array
for i in range(0,5):
    value = int(input(f"Enter value {i+1}: "))
    x.append(value)

print("Array module elements are :", x)

for i in range(0,5):
    sum=sum+x[i]
    
print(sum)

Output:
Enter value 1: 1
Enter value 2: 2
Enter value 3: 3
Enter value 4: 4
Enter value 5: 5
Array module elements are : array('i', [1, 2, 3, 4, 5])
15
Example : A Python Program to store and display the Reverse of the Dynamic/Run time/User given Values in an Array Module.
import array

# Create an empty integer array
x = array.array('i')
sum=0
# Input values and add them to the array
for i in range(0,5):
    value = int(input(f"Enter value {i+1}: "))
    x.append(value)
print("Array module elements are :", x)

x.reverse()

print("Array module elements after reverse are :", x)

Output:
Enter value 1: 2
Enter value 2: 3
Enter value 3: 4
Enter value 4: 5
Enter value 5: 6
Array module elements are : array('i', [2, 3, 4, 5, 6])
Array module elements after reverse are : array('i', [6, 5, 4, 3, 2])
Example : A Python Program to store and display the Characters in an Array Module.
import array

# Create an integer array with static values
x = array.array('u', ['A','B','C'])

print("Array module elements are : ", x)

Output:
Array module elements are :  array('u', 'ABC')

Lists Array Programs in Python

Example : A Python Program to store and display all Static Values in a Lists Array.
list1 = [10, 20, 30, 40, 50] # This is the List of integers.

print(list1[0]) # Accessing the first element of list.
print(list1[2]) # Accessing the third element of list.

print(list1)

Output:
10
30
[10, 20, 30, 40, 50]

--------------- OR ---------------

animals = ["Lion", "Tiger", "Dogs", "Cows", "Cats"]
x = animals[0]
y = animals[3]
print(x,y)

print("\n")
print(animals)

Output:
Lion Cows
['Lion', 'Tiger', 'Dogs', 'Cows', 'Cats']

--------------- OR ---------------

animals = ["Lion", "Tiger", "Dogs", "Cows", "Cats"]
for x in animals:
  print(x)

Output:
Lion
Tiger
Dogs
Cows
Cats

--------------- OR ---------------

animals = ["Lion", "Tiger", "Dogs", "Cows", "Cats"]
for x in animals:
  print(x, end=" ")

Output:
 Lion Tiger Dogs Cows Cats

--------------- OR ---------------

list1 = [10, 200.32, "Rose", 40, "Tomato"] # This is the List of Data.

print(list1[0]) # Accessing the first element of list.
print(list1[1]) # Accessing the second element of list.
print(list1[2]) # Accessing the third element of list.

print(list1)

Output:
10
200.32
Rose
[10, 200.32, 'Rose', 40, 'Tomato'] 
Example : A Python Program Example to store and display all Dynamic/Run time/User Given Values in a Lists Array.
# Initialize an empty list
x = []

# Use a loop to receive  5 values in array
for i in range(0,5):
    value = int(input(f"Enter value {i+1}: "))
    x.append(value)

print("The array values are:", x)

Output:
Enter value 1: 10
Enter value 2: 20
Enter value 3: 30
Enter value 4: 40
Enter value 5: 50
The array values are: [10, 20, 30, 40, 50]

-----------  OR  ------------

# Initialize an empty list
x = []

# Use a loop to receive  5 values in array
for i in range(5):
    value = int(input(f"Enter value {i+1}: "))
    x.append(value)

print("The array values are:", x)

Output:
Enter value 1: 10
Enter value 2: 20
Enter value 3: 30
Enter value 4: 40
Enter value 5: 50
The array values are: [10, 20, 30, 40, 50]

------------  OR  -------------

# Initialize an empty list
x = []

# Input the number of elements
n = int(input("Enter the number of array size : "))

# Use a loop to receive values
for i in range(n):
    value = int(input(f"Enter value {i+1}: "))
    x.append(value)

print("The array values are:", x)

Output:
Enter the number of array size : 5
Enter value 1: 10
Enter value 2: 20
Enter value 3: 30
Enter value 4: 40
Enter value 5: 50
The array values are: [10, 20, 30, 40, 50]

------------  OR  -------------

# Initialize an empty list
x = []

# Use a loop to receive  5 values in array
for i in range(0,5):
    value = int(input(f"Enter value {i+1}: "))
    x.append(value)
    
for i in range(0,3):
    print(x[i])

Output:
Enter value 1: 10
Enter value 2: 20
Enter value 3: 30
Enter value 4: 40
Enter value 5: 50
The array values are: 10
The array values are: 20
The array values are: 30

------------  OR  -------------

x = []

# Use a loop to receive  5 values in array
for i in range(0,5):
    value = int(input(f"Enter value {i+1}: "))
    x.append(value)

print("The array values are:", x[1:4])

Output:
Enter value 1: 10
Enter value 2: 20
Enter value 3: 30
Enter value 4: 40
Enter value 5: 50
The array values are: [20, 30, 40]

-------------  OR  -------------

x = []
# Use a loop to receive  5 values in array
for i in range(0,5):
    value = int(input(f"Enter value {i+1}: "))
    x.append(value)

#print("The first and last values are:", x[0],x[4])
print("The first and last values are:", x[0],x[-1])

Output:
Enter value 1: 1
Enter value 2: 2
Enter value 3: 3
Enter value 4: 4
Enter value 5: 5
The first and last values are: 1 5
Example : A Python Program to store and display only Odd location Values from a Lists Array.
# Initialize an empty list
x = []

# Use a loop to receive  5 values in array
for i in range(0,10):
    value = int(input(f"Enter value {i+1}: "))
    x.append(value)
    
print("The outputs are ",end=" ")    
for i in range(0,10):
    if i%2!=0:
        print(x[i], end=" ")

Output:
Enter value 1: 10
Enter value 2: 20
Enter value 3: 30
Enter value 4: 40
Enter value 5: 50
Enter value 6: 60
Enter value 7: 70
Enter value 8: 80
Enter value 9: 90
Enter value 10: 100
The outputs are  20 40 60 80 100
Example : A Python Program to store and display only Odd Values from a Lists Array.
# Initialize an empty list
x = []

# Use a loop to receive  5 values in array
for i in range(0,10):
    value = int(input(f"Enter value {i+1}: "))
    x.append(value)
    
print("The outputs are ",end=" ")    
for i in range(0,10):
    if x[i]%2!=0:
        print(x[i], end=" ")

Output:
Enter value 1: 41
Enter value 2: 20
Enter value 3: 35
Enter value 4: 6
Enter value 5: 21
Enter value 6: 22
Enter value 7: 55
Enter value 8: 10
Enter value 9: 78
Enter value 10: 91
The outputs are  41 35 21 55 91
Example : A Python Program to store and display the Sum Total of all the dynamic/run time/user-given values in a lists array.
# Initialize an empty list
x = []
sum=0
# Use a loop to receive values
for i in range(0,5):
    value = int(input(f"Enter value {i+1}: "))
    x.append(value)

print("The array values are:", x)

for i in range(0,5):
    sum=sum+x[i]
print("The sum total is ",sum)

--------------  OR  ---------------

# Initialize an empty list
x = []
sum=0
# Use a loop to receive values
for i in range(5):
    value = int(input(f"Enter value {i+1}: "))
    x.append(value)

print("The array values are:", x)

for i in range(5):
    sum=sum+x[i]
print("The sum total is ",sum)

Output:
Enter value 1: 1
Enter value 2: 2
Enter value 3: 3
Enter value 4: 4
Enter value 5: 5
The array values are: [1, 2, 3, 4, 5]
The sum total is  15
Example : Write a program in  Python to find out the Length of a Lists Array.
animals = ["Lion", "Tiger", "Dogs", "Cows", "Cats"]
x = len(animals)
print("The length of Array is = ",x)
print("The length of Array is = ",len(animals))

Output:
The length of Array is =  5
The length of Array is =  5
Example : A Python program to add a new element to the last component of a lists array.
animals = ["Lion", "Tiger", "Dogs", "Cows", "Cats"]
for x in animals:
  print(x, end=" ")

animals.append("Goat")    # Add new element at the end
print("\n")
for x in animals:
  print(x, end=" ")

Output:
Lion Tiger Dogs Cows Cats 

Lion Tiger Dogs Cows Cats Goat 

--------------- OR ---------------

number = [10, 25, 58, 4, 85]
for x in number:
  print(x, end=" ")

number.insert(2, 232)      # add new item(232) at specific location(2)
number.insert(4, "Mango")
print("\n")
for x in number:
  print(x, end=" ")

Output:
10 25 58 4 85 

10 25 232 58 Mango 4 85 
Example : A program in Python to Delete/Remove Specific Elements from a Lists Array.
animals = ["Lion", "Tiger", "Dogs", "Cows", "Cats"]
for x in animals:
  print(x, end=" ")

animals.pop(1)

print("\n")
for x in animals:
  print(x, end=" ")

animals.remove("Cows")

print("\n")
for x in animals:
  print(x, end=" ")

Output:
Lion Tiger Dogs Cows Cats 

Lion Dogs Cows Cats 

Lion Dogs Cats 
Example : Write a Python program to Sort the elements in a Lists Array.
number = [10, 25, 58, 4, 85]
for x in number:
  print(x, end=" ")

# number.sort()   sort the list in Ascending order
number.sort(reverse=False)      # sort the list in Ascending order
print("\n")
for x in number:
  print(x, end=" ")

number.sort(reverse=True)      # sort the list in Descending order
print("\n")
for x in number:
  print(x, end=" ")

Output:
10 25 58 4 85 

4 10 25 58 85 

85 58 25 10 4
Example : A Python program to display the Sum of Static Values in Lists Array
sum=0
number = [10, 25, 58, 4, 85]
for x in number:  
 sum=sum+x  
print(sum)

Output:
182

-----------  OR  ------------

sum=0
i=0
number = [10, 25, 58, 4, 85]
for x in number:  
 print(number[i],end=" ")
 sum=sum+x
 i=i+1

print("\n")
print(sum)

Output:
10 25 58 4 85 

182

-----------  OR  ------------

# Initialize sum to 0
sum = 0

# Loop through the first 10 natural numbers (1 to 10)
for num in range(1, 11):
    sum=sum + num  # Add the current number to the sum

print("The sum of the first 10 natural numbers is:", sum)

Output:
The sum of the first 10 natural numbers is: 55
Example : A program to Reverse the elements of a Lists Array of Python.
x = []
# Use a loop to receive values
for i in range(0,5):
    value = int(input(f"Enter value {i+1}: "))
    x.append(value)
print("The accepted array values are:", x)

print("The reverse of array is ")
for i in range(4,-1,-1):
    print (x[i], end=" ")

Output:
Enter value 1: 10
Enter value 2: 20
Enter value 3: 30
Enter value 4: 40
Enter value 5: 50
The accepted array values are: [10, 20, 30, 40, 50]
The reverse of array is 
50 40 30 20 10 

-------------  OR  --------------    

number = [10, 25, 58, 4, 85]
for x in number:
  print(x, end=" ")

number.reverse()      # reverse the list
print("\n")
for x in number:
  print(x, end=" ")

Output:
10 25 58 4 85 

85 4 58 25 10
Example : A program in Python to find out the index location of a specific element of a Lists Array.
number = [10, 25, 58, 4, 85]

x = number.index(58)  # gives the first occurrence of index value of an item
print(x, end=" ")

Output:
2
Example : Write a program to Count the no. of specific elements in Python in a Lists Array.
number = [10, 25, 58, 14, 85, 10, 10]

x = number.count(10)      # counts the no. of specific items 10 present in the list.

x = number.count(5)
print(x, end=" ")

Output:
3
0
Example : Write a program in Python to Copy all the elements of a Lists Array into another one and display it.
# Initialize an empty list
x = []
y = []

# Use a loop to receive  5 values in array
for i in range(0,5):
    value = int(input(f"Enter value {i+1}: "))
    x.append(value)

y = list(x)  #The list() constructor creates a new list with the same elements.   
print(y, end=" ")

Output:
Enter value 1: 10
Enter value 2: 20
Enter value 3: 30
Enter value 4: 40
Enter value 5: 50
[10, 20, 30, 40, 50]

------------  OR  -------------

# Initialize an empty list
x = []
y = []

# Use a loop to receive  5 values in array
for i in range(0,5):
    value = int(input(f"Enter value {i+1}: "))
    x.append(value)

y = x[:]  # slicing ([:]) to create a shallow copy of the list.
    
print(y, end=" ")

Output:
Enter value 1: 1
Enter value 2: 2
Enter value 3: 3
Enter value 4: 4
Enter value 5: 5
[1, 2, 3, 4, 5] 

------------  OR  -------------

# Initialize an empty list
x = []
y = []

# Use a loop to receive  5 values in array
for i in range(0,5):
    value = int(input(f"Enter value {i+1}: "))
    x.append(value)

for i in x:
    y.append(i) 
    
print(y)

Output:
Enter value 1: 20
Enter value 2: 35
Enter value 3: 62
Enter value 4: 24
Enter value 5: 15
[20, 35, 62, 24, 15]

------------  OR  -------------

number = [10, 25, 58, 4, 85]
for x1 in number:
  print(x1, end=" ")

y = number.copy()      # copy the list
print("\n")
for x2 in y:
  print(x2, end=" ")

Output:
10 25 58 4 85 

10 25 58 4 85 
Example : A Python program to Clear/Empty all the elements of a Lists Array.
number = [10, 25, 58, 4, 85]
for x1 in number:
  print(x1, end=" ")

number.clear()      # remove all the items from the list
print("\n")
for x2 in number:
  print(x2, end=" ")

Output:
10 25 58 4 85
Example : A Python program to Append All the Elements into another Lists Array.
number1 = [10, 25, 58, 24, 85]
number2 = [200, 302, 760, 525, 650]
for x1 in number1:
  print(x1, end=" ")

print("\n")
for x2 in number2:
  print(x2, end=" ")

number1.extend(number2)  # Adds one array into another

print("\n")
for x3 in number1:
  print(x3, end=" ")

Output:
10 25 58 24 85 

200 302 760 525 650 

10 25 58 24 85 200 302 760 525 650
 

Tuple Array Programs in Python

Example : A Python program to store and display static values from a Tuple Array.
x = (10, 20, 30, 40, 50)    
print(x)

Output:
(10, 20, 30, 40, 50)

------------  OR -----------

y= (10, "Hello India", 3.141, True) 
print(y)

Output:
(10, 'Hello India', 3.141, True)
Example : A Python program to store and modify static values in a Tuple Array.
x = (10, 20, 30, 40, 50)
x[2] = 50   
print(x)

Output:
TypeError: 'tuple' object does not support item assignment

Python Home Page


Some Important links:

Loading

Categories: Python Theory

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.