SlideShare a Scribd company logo
PYTHON PROGRAMMING
INFORMATION TECHNOLOGY
I SEMESTER
Prepared by:
Mrs. K Laxminarayanamma, Assistant Professor
2
Course Title PYTHON PROGRAMMING
Course Code ACSC01
Class B.Tech I Semester
Section IT A AND B
Name of the Faculty Mrs. K.Laxminarayanamma
Date 22 January, 2021
Course Outcome/s
1. Understand native data types like list, set, tuple,
dictionary use them in data processing applications
Topic Covered
1. Tuple comprehension, Conversion of List
comprehension to Tuple
2. Iterators and Iterables
3
MODULE - III
INTRODUCTION TO PYTHON
CONTAINER DATA TYPES
Lists: Accessing List elements, List operations, List methods, List comprehension;
Tuples: Accessing Tuple elements, Tuple operations, Tuple methods, Tuple
comprehension, Conversion of List comprehension to Tuple, Iterators and
Iterables, zip() function.
Sets: Accessing Set elements, Set operations, Set functions, Set comprehension;
Dictionaries: Accessing Dictionary elements, Dictionary operations, Dictionary
Functions, Nested Dictionary, Dictionary comprehension.
Syllabus
4
There are four collection data types in the Python programming language:
List is a collection which is ordered and changeable. Allows duplicate members.
Tuple is a collection which is ordered and unchangeable. Allows duplicate
members.
Set is a collection which is unordered and unindexed. No duplicate members.
Dictionary is a collection which is unordered, changeable and indexed. No
duplicate members.
Python Collections (Arrays)
5
Tuple comprehension
Tuple comprehension
Example :
T=(-1,2,-3,4,6)
T=tuple(i for i in T if i>0)
Output:
(2,4,6)
List comprehension
Example:
L=[-1,2,-3,4,6]
L2=[i for i in L if i>0]
Output:
[2,4,6]
6
Conversion of List comprehension to Tuple
Example
L=[1,2,3,4,5,6,7]
T=tuple([i for i in L])
Print(T)
Output:
(1,2,3,4,5,6,7)
7
List vs Tuple
List Tuple
1 The literal syntax of list is shown by the
[ ].
The literal syntax of the tuple is
shown by the ().
2 The List is mutable. The tuple is immutable.
3 List iteration is slower and is time
consuming.
Tuple iteration is faster.
4
Lists consume more memory
Tuple consume less memory as
compared to the list
5 The list is used in the scenario in which
we need to store the simple
collections with no constraints where
the value of the items can be changed.
The tuple is used in the cases where
we need to store the read-only
collections i.e., the value of the
items cannot be changed. It can be
used as the key inside the
dictionary.
6 List provides many in-built methods. Tuples have less in-built methods.
8
Python Iterators
An iterator is an object that contains a countable number of values.
An iterator is an object that can be iterated upon, meaning that you can traverse
through all the values.
Technically, in Python, an iterator is an object which implements the iterator
protocol, which consist of the methods __iter__() and __next__().
9
Iterator
In Python, an iterator is an object which implements the iterator
protocol, which consist of the methods __iter__() and
__next__() .
• An iterator is an object representing a stream of data.
• It returns the data one element at a time.
• A Python iterator must support a method called __next__() that
takes no arguments and always returns the next element of the
stream.
• If there are no more elements in the stream, __next__() must
raise the StopIteration exception.
• Iterators don’t have to be finite. It’s perfectly reasonable to
write an iterator that produces an infinite stream of data.
10
Iterable
• In Python, Iterable is anything you can loop over with a for loop.
• An object is called an iterable if u can get an iterator out of it.
• Calling iter() function on an iterable gives us an iterator.
• Calling next() function on iterator gives us the next element.
• If the iterator is exhausted(if it has no more elements), calling next() raises
StopIteration exception.
11
Iterable
12
Iterating through an Iterator
• We use the next() function to manually iterate through all the
items of an iterator.
• When we reach the end and there is no more data to be
returned, it will raise the StopIteration Exception.
# define a list
my_list = [4, 7, 0, 3]
# get an iterator using iter()
my_iter = iter(my_list)
# iterate through it using next()
# Output: 4
print(next(my_iter))
13
Iterating through an Iterator
# Output: 7
print(next(my_iter))
# next(obj) is same as
obj.__next__()
# Output: 0
print(my_iter.__next__())
# Output: 3
print(my_iter.__next__())
# This will raise error, no items left
next(my_iter)
14
Python Iterators
Iterator vs Iterable:
Lists, tuples, dictionaries, and sets are all iterable objects. They are
iterable containers which you can get an iterator from.
All these objects have a iter() method which is used to get an iterator:
Example
Return an iterator from a tuple, and print each value:
mytuple = ("apple", "banana", "cherry")
myit = iter(mytuple)
print(next(myit))
print(next(myit))
print(next(myit))
Output:
apple
banana
cherry
15
Python Iterators
Iterator vs Iterable:
Even strings are iterable objects, and can return an iterator:
Example
Strings are also iterable objects, containing a sequence of characters:
mystr = "banana"
myit = iter(mystr)
print(next(myit))
print(next(myit))
print(next(myit))
print(next(myit))
print(next(myit))
print(next(myit))
Output:
b
a
n
a
n
a
16
Python Iterators
Looping Through an Iterator
We can also use a for loop to iterate through an iterable object:
Example
Iterate the values of a tuple:
mytuple = ("apple", "banana", "cherry")
for x in mytuple:
print(x)
Output:
apple
banana
cherry
17
Python Iterators
Example
Iterate the characters of a string:
mystr = "banana"
for x in mystr:
print(x)
Output:
b
a
n
a
n
a
18
Working of for loop for Iterators
for <var> in <iterable>:
<statement(s)>
19
Python Sets
Sets are used to store multiple items in a single variable.
Set is one of 4 built-in data types in Python used to store collections of data, the
other 3 are List, Tuple, and Dictionary, all with different qualities and usage.
A set is a collection which is both unordered and unindexed.
Sets are written with curly brackets.
Example
Create a Set:
thisset = {"apple", "banana", "cherry"}
print(thisset)
output:
{'apple', 'banana', 'cherry'}
20
Set Items
Set items are unordered, unchangeable, and do not allow duplicate values.
Unordered
Unordered means that the items in a set do not have a defined order.
Set items can appear in a different order every time you use them, and cannot be
referred to by index or key.
Unchangeable
Sets are unchangeable, meaning that we cannot change the items after the set
has been created.
Once a set is created, you cannot change its items, but you can add new items.
Duplicates Not Allowed
Sets cannot have two items with the same value.
21
Set Items
Example
Duplicate values will be ignored:
thisset = {"apple", "banana", "cherry", "apple"}
print(thisset)
Output:
{'banana', 'cherry', 'apple'}
22
Python Dictionaries
Dictionaries are used to store data values in key:value pairs.
A dictionary is a collection which is unordered, changeable and does not allow
duplicates.
Dictionaries are written with curly brackets, and have keys and values:
Example
Create and print a dictionary:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(thisdict)
Output:
{'brand': 'Ford', 'model': 'Mustang', 'year': 1964}
23
Dictionary Items
Dictionary items are unordered, changeable, and does not allow duplicates.
Dictionary items are presented in key:value pairs, and can be referred to by
using the key name.
Example
Print the "brand" value of the dictionary:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(thisdict["brand"])
Output:
Ford

