and Tuple
23/06/2018
Workshop on “Introduction to
Python”
Department Of
Computer Science
Engineering
SKFGI, Mankundu
,1.Python Data Structure
A data Structure is a group of data element that are put together under one
name. Data Structure defines a particular way of sorting and organizing data
in a computer so that it can be used efficiently.
There are 3 different data structure in the Python programming language:
1.1. Sequence: It is most basic data structure in python. In the sequence
data structure each element has a specific index .this index value is start
from 0and its automatically incremented for the next element in the
sequence. There are several types of sequences in Python, the following
three are the most important.
1.1.1Lists are the most versatile sequence type. The elements of a list can
be any object, and lists are mutable - they can be changed. Elements can
be reassigned or removed, and new elements can be inserted. Allows
duplicate members.
1.1.2Tuples are like lists, but they are immutable - they can't be changed.
Allows duplicate members.
1.1.3 Strings are a special type of sequence that can only store characters,
and they have a special notation. String is also immutable
1.2. Set is a collection which is unordered and unindexed. No duplicate
members.
1.3. Dictionary is a collection which is unordered, changeable and indexed.
No duplicate members.
2. Python List
Lists are one of the most powerful data strucure in Python. They are just like
the arrays declared in other languages. But the most powerful thing is that
,list need not be always homogeneous. A single list can contain strings,
integers, as well as objects. Lists can also be used for implementing stacks
and queues. Lists are mutable, i.e., they can be altered once declared.
2.1How to create a list?
In Python programming, a list is created by placing all the items (elements)
inside a square bracket [ ], separated by commas.
It can have any number of items and they may be of different types
(integer, float, string etc.).
Example of list declarations:
Code Output
1. Empty list my_list = [] []
print(my_list)
2. List of integers my_list = [100, 200, 300] [100, 200, 300]
print(my_list)
3. List with mixed my_list = [1, "Mother", [1, 'Mother', 3.414]
data types 3.414]
print(my_list)
4. Nested list my_list = ["Morning", [8, 4, ['Morning', [8, 4, 6],
6], ['a']] ['a']]
print(my_list)
2.2List Index is used to access elements from a list
We can use the index operator [] to access an item in a list. Index starts
from 0. So, a list having 5 elements will have index from 0 to 4.
Example:
Code Output
aList = [123, 'xyz', 'zara', 'abc']; Index for xyz : 1
print( "Index for xyz : ", aList.index( 'xyz' ) ) Index for zara : 2
, print( "Index for zara : ", aList.index( 'zara' ))
2.3Negative indexing
Negative index is used in python to index starting from the last element of
the list, tuple or any other container class which supports indexing. -1 refers
to the last index, -2 refers to the second last index and so on.
Code Output
For lists: 5
array = [0,1,2,3,4,5]
print(array[-1])
array = [0,1,2,3,4,5] 2
print(array[-4])
2.4Slice lists in Python
Similar to string, list can also be sliced. We can access a range of items in a
list by using the slicing operator (colon) and square brackets[] are used to
slice along with the index. Syntax :
Seq=List[start:end:step]
Code Output