SlideShare a Scribd company logo
Module 3
LISTS, DICTIONARIES ,TUPLES and
REGULAR EXPRESSIONS
Lists
• A list is a sequence
Like a string, a list is a sequence of values. In a
string, the values are characters; in a list, they
can be any type. The values in list are called
elements or sometimes items.
A list that contains no elements is called an empty list;
you can create one with empty brackets, [ ].
Lists are mutable
• The syntax for accessing the elements of a list
is the same as for accessing the characters of a
string: the bracket operator.
• Any integer expression can be used as an
index.
• If you try to read or write an element that
does not exist, you get an IndexError.
• If an index has a negative value, it counts
backward from the end of the list.
Traversing a list
• The most common way to traverse the
elements of a list is with a for loop.
• if you want to write or update the elements,
you need the indices. A common way to do
that is to combine the functions range and
len:
List slices
List methods
• pop modifies the list and returns the element
that was removed. If you don’t provide an
index, it deletes and returns the last element.
• The sum() function only works when the list elements are
numbers. The other functions (max(), len(), etc.) work with
lists of strings and other types that can be comparable.
Dictionaries
• Dictionary as a set of counters
Suppose you are given a string and you want to
count how many times each letter appears.
There are several ways you could do it:
• 1. You could create 26 variables, one for each
letter of the alphabet. Then you could traverse
the string and, for each character, increment
the corresponding counter, probably using a
chained conditional.
• 2. You could create a list with 26 elements.
Then you could convert each character to a
number (using the built-in function ord), use
the number as an index into the list, and
increment the appropriate counter.
• 3. You could create a dictionary with
characters as keys and counters as the
corresponding values. The first time you see a
character, you would add an item to the
dictionary. After that you would increment the
value of an existing item.
Tuples
• Tuples are immutable
Module 4
• Classes and Objects
• Classes and Functions
• Classes and methods
Classes and Objects
CLASSES AND OBJECTS
• Python is an object-oriented programming language,
and class is a basis for any object oriented
programming language.
• Class is a user-defined data type which binds data and
functions together into single entity.
• Class is just a prototype (or a logical entity/blue print)
which will not consume any memory.
• An object is an instance of a class and it has physical
existence.
• One can create any number of objects for a class.
• A class can have a set of variables (also known as
attributes, member variables) and member functions
(also known as methods).
Programmer-Defined Types
• A class in Python can be created using a
keyword class.
• creating an empty class without any members
by just using the keyword pass within it.
class Point:
pass
print(Point)
• The process of creating a new object is called
as instantiation and the object is instance of a
class.
Attributes
• class attributes are class variables that are
inherited by every object of a class.
• One can assign values to these attributes using dot
operator.
• For example, keeping coordinate points in mind,
we can assign two attributes x and y for the object
of a class Point as below
p.x =10.0
p.y =20.0
x= p.x
>>> print(x)
10.0
Rectangles
Instances as return values
• Functions can return instances. For example,
find_center takes a Rectangle as an argument
and returns a Point that contains the coordinates
of the center of the Rectangle:
• Write a class Point representing a point on
coordinate system. Implement following
functions
o A function read_point( ) to receive x and y
attributes of a Point object as user input.
o A function distance() which takes two objects of
Point class as arguments and computes the
Euclidean distance between them.
o A function print_point()to display one point in
the form of ordered-pair.
Copying
• Copying an object is often an alternative to
aliasing. The copy module contains a function
called copy that can duplicate any object.
class Point:
pass
p1=Point()
p1.x=10
p1.y=20
p2=p1
>>> print(p1)
< main .Point object at 0x01581BF0>
>>> print(p2)
< main .Point object at 0x01581BF0>
Classes and Functions
• Time
programmer-defined type, we’ll define a class called Time that
records the time of day. The class definition looks like this:
Pure functions
Modifiers
Prototyping versus planning
Whenever we do not know the complete problem statement, we may
write the program initially, and then keep of modifying it as and when
requirement (problem definition) changes. This methodology is known
as prototype and patch.
first design the prototype based on the information available and
then perform patch-work as and when extra information is gathered.
But, this type of incremental development may end-up in
unnecessary code, with many special cases and it may be unreliable
too.
What is prototype and patch?
prototype and patch: A development plan that involves writing a rough draft of a
program, testing, and correcting errors as they are found.
Classes and Methods
Object-Oriented Features
• Programs include class and method
definitions.
• Most of the computation is expressed in terms
of operations on objects.
• Objects often represent things in the real
world, and methods often correspond to the
ways things in the real world interact.
function which is associated with a particular class is known
as a method.
Methods are semantically the same as functions, but there are two
syntactic differences:
Methods are defined inside a class definition in order to make the
relationship between the class and the method explicit.
The syntax for invoking a method is different from the syntax for
calling a function.
Pointing Objects
The init () Method
class Point:
def __init__ (self,x=0,y=0):
self.x=x
self.y=y
P1=Point()
_ _str_ _ method
• It’s a special method it is used to return a
string representation of an object.