More Related Content

Similar to set.pptx

RANDOMISATION-NUMERICAL METHODS FOR ENGINEERING.pptx
RANDOMISATION-NUMERICAL METHODS  FOR ENGINEERING.pptxRANDOMISATION-NUMERICAL METHODS  FOR ENGINEERING.pptx
RANDOMISATION-NUMERICAL METHODS FOR ENGINEERING.pptx
Out Cast
 
Phython presentation
Phython presentationPhython presentation
Phython presentation
karanThakur305665
 
Lecture 4 - Object Interaction and Collections
Lecture 4 - Object Interaction and CollectionsLecture 4 - Object Interaction and Collections
Lecture 4 - Object Interaction and Collections
Syed Afaq Shah MACS CP
 
Python For Data Science.pptx
Python For Data Science.pptxPython For Data Science.pptx
Python For Data Science.pptx
rohithprabhas1
 
11 Introduction to lists.pptx
11 Introduction to lists.pptx11 Introduction to lists.pptx
11 Introduction to lists.pptx
ssuser8e50d8
 
Mastering Python lesson 5a_lists_list_operations
Mastering Python lesson 5a_lists_list_operationsMastering Python lesson 5a_lists_list_operations
Mastering Python lesson 5a_lists_list_operations
Ruth Marvin
 
