List as array:
Array is a
collection of similar elements. Elements in the array can be accessed by index.
Index starts with 0. Array can be handled in python by module named array.
To create array
have to import array module in the program.
import array
Array_name =
module_name.function_name(‘datatype’,[elements])
example:
a=array.array(‘i’,[1,2,3,4])
a- array name
array- module
name
i- integer
datatype
import array
sum=0
a=array.array('i',[1,2,3,4])
for i in a:
sum=sum+i
print(sum)
10
fromlist()
function is used to append list to array. Here the list is act like a array.
arrayname.fromlist(list_name)
import array
sum=0
l=[6,7,8,9,5]
a=array.array('i',[])
a.fromlist(l)
for i in a:
sum=sum+i
print(sum)
35
a=[2,3,4,5]
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.