Docsity

Data Structure - Python, Lecture notes of Programming Languages

Toggle sidebar
bg1
Ngawang T
DATA STRUCTURE
Unit V
Data structure are specific methods of organizing, managing and storing data
efficiently in a computer so that operations can be performed effectively.
Built-in data structure:
❖List
❖tuple
❖set
❖dictionary
There are TWO types of data structure in Python:
✓Built-in
✓User-defined/ Advanced Data structures
User-defined/Advance data structure:
✓stack
✓Queue
✓Link list
✓Tree and Graph
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23

Partial preview of the text

Download Data Structure - Python and more Lecture notes Programming Languages in PDF only on Docsity!

DATA STRUCTURE

Unit V

Data structure are specific methods of organizing, managing and storing data

efficiently in a computer so that operations can be performed effectively.

Built-in data structure :

❖ List

❖ tuple

❖ set

❖ dictionary

There are TWO types of data structure in Python:

✓ Built-in

✓ User-defined/ Advanced Data structures

User-defined/Advance data structure:

✓ stack

✓ Queue

✓ Link list

✓ Tree and Graph

String

It represented by the group of characters.

Example: str = “Hello”

There are THREE operations that can generally perform on any sequence.

1. Indexing: It means retrieving any element by its position number.

Example:

s = “HELLO”

H E L L O

Index: 0 1 2 3 4

Char:

Negative index: - 5 - 4 - 3 - 2 - 1

✓ Positive Index starts form 0(left to right)

✓ Negative index starts form - 1(right to left)

✓ Access using s [ index ]

Cont.…..

2. Slicing: ❖ It means retrieving a range (or piece) of elements.

❖ Extract part of string using s[start: stop] / s[start: stop: step]

Example:

s = “PYTHON”

Index: 0 1 2 3 4 5

s:

Negative index:- 6 - 5 - 4 - 3 - 2 - 1

P Y T H O N

s = “PYTHON”

print(s[0:6]) # PYTHON (full string)

print(s[0:3]) # PYT (start 3-1)

print(s[1:5:2]) # YT (step 2)

print(s[::-1]) # NOHTYP (reverse string)

Cont.…..

3. Repetition: ❖ It means repeating a sequence.

❖ It use symbol “*”

Example:

s = “PYTHON”

s*2 #PYTHONPYTHON

String Operation Operation Example

len() Finds the length of a string str = “hello”

len(str) #

upper() Converts a string into capital letter str.upper # HELLO

Lower() Converts a string into small letter str.lower() #hello

Common string Operations

List

A list with different types of items

items = [1, 2, 'N', 'Go', 3.14]

❖ A collection of similar or different types of data items.

❖ A list is created by placing items inside [ ] separated by commas.

Example

A list with duplicate items

numbers = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4]

items:

index: 0 1 2 3 4 5 6 7 8 9

1 2 N Go 3.

number:

index:

Accessing Element of a List

  1. Single-dimensional List

➢ It is a list where elements are listed one after the other.

➢ Each element is allocated a unique number called index.

Examples: A list containing multiple of 5 up to 20

my_list = [5, 10, 15, 20]

my_list[0] = 5

my_list[2] = 15

my_list:

0 1 2 3

my_list[-1] = 20

my_list[-4] = 5

- 4 - 3 - 2 - 1

Positive Indexing

Negative Indexing

Adding Element to a list

➢ Element can be added to a list in different ways, following are the methods to

add element to a list.

✓ append()

✓ insert()

✓ extend()

  1. append() Method (^) It is a built-in method used to add an item at the end of a list.

SYNTAX: list.append( value )

Example: Language = ['c', 'cpp', 'Java’]

Language.append('Python’)

=> ['c', 'cpp', 'Java', 'Python']

Example: Language = ['c', 'cpp', 'Java’]

Language.append([‘Python’,’JavaScript’])

=> ['c', 'cpp', 'Java', [‘Python’,’JavaScript’]]

A list as an item can be added

Cont.…………..

  1. insert() Method A built-in method used to add an item at a specific position.

SYNTAX: list.insert(position, value)

Example: Language = ['c', 'cpp', 'Java’]

index: 0 1 2

Language.insert(0,'Python’)

=> ['Python','c', 'cpp', 'Java']

SYNTAX: (^) list1.extend(list2)

Example: Languages = ['c', 'cpp', 'Java’]

more_language = ['Python', 'Javascript’]

Languages.extend(more_language)

=> ['c', 'cpp', 'Java', 'Python', 'Javascript']

  1. extend() Method A built-in method used to add all items of one list in another list.

Cont.………

  1. del statement() Deletes an element at a specified index.

Example: a = [1, 2, 3]

del a[1]

print(a) => [1, 3]

  1. clear()

Removes all item.

Example: a = [1, 2, 3]

a.clear()

print(a) => []

Common List operations

List Function Operation Example

len() Finds length of a list

lst = [1, 2, 3]

len(lst) → 3

index()

Returns the index of the first

occurrence of a value

lst = [10, 20, 30]

lst.index(20) → 1

count()

Counts how many times a value

appears

lst = [1, 2, 2, 3]

lst.count(2) → 2

sort()

Sorts the list in ascending order

(default)

lst = [3, 1, 2]

lst.sort() → [1, 2, 3]

reverse() Reverses the order of the list

lst = [1, 2, 3]

lst.reverse() → [3, 2, 1]

copy() Returns a shallow copy of the list

lst = [1, 2, 3]

new_lst = lst.copy() → [1, 2, 3]

Accessing tuple items

➢ Each item of a tuple can be accessed through its index.

➢ Trying to access item out of the index range will result an IndexError.

t[0] = 1

t[3] = hello

my_list[-1] = 20

my_list[-4] = 5

Positive Indexing

Negative Indexing

t^1 2 3 Hello

Examples:

t = (1, 2, 3, "Hello")

index:

t^1 2 3 Hello

index:

SYNTAX

tuple_name[index]

Length of tuples

❖ Can be determined using the len() function.

Examples: t = (1, 2, 3, "Hello")

len(t) # 4

➢ Not possible to directly add items to a tuple as tuple are immutable.

➢ Workaround: convert a tuple into a list.

Adding Items to a Tuple

Examples:

t = (1, 2, 3, "Hello")

temp = list(t)

temp.append(‘hi’)

t = tuple(temp)

t = (1, 2, 3, "Hello“,’hi”)

➢ Not possible to directly remove items of a tuple as tuple are immutable.

➢ Workaround: convert a tuple into a list.

Removing Item of a Tuple

Examples:

t = (1, 2, 3, "Hello")

temp = list(t)

temp.remove( ‘Hello’)

print(t) # [1, 2, 3]

t = tuple(temp)

t = (1, 2, 3)

➢ Remove the entire tuple using del keyword.

Examples:

t = (1, 2, 3, "Hello")

del t

Common tuple operations

Tuple Function Operation Example

len() Finds length of a tuple

t = (1, 2, 3)

len(t) → 3

index()

Returns the index of the

first occurrence of a value

t = (10, 20, 30)

t.index(20) → 1

count()

Counts how many times a

value appears

t = (1, 2, 2, 3)

t.count(2) → 2

slicing Extracts part of the tuple

t = (1, 2, 3, 4)

t[1:3] → (2, 3)

immutability

Tuples cannot be changed

(no append/remove)

t = (1, 2, 3) → cannot

modify directly