COMPUTER SCIENCE SUPPORT MATERIAL CLASS 12.pdf
COMPUTER SCIENCE SUPPORT MATERIAL CLASS 12.pdfCOMPUTER SCIENCE SUPPORT MATERIAL CLASS 12.pdf
COMPUTER SCIENCE SUPPORT MATERIAL CLASS 12.pdf
rajkumar2792005
 
13- Data and Its Types presentation kafss
13- Data and Its Types presentation kafss13- Data and Its Types presentation kafss
13- Data and Its Types presentation kafss
AliKhokhar33
 
Stacks in Python will be made using lists.pdf
Stacks in Python will be made using lists.pdfStacks in Python will be made using lists.pdf
Stacks in Python will be made using lists.pdf
DeeptiMalhotra19
 
Arrays Introduction.pptx
Arrays Introduction.pptxArrays Introduction.pptx
Arrays Introduction.pptx
AtheenaNugent1
 
Python iteration
Python iterationPython iteration
Python iterationdietbuddha
 
numpy.pdf
numpy.pdfnumpy.pdf
python interview prep question , 52 questions
python interview prep question , 52 questionspython interview prep question , 52 questions
python interview prep question , 52 questions
gokul174578
 
Data structures in c#
Data structures in c#Data structures in c#
Data structures in c#
SivaSankar Gorantla
 
File handling in pythan.pptx
File handling in pythan.pptxFile handling in pythan.pptx
File handling in pythan.pptx
NawalKishore38
 
Basic data structures in python
Basic data structures in pythonBasic data structures in python
Basic data structures in python
Celine George
 
ProgFund_Lecture_3_Data_Structures_and_Iteration-1.pdf
ProgFund_Lecture_3_Data_Structures_and_Iteration-1.pdfProgFund_Lecture_3_Data_Structures_and_Iteration-1.pdf
ProgFund_Lecture_3_Data_Structures_and_Iteration-1.pdf
lailoesakhan
 
Data structures
Data structuresData structures
Data structures
naveeth babu
 
unit 5 stack & queue.ppt
unit 5 stack & queue.pptunit 5 stack & queue.ppt
unit 5 stack & queue.ppt
SeethaDinesh
 
LPR - Week 1
LPR - Week 1LPR - Week 1
LPR - Week 1
FaridRomadhana
 

Similar to set.pptx (20)

RANDOMISATION-NUMERICAL METHODS FOR ENGINEERING.pptx
RANDOMISATION-NUMERICAL METHODS  FOR ENGINEERING.pptxRANDOMISATION-NUMERICAL METHODS  FOR ENGINEERING.pptx
RANDOMISATION-NUMERICAL METHODS FOR ENGINEERING.pptx
 
Phython presentation
Phython presentationPhython presentation
Phython presentation
 
Lecture 4 - Object Interaction and Collections
Lecture 4 - Object Interaction and CollectionsLecture 4 - Object Interaction and Collections
Lecture 4 - Object Interaction and Collections
 
Python For Data Science.pptx
Python For Data Science.pptxPython For Data Science.pptx
Python For Data Science.pptx
 
11 Introduction to lists.pptx
11 Introduction to lists.pptx11 Introduction to lists.pptx
11 Introduction to lists.pptx
 
Mastering Python lesson 5a_lists_list_operations
Mastering Python lesson 5a_lists_list_operationsMastering Python lesson 5a_lists_list_operations
Mastering Python lesson 5a_lists_list_operations
 
COMPUTER SCIENCE SUPPORT MATERIAL CLASS 12.pdf
COMPUTER SCIENCE SUPPORT MATERIAL CLASS 12.pdfCOMPUTER SCIENCE SUPPORT MATERIAL CLASS 12.pdf
COMPUTER SCIENCE SUPPORT MATERIAL CLASS 12.pdf
 
13- Data and Its Types presentation kafss
13- Data and Its Types presentation kafss13- Data and Its Types presentation kafss
13- Data and Its Types presentation kafss
 
Stacks in Python will be made using lists.pdf
Stacks in Python will be made using lists.pdfStacks in Python will be made using lists.pdf
Stacks in Python will be made using lists.pdf
 
Arrays Introduction.pptx
Arrays Introduction.pptxArrays Introduction.pptx
Arrays Introduction.pptx
 
Python iteration
Python iterationPython iteration
Python iteration
 
numpy.pdf
numpy.pdfnumpy.pdf
numpy.pdf
 
python interview prep question , 52 questions
python interview prep question , 52 questionspython interview prep question , 52 questions
python interview prep question , 52 questions
 
Data structures in c#
Data structures in c#Data structures in c#
Data structures in c#
 
