In this tutorial, you will learn about set and list. In list you can modify its element after it created. In set also you can modify its element after it created. In set you can see the unordered collection but in list you can see the ordered collection of items or elements. You can change the elements in the list but you can not change or replace the elements in the set but you can remove the element by using some set methods.
Set in Python
A set is a collection which is unordered and unindexed, and do not allow duplicate values.
- unordered means that the items in a set do not have a defined order.
- set items can appear in a different order every time you use them, and cannot be referred to by index or key.
- A set is created by placing all the items(elements) inside curly braces{},separated by comma(,), or by using the built-in set() function.
- it can have any number of items and they may be of different types(integer, float, tuple, string, etc..).But a set cannot have mutable elements like lists,
sets or dictionaries as its elements.
sathwika={1,2,45,28,53,100,3.4,56,78} print(type(sathwika)) print(sathwika)
output:
<class 'set'> {1, 2, 3.4, 100, 45, 78, 53, 56, 28}
List in Python
A list in python is used to store the sequence of various types of data.
- Python lists are mutable type its mean we can modify its element after it created.
- A list can be defined as a collection of values or items of different types.
The items in the list are separated with the comma(,) and enclosed with the square brackets[].
sahithi=[1,2.2,'python',True] print(sahithi) varsha=[2.3,4,3,5.6,7,25] #for indexng print(varsha) print(varsha[2])
output:
[1, 2.2, 'python', True] 3