Professional Documents
Culture Documents
3
Basic Data Structures in Python – a review
• Basic data structures in Python – lists, tuple, dictionaries
and sets
• Lists – mutable, mixed data types, indexed data structures
• Ex: [1, 2, 3] [‘one’, ‘two’, ‘three’] [‘apples’, 50, True] [‘mouse’, [1,2,3]]
• Accessing a list : - Elements are accessed using the index operator [ ] . Index starts from 0
• Slicing a list: - A range of elements in a list can be accessed using the slicing operator :
and two indexes from where the portion of the list has to be sliced
• Ex: my_list = ['p','r','o','g','r','a','m','i','z’]
• print(my_list[2:5]) Output is ['o', 'g', 'r’]
• Methods used with a list : listname.append(), listname.extend(), listname.insert(),
listname.remove(), listname.pop(), listname.clear()
Refer Colab Notebook, LAB 1 (Python Basics).ipynb
4
Basic Data Structures in Python – a review
• Iterating over a list is using a for loop
• Ex: for k in range(2,10,2):
print (k)
• Output : ???
• Refer Colab Notebook, LAB 1 (Python Basics).ipynb
5
Basic Data Structures in Python – a review
• Tuples – an indexed linear data structure, mixed data
types but immutable – once defined cannot be altered.
• Example : num = (10,20,30) student = (‘John’, 48, ‘Computer Science’, 3.42)
• Refer Colab notebook LAB 1 (Python Basics).ipynb