More Related Content

Similar to Module 3,4.pptx

Python Programming - VI. Classes and Objects
Python Programming - VI. Classes and ObjectsPython Programming - VI. Classes and Objects
Python Programming - VI. Classes and Objects
Ranel Padon
 
Python For Data Science.pptx
Python For Data Science.pptxPython For Data Science.pptx
Python For Data Science.pptx
rohithprabhas1
 
Q-Step_WS_06112019_Data_Analysis_and_visualisation_with_Python.pptx
Q-Step_WS_06112019_Data_Analysis_and_visualisation_with_Python.pptxQ-Step_WS_06112019_Data_Analysis_and_visualisation_with_Python.pptx
Q-Step_WS_06112019_Data_Analysis_and_visualisation_with_Python.pptx
kalai75
 

Similar to Module 3,4.pptx (20)

Pa2 session 1
Pa2 session 1Pa2 session 1
Pa2 session 1
 
Pa1 session 2
Pa1 session 2 Pa1 session 2
Pa1 session 2
 
Python-Basics.pptx
Python-Basics.pptxPython-Basics.pptx
Python-Basics.pptx
 
Module 4.pptx
Module 4.pptxModule 4.pptx
Module 4.pptx
 
Java
JavaJava
Java
 
O6u CS-315A OOP Lecture (1).pdf
O6u CS-315A OOP Lecture (1).pdfO6u CS-315A OOP Lecture (1).pdf
O6u CS-315A OOP Lecture (1).pdf
 
Python programming
Python programmingPython programming
Python programming
 
Python Programming - VI. Classes and Objects
Python Programming - VI. Classes and ObjectsPython Programming - VI. Classes and Objects
Python Programming - VI. Classes and Objects
 
Concept of Object-Oriented in C++
Concept of Object-Oriented in C++Concept of Object-Oriented in C++
Concept of Object-Oriented in C++
 
Lec01-Algorithems - Introduction and Overview.pdf
Lec01-Algorithems - Introduction and Overview.pdfLec01-Algorithems - Introduction and Overview.pdf
Lec01-Algorithems - Introduction and Overview.pdf
 
Python for katana
Python for katanaPython for katana
Python for katana
 
C++ training
C++ training C++ training
C++ training
 
Summer Training Project On Python Programming
Summer Training Project On Python ProgrammingSummer Training Project On Python Programming
Summer Training Project On Python Programming
 
2. Values and Data types in Python.pptx
2. Values and Data types in Python.pptx2. Values and Data types in Python.pptx
2. Values and Data types in Python.pptx
 
Python For Data Science.pptx
Python For Data Science.pptxPython For Data Science.pptx
Python For Data Science.pptx
 
Python Basics.pptx
Python Basics.pptxPython Basics.pptx
Python Basics.pptx
 
Python-Classes.pptx
Python-Classes.pptxPython-Classes.pptx
Python-Classes.pptx
 
Q-Step_WS_06112019_Data_Analysis_and_visualisation_with_Python.pptx
Q-Step_WS_06112019_Data_Analysis_and_visualisation_with_Python.pptxQ-Step_WS_06112019_Data_Analysis_and_visualisation_with_Python.pptx
Q-Step_WS_06112019_Data_Analysis_and_visualisation_with_Python.pptx
 
Python with data Sciences
Python with data SciencesPython with data Sciences
Python with data Sciences
 
Learn advanced java programming
Learn advanced java programmingLearn advanced java programming
Learn advanced java programming
 

Recently uploaded

CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
R&R Consult
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
ViniHema
 
Automobile Management System Project Report.pdf
Automobile Management System Project Report.pdfAutomobile Management System Project Report.pdf
Automobile Management System Project Report.pdf
Kamal Acharya
 
