Example : A Python Example to store and display static values in an Array or List.
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 
Example : Write a program in  Python to find out the length of an Array or List.
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 in an Array or List.
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 an Array or List.
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 an Array or List.
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 program to Reverse the elements of an Array or List of Python.
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 an Array or List.
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 present in an Array or List in Python.
number = [10, 25, 58, 14, 85, 10, 10]

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

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

Output:
3
0
Example : Solve program in Python to Copy all the elements from an array and copy into another and display it.
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 all the elements of an 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 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 

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.