Collection
means storage location of number of values in same named space location.
There are
four collection data types in the Python programming language:
• List is a collection which is ordered and changeable. Duplicate elements can be stored in list.
• Tuple is a collection which is ordered and unchangeable. It also allows duplicate elements.
• Set is a collection which is unordered and unindexed. Duplicate elements not allowed here.
• Dictionary is a collection which is unordered and changeable. Duplicate elements not allowed here.
Python Lists
Create a
List:
print( mylist)
List with duplicate values
Example
Lists
allow duplicate values:
print( mylist)
Output
will be: = [‘Assam’, ‘Benaras’, ‘Chennai’, ‘Assam’]
To
determine how many items a list has, use the len() function:
Print the number of items in the list:
mylist = ["apple", "banana", "cherry"]
print(len( mylist))
List
Items - Data Types
String,
int and boolean data types:
list2 =
[1, 5, 7, 9, 3]
list3 =
[True, False, False]
A list can contain different data types:
A list
with strings, integers and boolean values:
type()
Example
What is
the data type of a list?
mylist =
["apple", "banana", "cherry"]
print(type(mylist))
It is
also possible to use the list() constructor when creating a new list.
Using the
list() constructor to make a List:
No comments:
Post a Comment