0% found this document useful (0 votes)
21 views

Python Lists and Tuples

Uploaded by

XYZ
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Python Lists and Tuples

Uploaded by

XYZ
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

PYTHON

LISTS AND TUPLES


DATATYPES
• In Python there is no need to define the datatype of variables

• No need to declare variables before using them

• Datatypes are divided into : Immutable and Mutable

• Immutable: Tuples, Strings, Numbers (int, float, etc) (no double in Python)
• No need to specify the size of the string in Python
• Mutable: Lists, dictionaries, Sets
DATATYPES
• Mutable: we can modify the contents of a list. We can append, update or
delete the elements of a list depending upon our requirements.

• Immutable : We cannot modify the contents of a list.


Creating List using range() Function
range(start, stop, stepsize)
range(0,10,1)  generates numbers from0 to 9 at a step of 1
List Operations
Concatenation of Two Lists
Membership in
Lists
Aliasing and Cloning Lists
If the programmer wants two independent lists, he
should not go for aliasing, instead use cloning or
copying
The List Methods and their
Description
Method Example Description
append() list.append() Adds an element at the end of the list
clear() list.clear() Removes all the elements from the list
copy() list.copy() Returns a copy of the list
count() list.count(x) Returns number of occurrences of x in the list

extend() list.extend(list1) Append list1 to list

index() list.index(x) Returns the first occurrence of x in the list

insert() list.insert(i,x) Insert x to the list at position specified by i


pop() list.pop() Removes the element at the specified position

remove() list.remove(x) Removes the first item with the specified value

reverse() list.reverse() Reverses the order of the list


sort() list.sort() Sorts the list
Finding Biggest and Smallest
Elements in a List
Nested Lists
• A list within another list is called a nested list.
Nested Lists as Matrices
• Suppose we want to create a matrix with 3 rows and 3 columns, we should create a list with 2
other lists as:
Tuples
• A tuple is a Python sequence which stores a group of elements or items.
• Tuples are similar to lists but the main difference is tuples are
immutable whereas lists are mutable.

• Since tuples are immutable, once a tuple is created, we cannot modify


its elements.

• Hence, we cannot perform operations like append, extend, insert,


remove, pop and clear on tuples
Accessing the Tuple Elements
Basic Operations on Tuples
Functions to Process Tuples
Function Example Description
len() len(tpl) Returns the number of elements in the tuple
min() min(tpl) Returns the smallest element in the tuple
max() max(tpl) Returns the highest element in the tuple
count() tpl.count(x) Returns how many times the element ‘x’ is found in tpl
index() tpl.index(x) Returns the first occurrence of the element ‘x’ in tpl
sorted() sorted(tpl) Sorts the elements of the tuple into ascending order.
Sorted(tpl, reverse=True) will sort in reverse order

You might also like