SlideShare a Scribd company logo
3
Most read
19
Most read
21
Most read
List And Tuples
Team Emertxe
List
List
Introduction

Used for storing different types of data unlike arrays
Example-1 student = [10, "Amar", 'M', 50, 55, 57, 67, 47]
Example-2 e_list = [] #Empty List

Indexing + Slicing can be applied on list
Example-1 print(student[1]) Gives "Amar"
Example-2 print(student[0: 3: 1])
Prints [10, "Amar", 'M']
Example-3 student[::] Print all elements
List
Examples
Example-1 #Create list with integer numbers
num = [10, 20, 30, 40, 50]
print(num)
print("num[0]: %dtnum[2]: %dn" % (num[0], num[2]))
Example-2 #Create list with strings
names = ["Ram", "Amar", "Thomas"]
print(names)
print("names[0]: %stnames[2]: %sn" % (names[0], names[2]))
Example-3 #Create list with different dtypes
x = [10, 20, 1.5, 6.7, "Ram", 'M']
print(x)
print("x[0]: %dtx[2]: %ftx[4]: %stx[5]: %cn" %(x[0], x[2], x[4], x[5]))
List
Creating list using range()
Example #Create list
num = list(range(4, 9, 2))
print(num)
List
Updating list
1 Creation lst = list(range(1, 5))
print(lst)
[1, 2, 3, 4]
2 append lst.append(9)
print(lst)
[1, 2, 3, 4, 9]
3 Update-1 lst[1] = 8
print(lst)
[1, 8, 3, 4, 9]
4 Update-2 lst[1: 3] = 10, 11
print(lst)
[1, 10, 11, 4, 9]
5 delete del lst[1]
print(lst)
[1, 11, 4, 9]
6 remove lst.remove(11)
print(lst)
[1, 4, 9]
7 reverse lst.reverse()
print(lst)
[9, 4, 1]
List
Concatenation of Two List
'+' operator is used to join two list
Example x = [10, 20, 30]
y = [5, 6, 7]
print(x + y)
List
Repetition of List
'*' is used to repeat the list 'n' times
Example x = [10, 20, 30]
print(x * 2)
List
Membership of List
'in' and 'not in' operators are used to check, whether an element belongs to the list
or not
Example x = [1, 2, 3, 4, 5]
a = 3
print(a in x)
Returns True, if the item is found
in the list
Example x = [1, 2, 3, 4, 5]
a = 7
print(a not in x)
Returns True, if the item is not
found in the list
List
Aliasing And Cloning Lists
Aliasing: Giving new name for the existing list
Example x = [10, 20, 30, 40]
y = x
Note: No separate memory will be allocated for y
Cloning / Copy: Making a copy
Example x = [10, 20, 30, 40]
y = x[:] <=> y = x.copy()
x[1] = 99
print(x)
print(y)
Note: Changes made in one list will not reflect other
List
Exercise
1. To find the maximum & minimum item in a list of items
2. Implement Bubble sort
3. To know how many times an element occurred in the list
4. To create employee list and search for the particular employee
List
To find the common items
#To find the common item in two lists
l1 = ["Thomas", "Richard", "Purdie", "Chris"]
l2 = ["Ram", "Amar", "Anthony", "Richard"]
#Covert them into sets
s1 = set(l1)
s2 = set(l2)
#Filter intersection of two sets
s3 = s1.intersection(s2)
#Convert back into the list
common = list(s3)
print(common)
List
Nested List
#To create a list with another list as element
list = [10, 20, 30, [80, 90]]
print(list)
List
List Comprehensions
Example-1: Create a list with squares of integers from 1 to 10
#Version-1
squares = []
for x in range(1, 11):
squares.append(x ** 2)
print(squares)
#Version-2
squares = []
squares = [x ** 2 for x in range(1, 11)]
print(squares)

List comprehensions represent creation of new lists from an iterable object(list, set,

tuple, dictionary or range) that satisfies a given condition
List
List Comprehensions
Example-2: Get squares of integers from 1 to 10 and take only the even numbers from the
result
even_squares = [x ** 2 for x in range(1, 11) if x % 2 == 0]
print(even_squares)

List comprehensions represent creation of new lists from an iterable object(list, set,

tuple, dictionary or range) that satisfies a given condition
List
List Comprehensions
Example-3: #Adding the elements of two list one by one
#Example-1
x = [10, 20, 30]
y = [1, 2, 3, 4]
lst = []
#Version-1
for i in x:
for j in y:
lst.append(i + j)
#Version-2
lst = [i + j for i in x for j in y]
#Example-2
lst = [i + j for i in "ABC" for j in "DE"]
print(lst)