File handling in pythan.pptx
File handling in pythan.pptxFile handling in pythan.pptx
File handling in pythan.pptx
 
Basic data structures in python
Basic data structures in pythonBasic data structures in python
Basic data structures in python
 
ProgFund_Lecture_3_Data_Structures_and_Iteration-1.pdf
ProgFund_Lecture_3_Data_Structures_and_Iteration-1.pdfProgFund_Lecture_3_Data_Structures_and_Iteration-1.pdf
ProgFund_Lecture_3_Data_Structures_and_Iteration-1.pdf
 
Data structures
Data structuresData structures
Data structures
 
unit 5 stack & queue.ppt
unit 5 stack & queue.pptunit 5 stack & queue.ppt
unit 5 stack & queue.ppt
 
LPR - Week 1
LPR - Week 1LPR - Week 1
LPR - Week 1
 

More from satyabratPanda2

643a4b39c9460_ppt.pptx
643a4b39c9460_ppt.pptx643a4b39c9460_ppt.pptx
643a4b39c9460_ppt.pptx
satyabratPanda2
 
strings.ppt
strings.pptstrings.ppt
strings.ppt
satyabratPanda2
 
6463576039739_ppt.pptx
6463576039739_ppt.pptx6463576039739_ppt.pptx
6463576039739_ppt.pptx
satyabratPanda2
 
643e6b4762473_ppt.pptx
643e6b4762473_ppt.pptx643e6b4762473_ppt.pptx
643e6b4762473_ppt.pptx
satyabratPanda2
 
CHEMISTRY MODULE-5.pdf
CHEMISTRY MODULE-5.pdfCHEMISTRY MODULE-5.pdf
CHEMISTRY MODULE-5.pdf
satyabratPanda2
 
WEEK-3 EXEED.pdf
WEEK-3 EXEED.pdfWEEK-3 EXEED.pdf
WEEK-3 EXEED.pdf
satyabratPanda2
 
intro22.ppt
intro22.pptintro22.ppt
intro22.ppt
satyabratPanda2
 
Customer Journey Map.pptx
Customer Journey Map.pptxCustomer Journey Map.pptx
Customer Journey Map.pptx
satyabratPanda2
 

More from satyabratPanda2 (8)

643a4b39c9460_ppt.pptx
643a4b39c9460_ppt.pptx643a4b39c9460_ppt.pptx
643a4b39c9460_ppt.pptx
 
strings.ppt
strings.pptstrings.ppt
strings.ppt
 
6463576039739_ppt.pptx
6463576039739_ppt.pptx6463576039739_ppt.pptx
6463576039739_ppt.pptx
 
643e6b4762473_ppt.pptx
643e6b4762473_ppt.pptx643e6b4762473_ppt.pptx
643e6b4762473_ppt.pptx
 
CHEMISTRY MODULE-5.pdf
CHEMISTRY MODULE-5.pdfCHEMISTRY MODULE-5.pdf
CHEMISTRY MODULE-5.pdf
 
WEEK-3 EXEED.pdf
WEEK-3 EXEED.pdfWEEK-3 EXEED.pdf
WEEK-3 EXEED.pdf
 
intro22.ppt
intro22.pptintro22.ppt
intro22.ppt
 
Customer Journey Map.pptx
Customer Journey Map.pptxCustomer Journey Map.pptx
Customer Journey Map.pptx
 

Recently uploaded

Bitcoin Lightning wallet and tic-tac-toe game XOXO
Bitcoin Lightning wallet and tic-tac-toe game XOXOBitcoin Lightning wallet and tic-tac-toe game XOXO
Bitcoin Lightning wallet and tic-tac-toe game XOXO
Matjaž Lipuš
 
Announcement of 18th IEEE International Conference on Software Testing, Verif...
Announcement of 18th IEEE International Conference on Software Testing, Verif...Announcement of 18th IEEE International Conference on Software Testing, Verif...
Announcement of 18th IEEE International Conference on Software Testing, Verif...
Sebastiano Panichella
 
Obesity causes and management and associated medical conditions
Obesity causes and management and associated medical conditionsObesity causes and management and associated medical conditions
Obesity causes and management and associated medical conditions
Faculty of Medicine And Health Sciences
 
Bonzo subscription_hjjjjjjjj5hhhhhhh_2024.pdf
Bonzo subscription_hjjjjjjjj5hhhhhhh_2024.pdfBonzo subscription_hjjjjjjjj5hhhhhhh_2024.pdf
Bonzo subscription_hjjjjjjjj5hhhhhhh_2024.pdf
khadija278284
 
