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:
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.
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.
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
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:
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.