Learn practical skills, build real-world projects, and advance your career
Created 4 years ago
Python List is a ordered collection and allows duplicate members. Lists are mutable, and hence, they can be altered even after their creation.Items in the list need not be of the same data type.
# Creating a List of 5 numbers
List = [1,2,3,4,5]
print(List)
# Accesing elements in a list
print("Third Element :",List[2])
# Adding elements to the list
List.append(6)
print("\n List after append() :" ,List)
# print the last element of list
print("\n Last Element of the list :",List[-1])
# Removing elements from List using remove()
List.remove(6)
# Slicing operation in list
List_segment = List[2:5]
print("\nSlicing elements in a range 2-4: ")
print(List_segment)
[1, 2, 3, 4, 5]
Third Element : 3
List after append() : [1, 2, 3, 4, 5, 6]
Last Element of the list : 6
Slicing elements in a range 2-4:
[3, 4, 5]
A dictionary is an unordered collection, changeable and indexed. Dictionaries have keys and values and are written with curly braces {}.Keys in the dictionary are unique .
# Creating a Dictionary
Dict = {1: 'The', 2: 'workshop', 3: 'for'}
print("\nDictionary with the use of Integer Keys: ")
print(Dict)
# Adding elements to the dictionary
Dict[4] = "WiDS"
print("\nDictionary after adding 4 th element: ")
print(Dict)
# accessing a element using key
print("Accessing a element using key:")
print(Dict[1])
Dictionary with the use of Integer Keys:
{1: 'The', 2: 'workshop', 3: 'for'}
Dictionary after adding 3 elements:
{1: 'The', 2: 'workshop', 3: 'for', 4: 'WiDS'}
Accessing a element using key:
The
A tuple is a ordered collection and are immutable unlike lists. Tuples have comma-seperated values and are written within square brackets .