List comprehensions represent creation of new lists from an iterable object(list, set,

tuple, dictionary or range) that satisfies a given condition
Tuple
Tuple
Introduction

A tuple is similar to list but it is immutable
Tuple
Creating Tuples
To create empty tuple
tup1 = ()
Tuple with one item
tup1 = (10, )
Tuple with different dtypes
tup3 = (10, 20, 1.1, 2.3, "Ram", 'M')
Tuple with no braces
t4 = 10, 20, 30, 40
Create tuple from the list
list = [10, 1.2, "Ram", 'M']
t5 = tuple(list)
Create tuple from range
t6 = tuple(range(4, 10, 2))
Tuple
Accessing Tuples

Accessing items in the tuple can be done by indexing or slicing method, similar to
that of list
Tuple
Basic Operations On Tuples
s = (10, "Ram", 10, 20, 30, 40, 50)
To find the length of the tuple
print(len(s))
Repetition operator
fee = (25.000, ) * 4
print(fee)
Concatenate the tuples using *
ns = s + fee
print(ns)
Membership
name = "Ram"
print(name in s)
Repetition
t1 = (1, 2, 3)
t2 = t1 * 3
print(t2)
Tuple
Functions To Process Tuples
len() len(tpl) Returns the number of elements in the tuple
min() min(tpl) Returns the smallest element in the tuple
max() max() Returns the biggest element in the tuple
count() tpl.count(x) Returns how many times the element ‘x’ is found in the tuple
index() tpl.index(x) Returns the first occurrence of the element ‘x’ in tpl.
Raises ValueError if ‘x’ is not found in the tuple
sorted() sorted(tpl) Sorts the elements of the tuple into ascending order.
sorted(tpl, reverse=True) will sort in reverse order
Tuple
Exercise
1. To accept elements in the form of a a tuple and display thier sum and average
2. To find the first occurrence of an element in a tuple
3. To sort a tuple with nested tuples
4. To insert a new item into a tuple at a specified location
5. To modify or replace an existing item of a tuple with new item
6. To delete an element from a particular position in the tuple
THANK YOU

More Related Content

PDF
Python programming : Arrays
PDF
Python programming : Strings
PPTX
List in Python
PDF
Tuples in Python
PPTX
Python Collections
PPTX
Unit 4 python -list methods
PPTX
Datastructures in python
PDF
Python tuples and Dictionary
Python programming : Arrays
Python programming : Strings
List in Python
Tuples in Python
Python Collections
Unit 4 python -list methods
Datastructures in python
Python tuples and Dictionary

What's hot (20)

PDF
Python list
PPTX
Linked list
PPTX
Merge sort algorithm
PDF
Python set
PPTX
Conditional and control statement
PPT
1.1 binary tree
PPTX
Python for loop
PDF
Python Variable Types, List, Tuple, Dictionary
PPTX
Python dictionary
PDF
Python lists &amp; sets
PDF
Python tuple
PPTX
Recursion
PDF
Operators in python
PDF
Introduction to Python
PPTX
File handling in Python
PDF
Strings in python
PDF
Python file handling
PDF
Datatypes in python
PPTX
Binary search
PPTX
Visualization and Matplotlib using Python.pptx
Python list
Linked list
Merge sort algorithm
Python set
Conditional and control statement
1.1 binary tree
Python for loop
Python Variable Types, List, Tuple, Dictionary
Python dictionary
Python lists &amp; sets
Python tuple
Recursion
Operators in python
Introduction to Python
File handling in Python
Strings in python
Python file handling
Datatypes in python
Binary search
Visualization and Matplotlib using Python.pptx
Ad

Similar to Python programming : List and tuples (20)

