0% found this document useful (0 votes)
35 views42 pages

PSP Presentation

I presented this to my class. It is on Lists in Python Programming. One can also take ideas about animations that I used to make it look more presentable

Uploaded by

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

PSP Presentation

I presented this to my class. It is on Lists in Python Programming. One can also take ideas about animations that I used to make it look more presentable

Uploaded by

adx.222005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 42

Python

Datatypes
Classes:- Classes:-
Mapping/ Classes:- list, tuple,
Dictionary Int,float, string
Descripti Classes:- complex Description:-
on Set, Description:- Holds
Holds Data frozenset Holds collection of
In key Descriptio numeric value Items
value n:- Classes:-bool
Pair form Hold
Description:-
collection
of Holds either
Unique True or False
items
SEQUENCE DATA-TYPE
Types of Sequences:

•Lists: Mutable, can store heterogeneous


elements.
•Tuples: Immutable, used for fixed
collections of items.
•Strings: Immutable, sequence of
characters.
LIST()
Definition of List in Python:-
a list is a built-in data structure that
allows you to store an ordered collection
LIST () of items. Lists can store elements of
different data types, such as integers,
strings, or even other lists
General Syntax for Lists

list_name = [item1, item2, item3, ...]


LIST () •Explanation:
•list_name: Name of the list
variable.
•[ ]: Square brackets denote a list.
•item1, item2, ...: Elements
separated by commas.
Features of Python Lists
1. Ordered Elements in a list maintain their insertion
order
2. Mutable Lists can be modified after creation by
adding, removing, or changing elements

3. Heterogeneous
Data
A single list can store items of different
data types
LIST () 4. Dynamic Size Lists can grow or shrink as needed,
without predefined limits
5. Duplicates Lists can contain duplicate elements
Allowed

6. Iterable Lists can be traversed using loops like


`for` or comprehensions
7. Supports Nesting Lists can contain other lists, creating
multi-dimensional data structures
Uses of Python Lists
1. Storing Collections of
Multi-dimensional Data
Data
Representation:
- Store multiple items in
- Store tabular data or
a single variable
matrices.
Data Processing:
- Used in algorithms for Dynamic Data Handling:
storing temporary results, LIST () - Collect data whose size isn't
like sorting, filtering, or known in advance, such as user
reducing data. inputs or API responses

Data Analysis and


Data Transformation:
Visualization:
- Convert other data types
- Lists are fundamental in
(strings, tuples, sets) to lists
libraries like NumPy, pandas,
for easier manipulation
and Matplotlib
Uses of Python Lists

Queue or Stack Operations: Efficient Iteration and Filtering:


- Use `.append()` and - List comprehensions enable
`.pop()` to mimic stack
behavior
LIST () concise and efficient loops.

Combining and Modifying Data:


- Combine lists using concatenation or
extend existing lists.
- Example: `list1 + list2` or
`list1.extend(list2)`.
Steps to create a

LIST ()

In Python
Steps to create LIST
a In Python
To create a list in Python, Here's what a
we use square brackets list looks
([])and separate each like:
item with a comma.
Items in a list can be ListName =
any basic object type [ListItem,
found in Python, ListItem1,ListIt
including integers, em2,
strings, floating point
values or boolean ListItem3, ...]
values.
Steps to create LIST
a In Python
To create a list in Python, For example, to
we use square brackets create a list
([])and separate each named
item with a comma.
Items in a list can be “z” that holds the
any basic object type integers 3, 7, 4
found in Python, and 2,
including integers, you would write:
strings, floating point
values or boolean # Define a list
values.
Steps to create LIST
a In Python
The “z” list defined # Define a list
above has items that
are all of the same Heterogenous
type (integer or int), Elements =
but as mentioned, all
the items in a list do
[3, True,
not need to be of the 'Michael',
same type as you can 2.0]
see on the next page.
Range in Lists

LIST ()
The range() function generates a
sequence of numbers, which is often
used to create or manipulate lists.
LIST ()
range(n) creates list from numbers 0 to
n-1
example: numbers= list(range(10))
output: [1, 2, 3, 4, 5, 6, 7, 8, 9]
Comprehensions in Lists

LIST ()
List comprehensions provide a concise
way to create and manipulate lists. They
consist of an expression followed by a for
LIST () clause, optionally with additional if
clauses.
Syntax: [<expression> for <item> in <if
condition>]
Examples:

squares = [x**2 for x in range(5)] output: [0,


1, 4, 9, 16]
even_squares = [x**2 for x in range(10) if x %
LIST () 2 == 0] output: [0, 4, 16, 36, 64]
names = ["Jake", "Boyle", "Rosa", ”Rosa”]

