SlideShare ist ein Scribd-Unternehmen logo
1 von 71
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.

Weitere ähnliche Inhalte

Ähnlich wie 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
 

Ähnlich wie 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
 

Kürzlich hochgeladen

Kürzlich hochgeladen (20)

data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduits
 
Glass Ceramics: Processing and Properties
Glass Ceramics: Processing and PropertiesGlass Ceramics: Processing and Properties
Glass Ceramics: Processing and Properties
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
NFPA 5000 2024 standard .
NFPA 5000 2024 standard                                  .NFPA 5000 2024 standard                                  .
NFPA 5000 2024 standard .
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineering
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdf
 
Vivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design SpainVivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design Spain
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 

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.