Media as a Mind Controlling Strategy In Old and Modern Era
Media as a Mind Controlling Strategy In Old and Modern EraMedia as a Mind Controlling Strategy In Old and Modern Era
Media as a Mind Controlling Strategy In Old and Modern Era
faizulhassanfaiz1670
 
María Carolina Martínez - eCommerce Day Colombia 2024
María Carolina Martínez - eCommerce Day Colombia 2024María Carolina Martínez - eCommerce Day Colombia 2024
María Carolina Martínez - eCommerce Day Colombia 2024
eCommerce Institute
 
2024-05-30_meetup_devops_aix-marseille.pdf
2024-05-30_meetup_devops_aix-marseille.pdf2024-05-30_meetup_devops_aix-marseille.pdf
2024-05-30_meetup_devops_aix-marseille.pdf
Frederic Leger
 
Tom tresser burning issue.pptx My Burning issue
Tom tresser burning issue.pptx My Burning issueTom tresser burning issue.pptx My Burning issue
Tom tresser burning issue.pptx My Burning issue
amekonnen
 
Gregory Harris - Cycle 2 - Civics Presentation
Gregory Harris - Cycle 2 - Civics PresentationGregory Harris - Cycle 2 - Civics Presentation
Gregory Harris - Cycle 2 - Civics Presentation
gharris9
 
Burning Issue Presentation By Kenmaryon.pdf
Burning Issue Presentation By Kenmaryon.pdfBurning Issue Presentation By Kenmaryon.pdf
Burning Issue Presentation By Kenmaryon.pdf
kkirkland2
 
Presentatie 8. Joost van der Linde & Daniel Anderton - Eliq 28 mei 2024
Presentatie 8. Joost van der Linde & Daniel Anderton - Eliq 28 mei 2024Presentatie 8. Joost van der Linde & Daniel Anderton - Eliq 28 mei 2024
Presentatie 8. Joost van der Linde & Daniel Anderton - Eliq 28 mei 2024
Dutch Power
 
Collapsing Narratives: Exploring Non-Linearity • a micro report by Rosie Wells
Collapsing Narratives: Exploring Non-Linearity • a micro report by Rosie WellsCollapsing Narratives: Exploring Non-Linearity • a micro report by Rosie Wells
Collapsing Narratives: Exploring Non-Linearity • a micro report by Rosie Wells
Rosie Wells
 
somanykidsbutsofewfathers-140705000023-phpapp02.pptx
somanykidsbutsofewfathers-140705000023-phpapp02.pptxsomanykidsbutsofewfathers-140705000023-phpapp02.pptx
somanykidsbutsofewfathers-140705000023-phpapp02.pptx
Howard Spence
 
International Workshop on Artificial Intelligence in Software Testing
International Workshop on Artificial Intelligence in Software TestingInternational Workshop on Artificial Intelligence in Software Testing
International Workshop on Artificial Intelligence in Software Testing
Sebastiano Panichella
 
Supercharge your AI - SSP Industry Breakout Session 2024-v2_1.pdf
Supercharge your AI - SSP Industry Breakout Session 2024-v2_1.pdfSupercharge your AI - SSP Industry Breakout Session 2024-v2_1.pdf
Supercharge your AI - SSP Industry Breakout Session 2024-v2_1.pdf
Access Innovations, Inc.
 
Presentatie 4. Jochen Cremer - TU Delft 28 mei 2024
Presentatie 4. Jochen Cremer - TU Delft 28 mei 2024Presentatie 4. Jochen Cremer - TU Delft 28 mei 2024
Presentatie 4. Jochen Cremer - TU Delft 28 mei 2024
Dutch Power
 
Gregory Harris' Civics Presentation.pptx
Gregory Harris' Civics Presentation.pptxGregory Harris' Civics Presentation.pptx
Gregory Harris' Civics Presentation.pptx
gharris9
 
Doctoral Symposium at the 17th IEEE International Conference on Software Test...
Doctoral Symposium at the 17th IEEE International Conference on Software Test...Doctoral Symposium at the 17th IEEE International Conference on Software Test...
Doctoral Symposium at the 17th IEEE International Conference on Software Test...
Sebastiano Panichella
 