uppernames =[name.upper() for name


in names] output: [“JAKE”, “BOYLE”, “AMY”,
“ROSA”]
Lists with different Data Types

LIST ()
Lists with different Data Types
Lists in Python can hold items of different
data types, including integers, strings, floats,
LIST () Booleans, and even other lists or objects.
Examples:
mixed_list = [42, "hello", 3.14, True]
print(mixed_list) Output: [42, 'hello', 3.14,
True]
LIST ()

nested_list = [1, ["apple",


"banana"], [True, False]]
print(nested_list[1][0]) Output:
apple
LIST ()

Common List Methods:


Python provides a wide range of
built-in methods to manipulate lists.
Here's a look at commonly used list
methods with examples.
Method Description Syntax Example Output
LIST
()
append() Adds an element list.append(element) fruits =
["apple"];
['apple',
to the end of the 'banana']
fruits.append
list. ("banana");
print(fruits)

extend() Adds all list.extend(iterable) numbers = [1, [1, 2, 3,


2];
elements of an 4]
numbers.exte
iterable to the nd([3, 4]);
list. print(number
s)

insert() Inserts an list.insert(index, letters = ["a", ['a', 'b',


"c"];
element at the element) 'c']
letters.insert(
specified index. 1, "b");
print(letters)
Method Description Syntax Example Out
LIST put
() remove() Removes the
first occurrence
list.remove(element) colors = ["red",
"green", "red"];
['gree
n',
colors.remove("re
of the specified d"); print(colors)
'red']
element.

pop() Removes and list.pop([index]) numbers = [1, 2,


3]; last =
3 [1, 2]
returns the
numbers.pop();
element at the print(last,
specified index numbers))
(default is last).

index() Returns the index list.index(element, fruits = ["apple", 1


of the first [start, end]) "banana"];
print(fruits.index(
occurrence of the "banana"))
specified
element.
count() Counts the list.count(element) numbers = [1, 2, 1]; 2
LIST occurrences
of a
print(numbers.count(1))

() specified
element in
the list.

sort() Sorts the list list.sort([key=None, numbers = [3, 1, 2]; [1, 2, 3]


in ascending reverse=False]) numbers.sort();
order (or print(numbers)
descending
if
reverse=True
).

reverse() Reverses the list.reverse() letters = ["a", "b"]; ['b', 'a']


order of the letters.reverse();
list. print(letters)
copy() Creates a new_list = list.copy() original = [1, 2]; copy_list = [1, 2]
shallow copy original.copy();
of the list. print(copy_list)
Method Description Syntax Example Out
LIST put
() clear() Removes all list.clear(element) items = [1, 2];
items.clear();
[]
elements from
the list. print(items)

len() Returns the len(list) fruits = ["apple",


"banana"];
2
number of print(len(fruits))
elements in
the list.