PPTX
UNIT-3 python and data structure alo.pptx
DOCX
Python Materials- Lists, Dictionary, Tuple
PDF
‘How to develop Pythonic coding rather than Python coding – Logic Perspective’
PPTX
tupple.pptx
PPTX
Python-List.pptx
PPTX
Lists on the pyhton to learn the children more easily on easy codes.pptx
PPTX
MODULE-2.pptx
PPTX
Python list tuple dictionary .pptx
PDF
PPTX
Python list tuple dictionary presentation
PPTX
List_tuple_dictionary.pptx
PDF
Python-Ukllllllllllllllllllllllllllllnit 2.pdklllllllf
PPTX
‏‏chap6 list tuples.pptx
PDF
GE3151_PSPP_UNIT_4_Notes
PPTX
List_tuple_dictionary.pptxList_tuple_dictionary.pptx
PPTX
Unit 4.pptx python list tuples dictionary
PPT
Programming in Python Lists and its methods .ppt
PDF
Python lecture 04
PDF
Module 2-Lists,Tuple and Dictionary Final by Dr.SV (1).pdf
UNIT-3 python and data structure alo.pptx
Python Materials- Lists, Dictionary, Tuple
‘How to develop Pythonic coding rather than Python coding – Logic Perspective’
tupple.pptx
Python-List.pptx
Lists on the pyhton to learn the children more easily on easy codes.pptx
MODULE-2.pptx
Python list tuple dictionary .pptx
Python list tuple dictionary presentation
List_tuple_dictionary.pptx
Python-Ukllllllllllllllllllllllllllllnit 2.pdklllllllf
‏‏chap6 list tuples.pptx
GE3151_PSPP_UNIT_4_Notes
List_tuple_dictionary.pptxList_tuple_dictionary.pptx
Unit 4.pptx python list tuples dictionary
Programming in Python Lists and its methods .ppt
Python lecture 04
Module 2-Lists,Tuple and Dictionary Final by Dr.SV (1).pdf
Ad

More from Emertxe Information Technologies Pvt Ltd (20)

Recently uploaded (20)

PDF
NewMind AI Monthly Chronicles - July 2025
PDF
KodekX | Application Modernization Development
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
A Presentation on Artificial Intelligence
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Empathic Computing: Creating Shared Understanding
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
Approach and Philosophy of On baking technology
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
NewMind AI Monthly Chronicles - July 2025
KodekX | Application Modernization Development
Agricultural_Statistics_at_a_Glance_2022_0.pdf
MYSQL Presentation for SQL database connectivity
Dropbox Q2 2025 Financial Results & Investor Presentation
Advanced methodologies resolving dimensionality complications for autism neur...
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Mobile App Security Testing_ A Comprehensive Guide.pdf
A Presentation on Artificial Intelligence
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Network Security Unit 5.pdf for BCA BBA.
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Empathic Computing: Creating Shared Understanding
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Approach and Philosophy of On baking technology
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
How UI/UX Design Impacts User Retention in Mobile Apps.pdf

