Python Data Structures: Lists, Tuples, and Dictionaries, Lecture notes of Software Engineering

An outline and explanation of Python's built-in data structures: Lists, Tuples, and Dictionaries. Learn about their definition, declaration, access, methods, and examples. Understand the differences between ordered and unchangeable collections, and discover list comprehension for creating new lists based on existing ones.

Typology: Lecture notes

2021/2022

Uploaded on 11/04/2022

sangekari-shiva
sangekari-shiva 🇮🇳

5

(1)

5 documents

1 / 56

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Lists, Tuples and
Dictionaries
P.Raja,
Assistant professor in ES(D4),
School of Electronics and Electrical
Engineering
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
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38

Partial preview of the text

Download Python Data Structures: Lists, Tuples, and Dictionaries and more Lecture notes Software Engineering in PDF only on Docsity!

Lists, Tuples and

Dictionaries

P.Raja,

Assistant professor in ES(D4),

School of Electronics and Electrical

Engineering

Outline

  • (^) Definition, Declaration, accesses of elements,

slicing, methods, comprehension & examples.

  • (^) List
  • (^) Tuple
  • (^) Dictionary

List

  • (^) List Items - Data Types :-

list1 = ["apple", "banana", "cherry"]

list2 = [ 1 , 5 , 7 , 9 , 3 ]

list3 = [True, False, False]

list1 = ["abc", 34 , True, 40 , "male"]

  • (^) The list() Constructor :-

list =

list(("apple", "banana", "cherry")) # note

the double round-brackets

print(list)

List

  • (^) Python Collections (Arrays) :-
  • (^) List is a collection which is ordered and changeable.
  • (^) Tuple is a collection which is ordered and

unchangeable.

  • (^) Both list and tuple Allows duplicate members.
  • (^) Set is a collection which is unordered, unchangeable*,

and unindexed.

  • (^) Dictionary is a collection which is ordered** and

changeable.

  • (^) Both Set and Dictionary do not allow duplicate

members.

List

  • (^) Access Items :-
  • (^) **+, ***

List

  • (^) Check if Item Exists :-

list = ["apple", "banana", "cherry"]

if "apple" in list:

print("Yes, 'apple' is in the fruits list")

  • (^) Change Item Value :-

list = ["apple", "banana", "cherry"]

list[ 1 ] = “orange"

print(list)

  • (^) Change a Range of Item Value :-

list =

["apple", "banana", "cherry", "orange", "kiwi", "mango"]

list[ 1 : 3 ] = ["blackcurrant", "watermelon"]

print(list)

List

Ordered Vs Unordered

List- List/Array Methods

Method Description

append() Adds an element at the end of the list

clear() Removes all the elements from the list

copy() Returns a copy of the list

count() Returns the number of elements with the specified value

extend() Add the elements of a list (or any iterable), to the end of the current list

index() Returns the index of the first element with the specified value

insert() Adds an element at the specified position

pop() Removes the element at the specified position

remove() Removes the first item with the specified value

reverse() Reverses the order of the list

sort() Sorts the list

List- List/Array Methods

append() Method :-

list .append( elmnt )

List- List/Array Methods

copy() Method :-

list .copy()

List- List/Array Methods

count() Method :-

list .count( value )

List- List/Array Methods

index() Method :-

list .index( elmnt )

List- List/Array Methods

insert() Method :-

list .insert( pos , elmnt )