del Deletes an del list[index] fruits = ['ban


element or slice ["apple", ana']
of the list (not a "banana"]; del
method but a fruits[0];
keyword). print(fruits)
LIST
() Iterate over a list
Python provides several ways to
iterate over list. The simplest and
the most common way to iterate
over a list is to use a for loop. This
method allows us to access each
element in the list directly.
LIST Using for Loop
()
We can use the range() method with
for loop to traverse the list. This
method allow us to access elements
by their index, which is useful if we
need to know the position of an
element or modify the list in place.

a = [1, 3, 5, 7, 9]
LIST # Calculate the length of the
list
()
n = len(a)

# Iterates over the indices


from 0 to n-1 (i.e., 0 to 4)
for i in range(n):
print(a[i])
Output
13579
LIST Using while Loop
This method is similar to the above method.
() Here we are using a while loop to iterate
through a list. We first need to find the
length of list using len(), then start at index
0 and access each item by its index then
incrementing the index by 1 after each
iteration.
a = [1, 3, 5, 7, 9]

# Start from the first index


i=0

# The loop runs till the last index (i.e., 4)


while i < len(a):
print(a[i])
i += 1
LIST Using List Comprehension
List comprehension is similar to for loop. It
() provides the shortest syntax for looping
through list.
a = [1, 3, 5, 7, 9]

# On each iteration val is passed to print


function

# And printed in the console.


[print(val) for val in a]
Output
13579

Note: This method is not a recommended


way to iterate through lists as it creates a
new list (extra space).
Nesting of list
LIST
() A nested list in Python refers to a list that
contains other lists as its elements. In
simpler terms, it is a list where each item
inside the main list can be another list, or
another list of lists, and so on. Nested lists
are useful for organizing data in hierarchical
structures or when working with
multidimensional data.
Example of a Nested List:
nested_list = [ [1, 2, 3],
# First sublist [4, 5, 6],
# Second sublist [7, 8, 9]
# Third sublist]
LIST () In this example, nested_list is a list containing three
sublists. Each sublist has its own set of elements.
Accessing Elements in a Nested List:
To access elements inside a nested list, you use
multiple indices, where each index accesses a
deeper level of the list.nested_list =
[ [1, 2, 3], [4, 5, 6], [7, 8, 9]
Practical Applications
1 . Managing To-Do Lists

- You can store tasks in a list and update them as needed.


- Example: ```python
tasks = ["Study Python", "Do laundry", "Call a friend"]
print(tasks[0]) # Output: Study Python

2. Storing Marks or Scores


LIST () Keep track of marks for different subjects or scores in a game.
- Example: ```python
marks = [85, 90, 78, 92] print("Highest marks:", max(marks))

3. Handling Shopping Lists


- Use lists to store items you need to buy.
- Example:
```python
shopping_list = ["Milk", "Bread", "Eggs"]
shopping_list.append("Butter")
print(shopping_list) # Output: ['Milk', 'Bread', 'Eggs', 'Butter']
Advantages of Python Lists:
LIST 1. Ease of Use:
() - Simple syntax and a wide range of built-in methods
make lists easy to use for beginners and experts alike.
- Example: `append()`, `remove()`, `sort()`.
2. Dynamic Size:
- Lists can grow or shrink in size dynamically without
requiring predefined size declarations.
- Example: `my_list.append(5)` adds a new element
to the list.
3. Heterogeneous Data Support:
LIST - Can store elements of different data types in the same list.

() - Example: `my_list = [1, "hello", 3.14]`.


4. Ordered and Indexable:
- Maintain the order of elements, and elements can be accessed by their
indices.
- Example: `my_list[0]` retrieves the first element.
5. Flexibility:
- Support for nesting allows creation of multi-dimensional lists.
- Example: `nested_list = [[1, 2], [3, 4]]`.
6. Iterable:
- Easily loop through elements using `for` loops or comprehensions.
- Example: `[x**2 for x in range(5)] → [0, 1, 4, 9, 16]`.
7. Wide Applications:
LIST - Used in various domains, such as data processing, string manipulation, and
algorithms.
() - Example: Represent stacks or queues using `append()` and `pop()`.

### **Limitations of Python Lists**:


1. **Performance for Large Data**:
- Lists are not as fast as specialized data structures like NumPy arrays for
numerical computations.
- Example: Operations on large lists can be slower due to Python’s overhead.
2. **Memory Usage**:
- Lists use more memory compared to fixed-size arrays because they store
references to objects rather than the objects themselves.
3. **Fixed-Type Data Operations**:
LIST
7

- Lists are less efficient for mathematical operations due


() to their ability to hold heterogeneous data. Libraries like
NumPy are better for homogeneous numerical data.
4. **No Direct Hashing**:
- Lists are not hashable and cannot be used as keys in dictionaries.

- Example: `{[1, 2]: "value"}` will raise a `TypeError`.

5. **Slower in Search Operations**:


- Linear search (`O(n)`) is the only way to find an element in a list,
making it slower than sets or dictionaries.
6. **Not Thread-Safe**:
LIST - Lists are not safe for multi-threaded operations without
() explicit synchronization.

7. **Insertion and Deletion Costs**:


- Adding or removing elements in the middle of a list can
be slow because other elements need to be shifted.
- Example: `list.insert(1, 10)` involves shifting elements to
accommodate the new item

-
CONCLUSION: ListPython lists are very flexible and can
LIST hold arbitrary data.Lists are a part of Python's syntax, so
() they do not need to be declared first.Lists can also be re-
sized quickly in a time-efficient manner. This is because
Python initializes some extra elements in the list at
initialization.Lists can hold heterogeneous
data.Mathematical functions cannot be directly applied to
lists. Instead, they have to be individually applied to each
element.Lists consume more memory as they are allocated
a few extra elements to allow for quicker appending of
items.
-
LIST ()
Thank You!
Team Members:
•Atharva Dixit - 24BCE10677
•Aastha Krishna - 24BCE10672
•Diva Chandra - 24BSA10066
•Sujal Bedarkar - 24BSA10304
•Keshav Sharma - 24BCE11506
We appreciate your time and
attention!

You might also like