RS Khurmi Machine Design Clutch and Brake Exercise Numerical Solutions
RS Khurmi Machine Design Clutch and Brake Exercise Numerical SolutionsRS Khurmi Machine Design Clutch and Brake Exercise Numerical Solutions
RS Khurmi Machine Design Clutch and Brake Exercise Numerical Solutions
Atif Razi
 
Fruit shop management system project report.pdf
Fruit shop management system project report.pdfFruit shop management system project report.pdf
Fruit shop management system project report.pdf
Kamal Acharya
 
Digital Signal Processing Lecture notes n.pdf
Digital Signal Processing Lecture notes n.pdfDigital Signal Processing Lecture notes n.pdf
Digital Signal Processing Lecture notes n.pdf
AbrahamGadissa
 

Recently uploaded (20)

ASME IX(9) 2007 Full Version .pdf
ASME IX(9)  2007 Full Version       .pdfASME IX(9)  2007 Full Version       .pdf
ASME IX(9) 2007 Full Version .pdf
 
The Ultimate Guide to External Floating Roofs for Oil Storage Tanks.docx
The Ultimate Guide to External Floating Roofs for Oil Storage Tanks.docxThe Ultimate Guide to External Floating Roofs for Oil Storage Tanks.docx
The Ultimate Guide to External Floating Roofs for Oil Storage Tanks.docx
 
Event Management System Vb Net Project Report.pdf
Event Management System Vb Net  Project Report.pdfEvent Management System Vb Net  Project Report.pdf
Event Management System Vb Net Project Report.pdf
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
 
Scaling in conventional MOSFET for constant electric field and constant voltage
Scaling in conventional MOSFET for constant electric field and constant voltageScaling in conventional MOSFET for constant electric field and constant voltage
Scaling in conventional MOSFET for constant electric field and constant voltage
 
Automobile Management System Project Report.pdf
Automobile Management System Project Report.pdfAutomobile Management System Project Report.pdf
Automobile Management System Project Report.pdf
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
 
RS Khurmi Machine Design Clutch and Brake Exercise Numerical Solutions
RS Khurmi Machine Design Clutch and Brake Exercise Numerical SolutionsRS Khurmi Machine Design Clutch and Brake Exercise Numerical Solutions
RS Khurmi Machine Design Clutch and Brake Exercise Numerical Solutions
 
Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.
 
WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234
 
shape functions of 1D and 2 D rectangular elements.pptx
shape functions of 1D and 2 D rectangular elements.pptxshape functions of 1D and 2 D rectangular elements.pptx
shape functions of 1D and 2 D rectangular elements.pptx
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
 
Introduction to Machine Learning Unit-4 Notes for II-II Mechanical Engineering
Introduction to Machine Learning Unit-4 Notes for II-II Mechanical EngineeringIntroduction to Machine Learning Unit-4 Notes for II-II Mechanical Engineering
Introduction to Machine Learning Unit-4 Notes for II-II Mechanical Engineering
 
Fruit shop management system project report.pdf
Fruit shop management system project report.pdfFruit shop management system project report.pdf
Fruit shop management system project report.pdf
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
 
Introduction to Casting Processes in Manufacturing
Introduction to Casting Processes in ManufacturingIntroduction to Casting Processes in Manufacturing
Introduction to Casting Processes in Manufacturing
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
 
Digital Signal Processing Lecture notes n.pdf
Digital Signal Processing Lecture notes n.pdfDigital Signal Processing Lecture notes n.pdf
Digital Signal Processing Lecture notes n.pdf
 

