Append Function Of Array Class

Overview:

  • The append() method of the array class adds an element past all the existing elements of an array instance.
  • If the new element added is different from the base type of the array Python raises a TypeError.

Example:

# Example Python program that appends an element to

# an array of floating-point numbers

import array

 

# Create an array of floating point numbers

floatArray = array.array("f", [3.14, 6.28, 12.56]);

print("Array of floating point numbers:");

print(floatArray);

 

# Append an element to the array

floatArray.append(25.12);

 

print("Array after appending an element:");

print(floatArray);

 

Output:

Array of floating point numbers:

array('f', [3.140000104904175, 6.28000020980835, 12.5600004196167])

Array after appending an element:

array('f', [3.140000104904175, 6.28000020980835, 12.5600004196167, 25.1200008392334])


Copyright 2022 © pythontic.com