Movatterモバイル変換


[0]ホーム

URL:


Python programming : List and tuples

The document provides an in-depth discussion on lists and tuples in Python, covering their creation, indexing, updating, and various operations such as concatenation and membership testing. It also introduces list comprehensions and basic tuple operations, including immutability and tuple functions. Additionally, the document includes exercises for practical applications to reinforce understanding of these data structures.

Embed presentation

Downloaded 268 times
List And TuplesTeam Emertxe
List
ListIntroductionUsed for storing different types of data unlike arraysExample-1 student = [10, "Amar", 'M', 50, 55, 57, 67, 47]Example-2 e_list = [] #Empty ListIndexing + Slicing can be applied on listExample-1 print(student[1]) Gives "Amar"Example-2 print(student[0: 3: 1])Prints [10, "Amar", 'M']Example-3 student[::] Print all elements
ListExamplesExample-1 #Create list with integer numbersnum = [10, 20, 30, 40, 50]print(num)print("num[0]: %dtnum[2]: %dn" % (num[0], num[2]))Example-2 #Create list with stringsnames = ["Ram", "Amar", "Thomas"]print(names)print("names[0]: %stnames[2]: %sn" % (names[0], names[2]))Example-3 #Create list with different dtypesx = [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]))
ListCreating list using range()Example #Create listnum = list(range(4, 9, 2))print(num)
ListUpdating list1 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] = 8print(lst)[1, 8, 3, 4, 9]4 Update-2 lst[1: 3] = 10, 11print(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]
ListConcatenation of Two List'+' operator is used to join two listExample x = [10, 20, 30]y = [5, 6, 7]print(x + y)
ListRepetition of List'*' is used to repeat the list 'n' timesExample x = [10, 20, 30]print(x * 2)
ListMembership of List'in' and 'not in' operators are used to check, whether an element belongs to the listor notExample x = [1, 2, 3, 4, 5]a = 3print(a in x)Returns True, if the item is foundin the listExample x = [1, 2, 3, 4, 5]a = 7print(a not in x)Returns True, if the item is notfound in the list
ListAliasing And Cloning ListsAliasing: Giving new name for the existing listExample x = [10, 20, 30, 40]y = xNote: No separate memory will be allocated for yCloning / Copy: Making a copyExample x = [10, 20, 30, 40]y = x[:] <=> y = x.copy()x[1] = 99print(x)print(y)Note: Changes made in one list will not reflect other
ListExercise1. To find the maximum & minimum item in a list of items2. Implement Bubble sort3. To know how many times an element occurred in the list4. To create employee list and search for the particular employee
ListTo find the common items#To find the common item in two listsl1 = ["Thomas", "Richard", "Purdie", "Chris"]l2 = ["Ram", "Amar", "Anthony", "Richard"]#Covert them into setss1 = set(l1)s2 = set(l2)#Filter intersection of two setss3 = s1.intersection(s2)#Convert back into the listcommon = list(s3)print(common)
ListNested List#To create a list with another list as elementlist = [10, 20, 30, [80, 90]]print(list)
ListList ComprehensionsExample-1: Create a list with squares of integers from 1 to 10#Version-1squares = []for x in range(1, 11):squares.append(x ** 2)print(squares)#Version-2squares = []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
ListList ComprehensionsExample-2: Get squares of integers from 1 to 10 and take only the even numbers from theresulteven_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
ListList ComprehensionsExample-3: #Adding the elements of two list one by one#Example-1x = [10, 20, 30]y = [1, 2, 3, 4]lst = []#Version-1for i in x:for j in y:lst.append(i + j)#Version-2lst = [i + j for i in x for j in y]#Example-2lst = [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
TupleIntroductionA tuple is similar to list but it is immutable
TupleCreating TuplesTo create empty tupletup1 = ()Tuple with one itemtup1 = (10, )Tuple with different dtypestup3 = (10, 20, 1.1, 2.3, "Ram", 'M')Tuple with no bracest4 = 10, 20, 30, 40Create tuple from the listlist = [10, 1.2, "Ram", 'M']t5 = tuple(list)Create tuple from ranget6 = tuple(range(4, 10, 2))
TupleAccessing TuplesAccessing items in the tuple can be done by indexing or slicing method, similar tothat of list
TupleBasic Operations On Tupless = (10, "Ram", 10, 20, 30, 40, 50)To find the length of the tupleprint(len(s))Repetition operatorfee = (25.000, ) * 4print(fee)Concatenate the tuples using *ns = s + feeprint(ns)Membershipname = "Ram"print(name in s)Repetitiont1 = (1, 2, 3)t2 = t1 * 3print(t2)
TupleFunctions To Process Tupleslen() len(tpl) Returns the number of elements in the tuplemin() min(tpl) Returns the smallest element in the tuplemax() max() Returns the biggest element in the tuplecount() tpl.count(x) Returns how many times the element ‘x’ is found in the tupleindex() tpl.index(x) Returns the first occurrence of the element ‘x’ in tpl.Raises ValueError if ‘x’ is not found in the tuplesorted() sorted(tpl) Sorts the elements of the tuple into ascending order.sorted(tpl, reverse=True) will sort in reverse order
TupleExercise1. To accept elements in the form of a a tuple and display thier sum and average2. To find the first occurrence of an element in a tuple3. To sort a tuple with nested tuples4. To insert a new item into a tuple at a specified location5. To modify or replace an existing item of a tuple with new item6. To delete an element from a particular position in the tuple
THANK YOU

