- store multiple value in single variable.
- list is mutable(changeable).
- collection of ordered elements.
- allow duplicate values.
- list is one in 4 data structure.
Output
['apple','banana','guava']
List length
Output
3
Data type
Output
<class 'list'>
list creation
create a list using tuple
Output
[1,2,3]
create a list using strings
Output
['h', 'a', 'l', 'l', 'o']
Access item (slicing)
index of frist element is zero(0). python start indexing from zero(0) to N-1
Access item
x=(1,
2,
3,4,5,6,7)
print(x[0])
print(x[-1])
#slicing
print(x[2:5])
#negative slicing
print(x[-7:-2])
print(x[0])
print(x[-1])
#slicing
print(x[2:5])
#negative slicing
print(x[-7:-2])
Try it Yourself »
Output
[1]
[7]
[3, 4, 5]
[1, 2, 3, 4, 5]
[7]
[3, 4, 5]
[1, 2, 3, 4, 5]
Python arrays
- Tuple:tuple is written in Parentheses (), collection of immutable and ordered values. allow duplicate.
- List:tuple is written in square brackets [], collection of mutavle and ordered values. allow duplicate.
- Dict:tuple is written in curly brackets {}, collection of unordered and mutable values. not allow duplicate.
- Set:tuple is written in curly brackets {}, collection of unordered , unindexed and immutable values. not allow duplicate.
No comments:
Post a Comment
Tell us how you like it.