AWANG ANIQKMALBIN AWANG TAJUDIN B22080004 ASSIGNMENT 2 MPU3193 PHILOSOPHY AND...
AWANG ANIQKMALBIN AWANG TAJUDIN B22080004 ASSIGNMENT 2 MPU3193 PHILOSOPHY AND...AWANG ANIQKMALBIN AWANG TAJUDIN B22080004 ASSIGNMENT 2 MPU3193 PHILOSOPHY AND...
AWANG ANIQKMALBIN AWANG TAJUDIN B22080004 ASSIGNMENT 2 MPU3193 PHILOSOPHY AND...
AwangAniqkmals
 

Recently uploaded (19)

Bitcoin Lightning wallet and tic-tac-toe game XOXO
Bitcoin Lightning wallet and tic-tac-toe game XOXOBitcoin Lightning wallet and tic-tac-toe game XOXO
Bitcoin Lightning wallet and tic-tac-toe game XOXO
 
Announcement of 18th IEEE International Conference on Software Testing, Verif...
Announcement of 18th IEEE International Conference on Software Testing, Verif...Announcement of 18th IEEE International Conference on Software Testing, Verif...
Announcement of 18th IEEE International Conference on Software Testing, Verif...
 
Obesity causes and management and associated medical conditions
Obesity causes and management and associated medical conditionsObesity causes and management and associated medical conditions
Obesity causes and management and associated medical conditions
 
Bonzo subscription_hjjjjjjjj5hhhhhhh_2024.pdf
Bonzo subscription_hjjjjjjjj5hhhhhhh_2024.pdfBonzo subscription_hjjjjjjjj5hhhhhhh_2024.pdf
Bonzo subscription_hjjjjjjjj5hhhhhhh_2024.pdf
 
Media as a Mind Controlling Strategy In Old and Modern Era
Media as a Mind Controlling Strategy In Old and Modern EraMedia as a Mind Controlling Strategy In Old and Modern Era
Media as a Mind Controlling Strategy In Old and Modern Era
 
María Carolina Martínez - eCommerce Day Colombia 2024
María Carolina Martínez - eCommerce Day Colombia 2024María Carolina Martínez - eCommerce Day Colombia 2024
María Carolina Martínez - eCommerce Day Colombia 2024
 
2024-05-30_meetup_devops_aix-marseille.pdf
2024-05-30_meetup_devops_aix-marseille.pdf2024-05-30_meetup_devops_aix-marseille.pdf
2024-05-30_meetup_devops_aix-marseille.pdf
 
Tom tresser burning issue.pptx My Burning issue
Tom tresser burning issue.pptx My Burning issueTom tresser burning issue.pptx My Burning issue
Tom tresser burning issue.pptx My Burning issue
 
Gregory Harris - Cycle 2 - Civics Presentation
Gregory Harris - Cycle 2 - Civics PresentationGregory Harris - Cycle 2 - Civics Presentation
Gregory Harris - Cycle 2 - Civics Presentation
 
Burning Issue Presentation By Kenmaryon.pdf
Burning Issue Presentation By Kenmaryon.pdfBurning Issue Presentation By Kenmaryon.pdf
Burning Issue Presentation By Kenmaryon.pdf
 
Presentatie 8. Joost van der Linde & Daniel Anderton - Eliq 28 mei 2024
Presentatie 8. Joost van der Linde & Daniel Anderton - Eliq 28 mei 2024Presentatie 8. Joost van der Linde & Daniel Anderton - Eliq 28 mei 2024
Presentatie 8. Joost van der Linde & Daniel Anderton - Eliq 28 mei 2024
 
Collapsing Narratives: Exploring Non-Linearity • a micro report by Rosie Wells
Collapsing Narratives: Exploring Non-Linearity • a micro report by Rosie WellsCollapsing Narratives: Exploring Non-Linearity • a micro report by Rosie Wells
Collapsing Narratives: Exploring Non-Linearity • a micro report by Rosie Wells
 
somanykidsbutsofewfathers-140705000023-phpapp02.pptx
somanykidsbutsofewfathers-140705000023-phpapp02.pptxsomanykidsbutsofewfathers-140705000023-phpapp02.pptx
somanykidsbutsofewfathers-140705000023-phpapp02.pptx
 
International Workshop on Artificial Intelligence in Software Testing
International Workshop on Artificial Intelligence in Software TestingInternational Workshop on Artificial Intelligence in Software Testing
International Workshop on Artificial Intelligence in Software Testing
 
