The Self-Taught Computer Scientist by Althoff Cory;

The Self-Taught Computer Scientist by Althoff Cory;

Author:Althoff, Cory; [Althoff, Cory]
Language: eng
Format: epub
Publisher: John Wiley & Sons, Incorporated
Published: 2021-09-27T00:00:00+00:00


Creating an Array

If you are programming in Python, you can use a list in most cases when you need an array. However, if you need the performance of a homogenous array, you can use Python's built-in array class. Here is how it works:

import array arr = array.array('f', (1.0, 1.5, 2.0, 2.5)) print(arr[1]) >> 1

First, you import Python's built-in array module:

import array

Then, you pass array.array two parameters. The first parameter tells Python what data type you want your array to hold. In this case, f stands for float (a data type in Python for decimal numbers), but you can also create an array with Python's other data types. The second parameter is a Python list containing the data you want to put into your array.

arr = array.array('f', (1.0, 1.5, 2.0, 2.5))



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.