Python Lists
.....................................................................................................................................................
mylist = ["apple", "banana", "cherry"]
.....................................................................................................................................................
List
.....................................................................................................................................................
Lists are used to store multiple items in a single variable.
Example
Create a List:
.....................................................................................................................................................
thislist = ["apple", "banana", "cherry"]
print(thislist)
OUTPUT is:
['apple', 'banana', 'cherry']
.......................................................................................................................................................
List items are ordered, changeable, and allow duplicate values.
List items are indexed, the first item has index [0], the second item has index [1] etc.
items have a defined order, and that order will not change.
We can change, add, and remove items in a list after it has been created
.....................................................................................................................................................
Allow Duplicates
Since lists are indexed, lists can have items with the same value:
thislist = ["apple", "banana", "cherry", "apple", "cherry"]
print(thislist)
.....................................................................................................................................................
OUTPUT
.....................................................................................................................................................
['apple', 'banana', 'cherry', 'apple', 'cherry']
.....................................................................................................................................................
thislist = ["apple", "banana", "cherry"]
List Length
print(len(thislist)) --->O/P 3
type()
print(type(mylist)) --->O/P <class 'list'>
.....................................................................................................................................................
.....................................................................................................................................................
A list can contain different data types:
list1 = ["abc", 34, True, 40, "male"]
List items can be of any data type:
list1 = ["apple", "banana", "cherry"]
list2 = [1, 5, 7, 9, 3]
list3 = [True, False, False]
.................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................... THANKS 😊
Comments
Post a Comment