Supercharge your AI - SSP Industry Breakout Session 2024-v2_1.pdf
Supercharge your AI - SSP Industry Breakout Session 2024-v2_1.pdfSupercharge your AI - SSP Industry Breakout Session 2024-v2_1.pdf
Supercharge your AI - SSP Industry Breakout Session 2024-v2_1.pdf
 
Presentatie 4. Jochen Cremer - TU Delft 28 mei 2024
Presentatie 4. Jochen Cremer - TU Delft 28 mei 2024Presentatie 4. Jochen Cremer - TU Delft 28 mei 2024
Presentatie 4. Jochen Cremer - TU Delft 28 mei 2024
 
Gregory Harris' Civics Presentation.pptx
Gregory Harris' Civics Presentation.pptxGregory Harris' Civics Presentation.pptx
Gregory Harris' Civics Presentation.pptx
 
Doctoral Symposium at the 17th IEEE International Conference on Software Test...
Doctoral Symposium at the 17th IEEE International Conference on Software Test...Doctoral Symposium at the 17th IEEE International Conference on Software Test...
Doctoral Symposium at the 17th IEEE International Conference on Software Test...
 
AWANG ANIQKMALBIN AWANG TAJUDIN B22080004 ASSIGNMENT 2 MPU3193 PHILOSOPHY AND...
AWANG ANIQKMALBIN AWANG TAJUDIN B22080004 ASSIGNMENT 2 MPU3193 PHILOSOPHY AND...AWANG ANIQKMALBIN AWANG TAJUDIN B22080004 ASSIGNMENT 2 MPU3193 PHILOSOPHY AND...
AWANG ANIQKMALBIN AWANG TAJUDIN B22080004 ASSIGNMENT 2 MPU3193 PHILOSOPHY AND...
 

