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.

Module 3,4.pptx

  • 1.
    Module 3 LISTS, DICTIONARIES,TUPLES and REGULAR EXPRESSIONS
  • 2.
    Lists • A listis 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 thatcontains 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 integerexpression 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.
  • 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:
  • 11.
  • 12.
  • 14.
    • pop modifiesthe list and returns the element that was removed. If you don’t provide an index, it deletes and returns the last element.
  • 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.
  • 29.
  • 33.
    • Dictionary asa 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. Youcould 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. Youcould 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.
  • 37.
  • 38.
    Module 4 • Classesand Objects • Classes and Functions • Classes and methods
  • 39.
    Classes and Objects CLASSESAND 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 • Aclass 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)
  • 42.
    • The processof creating a new object is called as instantiation and the object is instance of a class.
  • 44.
    Attributes • class attributesare 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
  • 46.
  • 48.
    Instances as returnvalues • 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 aclass 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.
  • 54.
    Copying • Copying anobject 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>
  • 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:
  • 59.
  • 60.
  • 61.
    Prototyping versus planning Wheneverwe 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.
  • 63.
    Classes and Methods Object-OrientedFeatures • 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 isassociated 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.
  • 65.
  • 68.
  • 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.