.

Sunday, August 2, 2015

Data Structures 1: Python List/Arrays

As we have discussed 3 different sorting algorithms in the previous posts (Selection Sort , Bubble Sort, Insertion Sort ), you may have noticed that the input is stored as a sequence in square brackets[] and assigned to a variable. This notation is for creating list in Python. Lists or arrays are generally for storing a sequence of items of same data type. Lists are most basic data structure and comes in-built in most of the programming languages. Python list allows you to insert different data type items and hence made things easier.


Syntax


>>> fruits = ['Banana', 'Apple', 'Lime','Oranges','Grapes']


Operations Lists can be indexed,sliced,manipulated with other inbuilt functions. Below are shown some of the examples.


>>> print(fruits[1])
>>> print(fruits[1:3])
>>> fruits.append('Mango')

9 comments :