Recommended

PDF
Python programming : Arrays
PDF
Python programming : Strings
PPTX
List in Python
PPTX
Python list
PDF
Python tuples and Dictionary
PPTX
Unit 4 python -list methods
PPT
Python List.ppt
PDF
Python list
PDF
Arrays in python
PDF
Python Variable Types, List, Tuple, Dictionary
PDF
List,tuple,dictionary
PPTX
List in Python
PDF
Tuples in Python
PDF
Python programming : Files
PPTX
sorting and its types
PPT
Python Pandas
PPSX
Modules and packages in python
PDF
Datatypes in python
PDF
Strings in python
PPT
Data Structure and Algorithms Linked List
PPTX
Collision in Hashing.pptx
PPTX
Doubly linked list (animated)
PDF
Set methods in python
PPT
Data Structures- Part5 recursion
PPTX
Python array
PPTX
UNIT-3 python and data structure alo.pptx
DOCX
Python Materials- Lists, Dictionary, Tuple

More Related Content

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

What's hot

PPT
Python List.ppt
PDF
Python list
PDF
Arrays in python
PDF
Python Variable Types, List, Tuple, Dictionary
PDF
List,tuple,dictionary
PPTX
List in Python
PDF
Tuples in Python
PDF
Python programming : Files
PPTX
sorting and its types
PPT
Python Pandas
PPSX
Modules and packages in python
PDF
Datatypes in python
PDF
Strings in python
PPT
Data Structure and Algorithms Linked List
PPTX
Collision in Hashing.pptx
PPTX
Doubly linked list (animated)
PDF
Set methods in python
PPT
Data Structures- Part5 recursion
PPTX
Python array
Python List.ppt
Python list
Arrays in python
Python Variable Types, List, Tuple, Dictionary
List,tuple,dictionary
List in Python
Tuples in Python
Python programming : Files
sorting and its types
Python Pandas
Modules and packages in python
Datatypes in python
Strings in python
Data Structure and Algorithms Linked List
Collision in Hashing.pptx
Doubly linked list (animated)
Set methods in python
Data Structures- Part5 recursion
Python array

Similar to Python programming : List and tuples

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, Operations performed on list
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
Python list
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
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, Operations performed on list
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
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

More from Emertxe Information Technologies Pvt Ltd

PDF
PDF
Career Transition (1).pdf
PDF
PDF
PDF
Career Transition (1).pdf

Recently uploaded

PPTX
Leon Brands - Intro to GPU Occlusion (Graphics Programming Conference 2024)
PDF
[BDD 2025 - Full-Stack Development] Agentic AI Architecture: Redefining Syste...
PDF
Cybersecurity Prevention and Detection: Unit 2
PDF
[DevFest Strasbourg 2025] - NodeJs Can do that !!
PDF
"DISC as GPS for team leaders: how to lead a team from storming to performing...
 
PDF
PCCC25(設立25年記念PCクラスタシンポジウム):エヌビディア合同会社 テーマ2「NVIDIA BlueField-4 DPU」
PDF
Mastering UiPath Maestro – Session 2 – Building a Live Use Case - Session 2
PPTX
"Feelings versus facts: why metrics are more important than intuition", Igor ...
 
