How to Print the Count of an Array in Python

How to Print the Count of an Array in Python thumbnail
Python uses the LEN function to count the members of an array.

The method used to print the count of an array in Python depends on its release date and version. Python versions 2.6 and earlier use a simple “print” statement while Python 3000, released in February 2009, uses a print function. There are subtle, but major differences between the two commands and when you use Python 3000’s print function, there is no backward compatibility. Regardless of how you format the print statement, the LEN function is necessary to return the count of the array.

Instructions

  1. Using the Print Statement

    • 1

      Declare the array and identify its component members. An array that includes even numbers from 10 through 20 will appear as:

      evenNumbers = array([10, 12, 14, 16, 128, 20])
      (Reference 2-section 3.1.2 (second to the last entry before section 3.2 starts)-all steps)

    • 2

      Type a print statement telling Python to display a count of the array:
      print len (evenNumbers)

    • 3

      View the results:
      [6]

    Using the Print Function

    • 4

      Declare the array and identify its component members. An array that includes even numbers from 10 through 20 will appear as:
      evenNumbers = ([10, 12, 14, 16, 128, 20])

    • 5

      Review the syntax of the print function command. Before adding function arguments, the skeleton of the statement appears as print(). Inside the parentheses go instructions to get the length of the array using the len function, your object – the array – and an ender string – end – with an instruction to end the print function and start a new line of code:
      print (len (evenNumbers), end="\n")

    • 6

      View the results of the print function, which appear as:
      [6]

Related Searches:

References

  • Photo Credit Ryan McVay/Photodisc/Getty Images

Comments

Related Ads

Featured