set.pptx

  • 1. PYTHON PROGRAMMING INFORMATION TECHNOLOGY I SEMESTER Prepared by: Mrs. K Laxminarayanamma, Assistant Professor
  • 2. 2 Course Title PYTHON PROGRAMMING Course Code ACSC01 Class B.Tech I Semester Section IT A AND B Name of the Faculty Mrs. K.Laxminarayanamma Date 22 January, 2021 Course Outcome/s 1. Understand native data types like list, set, tuple, dictionary use them in data processing applications Topic Covered 1. Tuple comprehension, Conversion of List comprehension to Tuple 2. Iterators and Iterables
  • 3. 3 MODULE - III INTRODUCTION TO PYTHON CONTAINER DATA TYPES Lists: Accessing List elements, List operations, List methods, List comprehension; Tuples: Accessing Tuple elements, Tuple operations, Tuple methods, Tuple comprehension, Conversion of List comprehension to Tuple, Iterators and Iterables, zip() function. Sets: Accessing Set elements, Set operations, Set functions, Set comprehension; Dictionaries: Accessing Dictionary elements, Dictionary operations, Dictionary Functions, Nested Dictionary, Dictionary comprehension. Syllabus
  • 4. 4 There are four collection data types in the Python programming language: List is a collection which is ordered and changeable. Allows duplicate members. Tuple is a collection which is ordered and unchangeable. Allows duplicate members. Set is a collection which is unordered and unindexed. No duplicate members. Dictionary is a collection which is unordered, changeable and indexed. No duplicate members. Python Collections (Arrays)
  • 5. 5 Tuple comprehension Tuple comprehension Example : T=(-1,2,-3,4,6) T=tuple(i for i in T if i>0) Output: (2,4,6) List comprehension Example: L=[-1,2,-3,4,6] L2=[i for i in L if i>0] Output: [2,4,6]
  • 6. 6 Conversion of List comprehension to Tuple Example L=[1,2,3,4,5,6,7] T=tuple([i for i in L]) Print(T) Output: (1,2,3,4,5,6,7)
  • 7. 7 List vs Tuple List Tuple 1 The literal syntax of list is shown by the [ ]. The literal syntax of the tuple is shown by the (). 2 The List is mutable. The tuple is immutable. 3 List iteration is slower and is time consuming. Tuple iteration is faster. 4 Lists consume more memory Tuple consume less memory as compared to the list 5 The list is used in the scenario in which we need to store the simple collections with no constraints where the value of the items can be changed. The tuple is used in the cases where we need to store the read-only collections i.e., the value of the items cannot be changed. It can be used as the key inside the dictionary. 6 List provides many in-built methods. Tuples have less in-built methods.
  • 8. 8 Python Iterators An iterator is an object that contains a countable number of values. An iterator is an object that can be iterated upon, meaning that you can traverse through all the values. Technically, in Python, an iterator is an object which implements the iterator protocol, which consist of the methods __iter__() and __next__().
  • 9. 9 Iterator In Python, an iterator is an object which implements the iterator protocol, which consist of the methods __iter__() and __next__() . • An iterator is an object representing a stream of data. • It returns the data one element at a time. • A Python iterator must support a method called __next__() that takes no arguments and always returns the next element of the stream. • If there are no more elements in the stream, __next__() must raise the StopIteration exception. • Iterators don’t have to be finite. It’s perfectly reasonable to write an iterator that produces an infinite stream of data.
  • 10. 10 Iterable • In Python, Iterable is anything you can loop over with a for loop. • An object is called an iterable if u can get an iterator out of it. • Calling iter() function on an iterable gives us an iterator. • Calling next() function on iterator gives us the next element. • If the iterator is exhausted(if it has no more elements), calling next() raises StopIteration exception.
  • 12. 12 Iterating through an Iterator • We use the next() function to manually iterate through all the items of an iterator. • When we reach the end and there is no more data to be returned, it will raise the StopIteration Exception. # define a list my_list = [4, 7, 0, 3] # get an iterator using iter() my_iter = iter(my_list) # iterate through it using next() # Output: 4 print(next(my_iter))
  • 13. 13 Iterating through an Iterator # Output: 7 print(next(my_iter)) # next(obj) is same as obj.__next__() # Output: 0 print(my_iter.__next__()) # Output: 3 print(my_iter.__next__()) # This will raise error, no items left next(my_iter)
  • 14. 14 Python Iterators Iterator vs Iterable: Lists, tuples, dictionaries, and sets are all iterable objects. They are iterable containers which you can get an iterator from. All these objects have a iter() method which is used to get an iterator: Example Return an iterator from a tuple, and print each value: mytuple = ("apple", "banana", "cherry") myit = iter(mytuple) print(next(myit)) print(next(myit)) print(next(myit)) Output: apple banana cherry
  • 15. 15 Python Iterators Iterator vs Iterable: Even strings are iterable objects, and can return an iterator: Example Strings are also iterable objects, containing a sequence of characters: mystr = "banana" myit = iter(mystr) print(next(myit)) print(next(myit)) print(next(myit)) print(next(myit)) print(next(myit)) print(next(myit)) Output: b a n a n a
  • 16. 16 Python Iterators Looping Through an Iterator We can also use a for loop to iterate through an iterable object: Example Iterate the values of a tuple: mytuple = ("apple", "banana", "cherry") for x in mytuple: print(x) Output: apple banana cherry
  • 17. 17 Python Iterators Example Iterate the characters of a string: mystr = "banana" for x in mystr: print(x) Output: b a n a n a
  • 18. 18 Working of for loop for Iterators for <var> in <iterable>: <statement(s)>
  • 19. 19 Python Sets Sets are used to store multiple items in a single variable. Set is one of 4 built-in data types in Python used to store collections of data, the other 3 are List, Tuple, and Dictionary, all with different qualities and usage. A set is a collection which is both unordered and unindexed. Sets are written with curly brackets. Example Create a Set: thisset = {"apple", "banana", "cherry"} print(thisset) output: {'apple', 'banana', 'cherry'}
  • 20. 20 Set Items Set items are unordered, unchangeable, and do not allow duplicate values. Unordered Unordered means that the items in a set do not have a defined order. Set items can appear in a different order every time you use them, and cannot be referred to by index or key. Unchangeable Sets are unchangeable, meaning that we cannot change the items after the set has been created. Once a set is created, you cannot change its items, but you can add new items. Duplicates Not Allowed Sets cannot have two items with the same value.
  • 21. 21 Set Items Example Duplicate values will be ignored: thisset = {"apple", "banana", "cherry", "apple"} print(thisset) Output: {'banana', 'cherry', 'apple'}
  • 22. 22 Python Dictionaries Dictionaries are used to store data values in key:value pairs. A dictionary is a collection which is unordered, changeable and does not allow duplicates. Dictionaries are written with curly brackets, and have keys and values: Example Create and print a dictionary: thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } print(thisdict) Output: {'brand': 'Ford', 'model': 'Mustang', 'year': 1964}
  • 23. 23 Dictionary Items Dictionary items are unordered, changeable, and does not allow duplicates. Dictionary items are presented in key:value pairs, and can be referred to by using the key name. Example Print the "brand" value of the dictionary: thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } print(thisdict["brand"]) Output: Ford