PPTX
UFCD 0797 - SISTEMAS OPERATIVOS_Unidade Completa.pptx
PDF
Agentic Intro and Hands-on: Build your first Coded Agent
PDF
MuleSoft Meetup: Dreamforce'25 Tour- Vibing With AI & Agents.pdf
PPTX
Guardrails in Action - Ensuring Safe AI with Azure AI Content Safety.pptx
PDF
Parallel Computing BCS702 Module notes of the vtu college 7th sem 4.pdf
PDF
Lets Build a Serverless Function with Kiro
PDF
The Necessity of Digital Forensics, the Digital Forensics Process & Laborator...
PDF
Transforming Content Operations in the Age of AI
PPTX
kernel PPT (Explanation of Windows Kernal).pptx
PDF
[BDD 2025 - Full-Stack Development] PHP in AI Age: The Laravel Way. (Rizqy Hi...
PPTX
Connecting the unconnectable: Exploring LoRaWAN for IoT
PDF
[BDD 2025 - Artificial Intelligence] AI for the Underdogs: Innovation for Sma...
Leon Brands - Intro to GPU Occlusion (Graphics Programming Conference 2024)
[BDD 2025 - Full-Stack Development] Agentic AI Architecture: Redefining Syste...
Cybersecurity Prevention and Detection: Unit 2
[DevFest Strasbourg 2025] - NodeJs Can do that !!
"DISC as GPS for team leaders: how to lead a team from storming to performing...
 
PCCC25(設立25年記念PCクラスタシンポジウム):エヌビディア合同会社 テーマ2「NVIDIA BlueField-4 DPU」
Mastering UiPath Maestro – Session 2 – Building a Live Use Case - Session 2
"Feelings versus facts: why metrics are more important than intuition", Igor ...
 
UFCD 0797 - SISTEMAS OPERATIVOS_Unidade Completa.pptx
Agentic Intro and Hands-on: Build your first Coded Agent
MuleSoft Meetup: Dreamforce'25 Tour- Vibing With AI & Agents.pdf
Guardrails in Action - Ensuring Safe AI with Azure AI Content Safety.pptx
Parallel Computing BCS702 Module notes of the vtu college 7th sem 4.pdf
Lets Build a Serverless Function with Kiro
The Necessity of Digital Forensics, the Digital Forensics Process & Laborator...
Transforming Content Operations in the Age of AI
kernel PPT (Explanation of Windows Kernal).pptx
[BDD 2025 - Full-Stack Development] PHP in AI Age: The Laravel Way. (Rizqy Hi...
Connecting the unconnectable: Exploring LoRaWAN for IoT
[BDD 2025 - Artificial Intelligence] AI for the Underdogs: Innovation for Sma...
In this document
Powered by AI

Introduction to collections in Python, focusing on Lists and Tuples.

Detailed explanation of Lists including creation, indexing, types, concatenation, repetition, and membership.

Operations on lists such as updating, deleting, cloning, and aliasing.

Practical exercises for finding maximum/minimum items, common elements, and sorting.

Introduction to Nested Lists and List Comprehensions for efficient list creation.

Basic introduction to Tuples, highlighting their immutability and similarities to Lists.

Creating, accessing, performing operations, and processing functions on Tuples.

Closing remarks and acknowledgments.

Python programming : List and tuples

  • 1.
  • 2.
  • 3.
    ListIntroductionUsed for storingdifferent types of data unlike arraysExample-1 student = [10, "Amar", 'M', 50, 55, 57, 67, 47]Example-2 e_list = [] #Empty ListIndexing + Slicing can be applied on listExample-1 print(student[1]) Gives "Amar"Example-2 print(student[0: 3: 1])Prints [10, "Amar", 'M']Example-3 student[::] Print all elements
  • 4.
    ListExamplesExample-1 #Create listwith integer numbersnum = [10, 20, 30, 40, 50]print(num)print("num[0]: %dtnum[2]: %dn" % (num[0], num[2]))Example-2 #Create list with stringsnames = ["Ram", "Amar", "Thomas"]print(names)print("names[0]: %stnames[2]: %sn" % (names[0], names[2]))Example-3 #Create list with different dtypesx = [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.
    ListCreating list usingrange()Example #Create listnum = list(range(4, 9, 2))print(num)
  • 6.
    ListUpdating list1 Creationlst = 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] = 8print(lst)[1, 8, 3, 4, 9]4 Update-2 lst[1: 3] = 10, 11print(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.
    ListConcatenation of TwoList'+' operator is used to join two listExample x = [10, 20, 30]y = [5, 6, 7]print(x + y)
  • 8.
    ListRepetition of List'*'is used to repeat the list 'n' timesExample x = [10, 20, 30]print(x * 2)
  • 9.
    ListMembership of List'in'and 'not in' operators are used to check, whether an element belongs to the listor notExample x = [1, 2, 3, 4, 5]a = 3print(a in x)Returns True, if the item is foundin the listExample x = [1, 2, 3, 4, 5]a = 7print(a not in x)Returns True, if the item is notfound in the list
  • 10.
    ListAliasing And CloningListsAliasing: Giving new name for the existing listExample x = [10, 20, 30, 40]y = xNote: No separate memory will be allocated for yCloning / Copy: Making a copyExample x = [10, 20, 30, 40]y = x[:] <=> y = x.copy()x[1] = 99print(x)print(y)Note: Changes made in one list will not reflect other
  • 11.
    ListExercise1. To findthe maximum & minimum item in a list of items2. Implement Bubble sort3. To know how many times an element occurred in the list4. To create employee list and search for the particular employee
  • 12.
    ListTo find thecommon items#To find the common item in two listsl1 = ["Thomas", "Richard", "Purdie", "Chris"]l2 = ["Ram", "Amar", "Anthony", "Richard"]#Covert them into setss1 = set(l1)s2 = set(l2)#Filter intersection of two setss3 = s1.intersection(s2)#Convert back into the listcommon = list(s3)print(common)
  • 13.
    ListNested List#To createa list with another list as elementlist = [10, 20, 30, [80, 90]]print(list)
  • 14.
    ListList ComprehensionsExample-1: Createa list with squares of integers from 1 to 10#Version-1squares = []for x in range(1, 11):squares.append(x ** 2)print(squares)#Version-2squares = []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.
    ListList ComprehensionsExample-2: Getsquares of integers from 1 to 10 and take only the even numbers from theresulteven_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.
    ListList ComprehensionsExample-3: #Addingthe elements of two list one by one#Example-1x = [10, 20, 30]y = [1, 2, 3, 4]lst = []#Version-1for i in x:for j in y:lst.append(i + j)#Version-2lst = [i + j for i in x for j in y]#Example-2lst = [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.
  • 18.
    TupleIntroductionA tuple issimilar to list but it is immutable
  • 19.
    TupleCreating TuplesTo createempty tupletup1 = ()Tuple with one itemtup1 = (10, )Tuple with different dtypestup3 = (10, 20, 1.1, 2.3, "Ram", 'M')Tuple with no bracest4 = 10, 20, 30, 40Create tuple from the listlist = [10, 1.2, "Ram", 'M']t5 = tuple(list)Create tuple from ranget6 = tuple(range(4, 10, 2))
  • 20.
    TupleAccessing TuplesAccessing itemsin the tuple can be done by indexing or slicing method, similar tothat of list
  • 21.
    TupleBasic Operations OnTupless = (10, "Ram", 10, 20, 30, 40, 50)To find the length of the tupleprint(len(s))Repetition operatorfee = (25.000, ) * 4print(fee)Concatenate the tuples using *ns = s + feeprint(ns)Membershipname = "Ram"print(name in s)Repetitiont1 = (1, 2, 3)t2 = t1 * 3print(t2)
  • 22.
    TupleFunctions To ProcessTupleslen() len(tpl) Returns the number of elements in the tuplemin() min(tpl) Returns the smallest element in the tuplemax() max() Returns the biggest element in the tuplecount() tpl.count(x) Returns how many times the element ‘x’ is found in the tupleindex() tpl.index(x) Returns the first occurrence of the element ‘x’ in tpl.Raises ValueError if ‘x’ is not found in the tuplesorted() sorted(tpl) Sorts the elements of the tuple into ascending order.sorted(tpl, reverse=True) will sort in reverse order
  • 23.
    TupleExercise1. To acceptelements in the form of a a tuple and display thier sum and average2. To find the first occurrence of an element in a tuple3. To sort a tuple with nested tuples4. To insert a new item into a tuple at a specified location5. To modify or replace an existing item of a tuple with new item6. To delete an element from a particular position in the tuple
  • 24.

[8]ページ先頭

©2009-2025 Movatter.jp