Python programming : List and tuples

  • 3. List Introduction  Used for storing different types of data unlike arrays Example-1 student = [10, "Amar", 'M', 50, 55, 57, 67, 47] Example-2 e_list = [] #Empty List  Indexing + Slicing can be applied on list Example-1 print(student[1]) Gives "Amar" Example-2 print(student[0: 3: 1]) Prints [10, "Amar", 'M'] Example-3 student[::] Print all elements
  • 4. List Examples Example-1 #Create list with integer numbers num = [10, 20, 30, 40, 50] print(num) print("num[0]: %dtnum[2]: %dn" % (num[0], num[2])) Example-2 #Create list with strings names = ["Ram", "Amar", "Thomas"] print(names) print("names[0]: %stnames[2]: %sn" % (names[0], names[2])) Example-3 #Create list with different dtypes x = [10, 20, 1.5, 6.7, "Ram", 'M'] print(x) print("x[0]: %dtx[2]: %ftx[4]: %stx[5]: %cn" %(x[0], x[2], x[4], x[5]))
  • 5. List Creating list using range() Example #Create list num = list(range(4, 9, 2)) print(num)
  • 6. List Updating list 1 Creation lst = list(range(1, 5)) print(lst) [1, 2, 3, 4] 2 append lst.append(9) print(lst) [1, 2, 3, 4, 9] 3 Update-1 lst[1] = 8 print(lst) [1, 8, 3, 4, 9] 4 Update-2 lst[1: 3] = 10, 11 print(lst) [1, 10, 11, 4, 9] 5 delete del lst[1] print(lst) [1, 11, 4, 9] 6 remove lst.remove(11) print(lst) [1, 4, 9] 7 reverse lst.reverse() print(lst) [9, 4, 1]
  • 7. List Concatenation of Two List '+' operator is used to join two list Example x = [10, 20, 30] y = [5, 6, 7] print(x + y)
  • 8. List Repetition of List '*' is used to repeat the list 'n' times Example x = [10, 20, 30] print(x * 2)
  • 9. List Membership of List 'in' and 'not in' operators are used to check, whether an element belongs to the list or not Example x = [1, 2, 3, 4, 5] a = 3 print(a in x) Returns True, if the item is found in the list Example x = [1, 2, 3, 4, 5] a = 7 print(a not in x) Returns True, if the item is not found in the list
  • 10. List Aliasing And Cloning Lists Aliasing: Giving new name for the existing list Example x = [10, 20, 30, 40] y = x Note: No separate memory will be allocated for y Cloning / Copy: Making a copy Example x = [10, 20, 30, 40] y = x[:] <=> y = x.copy() x[1] = 99 print(x) print(y) Note: Changes made in one list will not reflect other
  • 11. List Exercise 1. To find the maximum & minimum item in a list of items 2. Implement Bubble sort 3. To know how many times an element occurred in the list 4. To create employee list and search for the particular employee
  • 12. List To find the common items #To find the common item in two lists l1 = ["Thomas", "Richard", "Purdie", "Chris"] l2 = ["Ram", "Amar", "Anthony", "Richard"] #Covert them into sets s1 = set(l1) s2 = set(l2) #Filter intersection of two sets s3 = s1.intersection(s2) #Convert back into the list common = list(s3) print(common)
  • 13. List Nested List #To create a list with another list as element list = [10, 20, 30, [80, 90]] print(list)
  • 14. List List Comprehensions Example-1: Create a list with squares of integers from 1 to 10 #Version-1 squares = [] for x in range(1, 11): squares.append(x ** 2) print(squares) #Version-2 squares = [] squares = [x ** 2 for x in range(1, 11)] print(squares)  List comprehensions represent creation of new lists from an iterable object(list, set,  tuple, dictionary or range) that satisfies a given condition
  • 15. List List Comprehensions Example-2: Get squares of integers from 1 to 10 and take only the even numbers from the result even_squares = [x ** 2 for x in range(1, 11) if x % 2 == 0] print(even_squares)  List comprehensions represent creation of new lists from an iterable object(list, set,  tuple, dictionary or range) that satisfies a given condition
  • 16. List List Comprehensions Example-3: #Adding the elements of two list one by one #Example-1 x = [10, 20, 30] y = [1, 2, 3, 4] lst = [] #Version-1 for i in x: for j in y: lst.append(i + j) #Version-2 lst = [i + j for i in x for j in y] #Example-2 lst = [i + j for i in "ABC" for j in "DE"] print(lst)  List comprehensions represent creation of new lists from an iterable object(list, set,  tuple, dictionary or range) that satisfies a given condition
  • 17. Tuple
  • 18. Tuple Introduction  A tuple is similar to list but it is immutable
  • 19. Tuple Creating Tuples To create empty tuple tup1 = () Tuple with one item tup1 = (10, ) Tuple with different dtypes tup3 = (10, 20, 1.1, 2.3, "Ram", 'M') Tuple with no braces t4 = 10, 20, 30, 40 Create tuple from the list list = [10, 1.2, "Ram", 'M'] t5 = tuple(list) Create tuple from range t6 = tuple(range(4, 10, 2))
  • 20. Tuple Accessing Tuples  Accessing items in the tuple can be done by indexing or slicing method, similar to that of list
  • 21. Tuple Basic Operations On Tuples s = (10, "Ram", 10, 20, 30, 40, 50) To find the length of the tuple print(len(s)) Repetition operator fee = (25.000, ) * 4 print(fee) Concatenate the tuples using * ns = s + fee print(ns) Membership name = "Ram" print(name in s) Repetition t1 = (1, 2, 3) t2 = t1 * 3 print(t2)
  • 22. Tuple Functions To Process Tuples len() len(tpl) Returns the number of elements in the tuple min() min(tpl) Returns the smallest element in the tuple max() max() Returns the biggest element in the tuple count() tpl.count(x) Returns how many times the element ‘x’ is found in the tuple index() tpl.index(x) Returns the first occurrence of the element ‘x’ in tpl. Raises ValueError if ‘x’ is not found in the tuple sorted() sorted(tpl) Sorts the elements of the tuple into ascending order. sorted(tpl, reverse=True) will sort in reverse order
  • 23. Tuple Exercise 1. To accept elements in the form of a a tuple and display thier sum and average 2. To find the first occurrence of an element in a tuple 3. To sort a tuple with nested tuples 4. To insert a new item into a tuple at a specified location 5. To modify or replace an existing item of a tuple with new item 6. To delete an element from a particular position in the tuple