Example : A Python Example to store and display all Static Values in an Array or Lists.
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 : Write a program in Python to find out the Length of an Array or Lists.
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 the last of an Array or Lists.
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 Lists.
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 Lists.
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 Array Values.
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 an Array or Lists 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 Lists in Python.
number = [10, 25, 58, 14, 85, 10, 10]
x = number.count(10) # counts the no. of specific 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/Empty 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
Some Important links:
0 Comments