Module 3,4.pptx

  • 1. Module 3 LISTS, DICTIONARIES ,TUPLES and REGULAR EXPRESSIONS
  • 2. Lists • A list is a sequence Like a string, a list is a sequence of values. In a string, the values are characters; in a list, they can be any type. The values in list are called elements or sometimes items.
  • 3. A list that contains no elements is called an empty list; you can create one with empty brackets, [ ].
  • 4. Lists are mutable • The syntax for accessing the elements of a list is the same as for accessing the characters of a string: the bracket operator.
  • 5. • Any integer expression can be used as an index. • If you try to read or write an element that does not exist, you get an IndexError. • If an index has a negative value, it counts backward from the end of the list.
  • 6.
  • 7. Traversing a list • The most common way to traverse the elements of a list is with a for loop. • if you want to write or update the elements, you need the indices. A common way to do that is to combine the functions range and len:
  • 8.
  • 9.
  • 10.
  • 13.
  • 14. • pop modifies the list and returns the element that was removed. If you don’t provide an index, it deletes and returns the last element.
  • 15.
  • 16. • The sum() function only works when the list elements are numbers. The other functions (max(), len(), etc.) work with lists of strings and other types that can be comparable.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 30.
  • 31.
  • 32.
  • 33. • Dictionary as a set of counters Suppose you are given a string and you want to count how many times each letter appears. There are several ways you could do it:
  • 34. • 1. You could create 26 variables, one for each letter of the alphabet. Then you could traverse the string and, for each character, increment the corresponding counter, probably using a chained conditional. • 2. You could create a list with 26 elements. Then you could convert each character to a number (using the built-in function ord), use the number as an index into the list, and increment the appropriate counter.
  • 35. • 3. You could create a dictionary with characters as keys and counters as the corresponding values. The first time you see a character, you would add an item to the dictionary. After that you would increment the value of an existing item.
  • 36.
  • 38. Module 4 • Classes and Objects • Classes and Functions • Classes and methods
  • 39. Classes and Objects CLASSES AND OBJECTS • Python is an object-oriented programming language, and class is a basis for any object oriented programming language. • Class is a user-defined data type which binds data and functions together into single entity. • Class is just a prototype (or a logical entity/blue print) which will not consume any memory. • An object is an instance of a class and it has physical existence. • One can create any number of objects for a class. • A class can have a set of variables (also known as attributes, member variables) and member functions (also known as methods).
  • 40. Programmer-Defined Types • A class in Python can be created using a keyword class. • creating an empty class without any members by just using the keyword pass within it. class Point: pass print(Point)
  • 41.
  • 42. • The process of creating a new object is called as instantiation and the object is instance of a class.
  • 43.
  • 44. Attributes • class attributes are class variables that are inherited by every object of a class. • One can assign values to these attributes using dot operator. • For example, keeping coordinate points in mind, we can assign two attributes x and y for the object of a class Point as below p.x =10.0 p.y =20.0
  • 45.
  • 47.
  • 48. Instances as return values • Functions can return instances. For example, find_center takes a Rectangle as an argument and returns a Point that contains the coordinates of the center of the Rectangle:
  • 49. • Write a class Point representing a point on coordinate system. Implement following functions o A function read_point( ) to receive x and y attributes of a Point object as user input. o A function distance() which takes two objects of Point class as arguments and computes the Euclidean distance between them. o A function print_point()to display one point in the form of ordered-pair.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54. Copying • Copying an object is often an alternative to aliasing. The copy module contains a function called copy that can duplicate any object. class Point: pass p1=Point() p1.x=10 p1.y=20 p2=p1 >>> print(p1) < main .Point object at 0x01581BF0> >>> print(p2) < main .Point object at 0x01581BF0>
  • 55.
  • 56.
  • 57. Classes and Functions • Time programmer-defined type, we’ll define a class called Time that records the time of day. The class definition looks like this:
  • 58.
  • 61. Prototyping versus planning Whenever we do not know the complete problem statement, we may write the program initially, and then keep of modifying it as and when requirement (problem definition) changes. This methodology is known as prototype and patch. first design the prototype based on the information available and then perform patch-work as and when extra information is gathered. But, this type of incremental development may end-up in unnecessary code, with many special cases and it may be unreliable too. What is prototype and patch? prototype and patch: A development plan that involves writing a rough draft of a program, testing, and correcting errors as they are found.
  • 62.
  • 63. Classes and Methods Object-Oriented Features • Programs include class and method definitions. • Most of the computation is expressed in terms of operations on objects. • Objects often represent things in the real world, and methods often correspond to the ways things in the real world interact.
  • 64. function which is associated with a particular class is known as a method. Methods are semantically the same as functions, but there are two syntactic differences: Methods are defined inside a class definition in order to make the relationship between the class and the method explicit. The syntax for invoking a method is different from the syntax for calling a function.
  • 66.
  • 67.
  • 68. The init () Method
  • 69.
  • 70. class Point: def __init__ (self,x=0,y=0): self.x=x self.y=y P1=Point()
  • 71. _ _str_ _ method • It’s a special method it is used to return a string representation of an object.