0% found this document useful (0 votes)
18 views9 pages

Python Basics: Lists, Dictionaries, Classes

The document provides an overview of Python basics, including list and dictionary methods, as well as the concepts of mutable and immutable objects. It explains the structure of classes in Python, detailing attributes, methods, and special methods, along with the concept of inheritance. Additionally, it highlights the differences between mutable and immutable objects, including their memory efficiency and performance implications.

Uploaded by

jhakrajesh12345
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views9 pages

Python Basics: Lists, Dictionaries, Classes

The document provides an overview of Python basics, including list and dictionary methods, as well as the concepts of mutable and immutable objects. It explains the structure of classes in Python, detailing attributes, methods, and special methods, along with the concept of inheritance. Additionally, it highlights the differences between mutable and immutable objects, including their memory efficiency and performance implications.

Uploaded by

jhakrajesh12345
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Python practice Computer Science 9618

PYTHON BASICS-3

LIST METHODS
SN Methods with Description

1 [Link](obj)

Appends object obj to list

2 [Link](obj)

Returns count of how many times obj occurs in list

3 [Link](seq)

Appends the contents of seq to list

4 [Link](obj)

Returns the lowest index in list that obj appears

5 [Link](index, obj)

Inserts object obj into list at offset index

6 [Link](obj=list[-1])
Removes and returns last object or obj from list

7 [Link](obj)
Removes object obj from list

8 [Link]()
Reverses objects of list in place

9 [Link]([func])
Sorts objects of list, use compare func if given

Merryland International School 1 2024-2025


Python practice Computer Science 9618

MUTABLE AND IMMUTABLE OBJECTS


In Python, objects can be classified as either mutable or immutable based
on whether their value or state can be changed after they are created.

Mutable Objects
Mutable objects are those whose value or state can be changed after they
are created. When you modify a mutable object, you are changing the
object itself, not creating a new one. Examples of mutable objects in
Python include:

1. Lists: You can add, remove, or modify elements.

2. Dictionaries: You can add, remove, or modify key-value pairs.

3. Sets: You can add or remove elements.

Immutable Objects
Immutable objects, on the other hand, are those whose value or state cannot be
changed after they are created. If you attempt to modify an immutable object, a new
object is created instead. Examples of immutable objects in Python include:

1. Strings: Any operation that appears to modify a string creates a new string.

Merryland International School 2 2024-2025


Python practice Computer Science 9618

2. Tuples: Once created, elements cannot be added, removed, or modified.

3. Integers, Floats, Booleans: Any operation that changes their value results in a new
object.

Key Differences
- Memory Efficiency: Immutable objects can be more memory-efficient because
Python can reuse existing objects rather than creating new ones.
- Thread Safety: Immutable objects are inherently thread-safe because their
state cannot change, making them ideal for concurrent programming.
- Performance: Mutable objects can be modified in place, which can be more
efficient when changes are frequent, whereas immutable objects may involve
creating multiple copies.

Merryland International School 3 2024-2025


Python practice Computer Science 9618

SN Methods with Description

1 [Link]()

Removes all elements of dictionary dict.

2 [Link]()

Returns a shallow copy of dictionary dict.

3 [Link]()

Create a new dictionary with keys from seq and values set to value.

4 [Link](key, default=None)

For key key, returns value or default if key not in dictionary.

5 dict.has_key(key)
Removed, use the in operation instead.

6 [Link]()
Returns a list of dict's (key, value) tuple pairs.

7 [Link]()
Returns list of dictionary dict's keys.

8 [Link](key, default=None)
Similar to get(), but will set dict[key]=default if key is not already in dict.

9 [Link](dict2)
Adds dictionary dict2's key-values pairs to dict.

10 [Link]()
Returns list of dictionary dict's values.

DICTIONARY METHODS

Merryland International School 4 2024-2025


Python practice Computer Science 9618

CLASS
In Python, a class is a blueprint for creating objects (a particular data structure).
Classes encapsulate data for the object and methods to manipulate that data. They
are a fundamental concept in Object-Oriented Programming (OOP), which allows
for modeling real-world entities and relationships in code.

Defining a Class in Python

To define a class in Python, you use the `class` keyword followed by the class name
(usually written in PascalCase). Inside the class, you can define attributes
(variables) and methods (functions).

Here's a simple example:

Key Components of a Class

1. Class Name: The name of the class, which is conventionally written in


PascalCase (e.g., `Dog`).

2. Attributes:
Merryland International School 5 2024-2025
Python practice Computer Science 9618

- Class Attributes: These are attributes shared by all instances of the class. They
are defined directly within the class and outside any instance methods. In the
example above, `species` is a class attribute.
- Instance Attributes: These are unique to each instance of a class and defined
within the `__init__` method. In the example, `name` and `age` are instance
attributes.

3. Methods:
- Instance Methods: Defined using `def`, instance methods have at least one
parameter (`self`), which represents the instance of the class. They are used to
manipulate instance attributes or perform operations using them. Examples in the
class above are `bark()` and `get_age()`.

4. __init__Method: Also known as the initializer or constructor method, `__init__` is


called automatically when a new instance of the class is created. It is used to
initialize the instance attributes of the class.

Creating an Object (Instance) of a Class

An object is an instance of a class. Once a class is defined, you can create objects
(instances) of that class.

Merryland International School 6 2024-2025


Python practice Computer Science 9618

Special Methods

Python classes can also include special methods, sometimes called magic methods
or dunder methods (double underscore). These methods allow you to define how
objects of the class behave with built-in Python functions and operators. Examples
include:
 __str__(self)`: Returns a string representation of the object.
 __len__(self)`: Returns the length of the object.
 __eq__(self, other)`: Defines equality behavior (`==`).
Example:

Inheritance

Merryland International School 7 2024-2025


Python practice Computer Science 9618

Python also supports inheritance, which allows a new class to inherit the attributes
and methods of an existing class. This promotes code reusability and logical
hierarchy.
Example:

LINEAR SEARCH

Merryland International School 8 2024-2025


Python practice Computer Science 9618

May/June 2021-9618/41

Merryland International School 9 2024-2025

You might also like