Unit 2: Data Types and OOP Concepts
I. Orientation — Python’s Data and Object Model
Python is a dynamically typed, object-oriented programming language in which every value is an object. A data type determines a value’s possible contents, supported operations, and behavior; object-oriented programming organizes related data and behavior into reusable classes and objects.
- Dynamic typing: A variable is bound to an object without an explicit type declaration;
x = 10bindsxto an integer, whilex = "ten"can later bind it to a string. - Object identity, type, and value: Every object has an identity, a type, and a value. The functions
id(x),type(x), and expressions involvingxreveal these properties. - Mutability:
- Mutable objects: Lists, dictionaries, and sets can change after creation.
- Immutable objects: Strings, tuples, and range objects cannot change after creation.
- Ordered collections: Strings, lists, tuples, dictionaries, and ranges preserve a defined order; dictionary order follows insertion order.
- Unordered collections: Sets do not support positional indexing because their elements are organized by hashing rather than sequence position.
- Zero-based indexing: The first element of a sequence has index
0; negative indices count backward, so-1identifies the last element. - Iteration: Collection and range objects are iterable and can be processed with
for, membership operators, comprehensions, and functions such aslen(). - Class-based organization: A class defines attributes and methods, while an object is a particular instance created from that class.
II. Strings — Immutable Text Sequences
A. Strings
A string is an immutable sequence of Unicode characters used to represent and manipulate textual data.
- Creation: Strings are enclosed in matching single, double, or triple quotation marks.
name = "Python"
message = 'Data Types'
paragraph = """Text can
span lines."""- Indexing and slicing: For
s = "Python",s[0]is"P",s[-1]is"n", ands[1:4]is"yth". A slice followss[start:stop:step], excludingstop. - Immutability:
s[0] = "J"raisesTypeError; a changed string must be created, such as"J" + s[1:]. - Operators:
+concatenates,*repeats, andintests membership. Thus,"Py" in "Python"evaluates toTrue. - Common methods:
- Case and spacing:
upper(),lower(),title(), andstrip(). - Search and replacement:
find(),count(), andreplace(). - Splitting and joining:
"a,b".split(",")produces["a", "b"];"-".join(["a", "b"])produces"a-b".
- Case and spacing:
- Formatting: An f-string embeds expressions clearly.
language = "Python"
version = 3
text = f"{language} version {version}"- Escape sequences:
\nrepresents a newline and\ta tab; raw strings such asr"C:\new"treat backslashes literally.
III. Lists — Mutable Ordered Collections
A. Lists
A list is a mutable, ordered sequence that can store duplicate values and objects of different types.
- Creation: Square brackets or
list()create lists;numbers = [10, 20, 30]contains three integer references. - Access: Lists support indexing, slicing, and nesting. In
matrix = [[1, 2], [3, 4]],matrix[1][0]is3. - Mutation: An indexed element or slice can be replaced;
numbers[1] = 25changes the list to[10, 25, 30]. - Common methods:
- Adding:
append(x)adds one object,extend(iterable)adds several elements, andinsert(i, x)adds at indexi. - Removing:
remove(x)removes the first matching value,pop(i)removes and returns an indexed value, andclear()removes all elements. - Organization:
sort()changes the list in place, whilereverse()reverses its order.
- Adding:
- Copying and aliasing:
b = amakes both names refer to one list;b = a.copy()creates a shallow copy. - List comprehensions: A concise expression can construct a transformed or filtered list.
squares = [n * n for n in range(1, 6) if n % 2 != 0]
# Result: [1, 9, 25]Here, n is each generated integer, and the condition retains odd values.
IV. Tuples — Fixed Ordered Collections
A. Tuples
A tuple is an immutable, ordered sequence suited to fixed records and values that should not be reassigned.
- Creation: Parentheses are conventional:
point = (4, 7). A one-element tuple requires a comma, as insingle = (4,). - Immutability:
point[0] = 5raisesTypeError, although a mutable object stored inside a tuple may itself change. - Operations: Tuples support indexing, slicing, concatenation, repetition, membership,
count(), andindex(). - Packing and unpacking:
record = ("Asha", 20)packs two values;name, age = recordassigns them separately. - Multiple return values: A function can return a tuple, which callers may unpack.
def minimum_maximum(values):
return min(values), max(values)
lowest, highest = minimum_maximum([4, 1, 9])- List contrast: Tuples provide structural immutability and can be dictionary keys when all their contents are hashable; lists cannot be dictionary keys.
V. Dictionaries — Key–Value Mappings
A. Dictionaries
A dictionary is a mutable mapping that associates unique, hashable keys with arbitrary values.
- Creation:
student = {"name": "Ravi", "marks": 82}maps two string keys to values. - Key rules: Keys must be hashable, so strings, numbers, and suitable tuples are allowed; lists and sets are not. Reassigning an existing key replaces its value.
- Access:
student["name"]returns"Ravi"but raisesKeyErrorfor a missing key;student.get("grade", "NA")safely supplies"NA". - Modification:
student["marks"] = 90updates a value, whilestudent["city"] = "Pune"inserts a pair. - Common methods:
keys(),values(), anditems()provide dynamic views;update()merges entries, andpop(key)removes and returns a value. - Iteration: Iterating directly processes keys; paired iteration uses
items().
for key, value in student.items():
print(key, value)- Dictionary comprehension:
{n: n * n for n in range(1, 4)}produces{1: 1, 2: 4, 3: 9}. - Use cases: Dictionaries model named records, frequency tables, configurations, caches, and fast key-based lookup.
VI. Sets — Collections of Unique Elements
A. Sets
A set is a mutable collection of unique, hashable elements designed for membership testing and mathematical set operations.
- Creation:
{1, 2, 3}creates a set, but an empty set requiresset()because{}creates an empty dictionary. - Uniqueness:
{1, 1, 2}becomes{1, 2}; duplicate values are automatically eliminated. - No positional access: Sets are not indexed or sliced, and their displayed iteration order should not be treated as a fixed sequence order.
- Modification:
add(x)inserts one element,update(iterable)inserts several,discard(x)removes without error, andremove(x)raisesKeyErrorif absent. - Set operations: For sets
AandB:- Union:
A | Bcontains elements in either set. - Intersection:
A & Bcontains common elements. - Difference:
A - Bcontains elements only inA. - Symmetric difference:
A ^ Bcontains elements in exactly one set.
- Union:
- Relationships:
A <= Btests whetherAis a subset;A.isdisjoint(B)tests whether no elements are shared. - Immutable form:
frozenset()creates an immutable, hashable set suitable for use as a dictionary key or another set’s element.
VII. Range — Arithmetic Integer Sequences
A. Range
A range is an immutable, memory-efficient sequence of integers commonly used to control iteration.
- Forms:
range(stop),range(start, stop), andrange(start, stop, step)define arithmetic sequences in whichstopis excluded. - Symbol meanings:
start: First generated integer; the default is0.stop: Exclusive boundary.step: Difference between consecutive integers; the default is1and it cannot be zero.
- Examples:
range(2, 8, 2)represents2, 4, 6;range(5, 0, -1)represents5, 4, 3, 2, 1. - Memory efficiency: A range stores its arithmetic definition rather than materializing every integer.
list(range(4))explicitly creates[0, 1, 2, 3]. - Sequence behavior: Ranges support
len(), indexing, slicing, membership testing, and equality comparisons. - Loop use:
for index in range(3):
print(index)This iterates with index equal to 0, 1, and 2.
VIII. OOP Features — Class-Based Program Design
A. OOP features
Object-oriented programming models a system through interacting objects that combine state with behavior.
- Class: A blueprint defining attributes and methods;
class Account:introduces a new class. - Object: An instance of a class;
a = Account()constructs anAccountobject. - Constructor initialization:
__init__initializes instance state, whileselfrefers to the current instance.
class Account:
bank = "ABC"
def __init__(self, owner, balance=0):
self.owner = owner
self.balance = balance
def deposit(self, amount):
self.balance += amount- Attributes:
bankis a class attribute shared through the class;ownerandbalanceare instance attributes. - Methods:
a.deposit(100)invokes behavior withasupplied asself. - Abstraction: A class exposes useful operations while hiding implementation details; callers use
deposit()without managing the assignment directly. - Polymorphism: Different classes can respond to the same method name, such as multiple shapes implementing
area(). - Benefits: OOP supports modularity, reuse, maintainability, and direct modeling of entities with related data and operations.
IX. Encapsulation — Controlled Access to State
A. Encapsulation
Encapsulation bundles data and methods within a class and controls how an object’s internal state is accessed or modified.
- Public members:
self.nameis accessible normally and forms part of the object’s public interface. - Non-public convention:
_balancesignals that a member is intended for internal use, although Python does not enforce this restriction. - Name mangling:
__pinis transformed to a class-qualified name such as_Account__pin, reducing accidental access but not providing absolute privacy. - Properties:
propertyenables validated, method-controlled access using attribute syntax.
class Temperature:
def __init__(self, celsius):
self.celsius = celsius
@property
def celsius(self):
return self._celsius
@celsius.setter
def celsius(self, value):
if value < -273.15:
raise ValueError("Below absolute zero")
self._celsius = value- Invariant protection: The setter ensures
_celsiusnever falls below-273.15, preserving a valid object state. - Interface stability: Internal representation can change while clients continue using
object.celsius. - Limitation: Python follows a “consenting adults” philosophy; encapsulation relies on interfaces, naming conventions, and careful design rather than strict access keywords.
X. Inheritance — Extending Existing Classes
A. Inheritance
Inheritance creates a new class from an existing class, allowing behavior and attributes to be reused, specialized, or replaced.
- Terminology: The existing class is the base or parent class; the derived class is the subclass or child class.
- Basic syntax:
class Dog(Animal):makesDoginherit accessible behavior fromAnimal. - Method overriding: A subclass can redefine an inherited method to provide specialized polymorphic behavior.
- Parent initialization:
super()accesses the parent implementation without naming the parent class directly.
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
return "sound"
class Dog(Animal):
def __init__(self, name, breed):
super().__init__(name)
self.breed = breed
def speak(self):
return "bark"- Inherited state: A
DogreceivesnamethroughAnimal.__init__and adds its ownbreedattribute. - Type relationship:
isinstance(Dog("Max", "Beagle"), Animal)isTrue, expressing that a dog “is an” animal. - Forms: Python supports single, multilevel, hierarchical, and multiple inheritance. In multiple inheritance, the method resolution order determines search order and is available through
ClassName.mro(). - Design constraint: Inheritance is appropriate for a genuine “is-a” relationship; composition is preferable when one object merely “has-a” collaborating object.
Did this save you a night before the exam?
LPU Notes is free, and it stays free. Ads cover part of the server bill. The rest comes out of a student's own pocket: the domain, the storage, and keeping the site up through the weeks everyone needs it at once.
The payment button didn't load. An ad blocker or a filtered network is the usual reason. to try again.
Nothing here is ever locked, and nothing unlocks. Chip in only if it was worth it. What it pays for →