Introduction to Python
Part 2
Dr Nisha Chaurasia
Python Part-2
What is Python?
Operators
Variables
Lists
If / Else
Loops
Unconditional Statement
Functions
Strings (Revisited)
Lists (Revisited)
Lists (Revisited)
List is an ordered sequence of items. Values in the list are called elements / items.
It can be written as a list of comma-separated items (values) between square
brackets[ ].
Items in the lists can be of different data types.
Lists can be understood as ordered, unchangeable (only elements can be
modified), heterogeneous (supports different data types) and contains duplicates.
Operations on list:
i. Indexing
ii. Slicing
iii. Concatenation
iv. Repetitions
Indexing/Slicing
Creating the list in different ways
Slicing Syntax:
list_varible(start:stop:step)
Accessing through loop
Accessing list items
Working with Lists
Create a Python Lists:
A list is created by placing elements inside square brackets [], separated by
commas.
We can use the built-in list() function to convert other iterables (strings,
dictionaries, tuples, etc.) to a list.
Access List Elements
Each element in a list is associated with a number, known as an index.
The index of first item is 0, the index of second item is 1, and so on.
Positive Indexing Negative Indexing
List slicing
Add Elements to a Python List
Use the append() method to add an element to the end of a Python list.
Use the insert() method to add an element at the specified index.
Use the extend() method to add elements to a list and add them at the end of the list.
Change List Items
The items of a list can be changed by assigning new values using the = operator.
The + operator can be used to concatenate two or more lists together
By using the * operator we can replicate our lists by the number of times we specify
Remove an Item From a List
Remove an item from a list using the remove() method
Single item removal
Multiple items removal
We can also use the del statement to delete the # deleting the entire
list del names
entire list.
Python List Length
Use the built-in len() function to find the number of elements in a list.
Iterating Through a List
Use a for loop to iterate over the elements of a list.
Element check in the list
Use the in keyword to check if an item exists in the list.
List Comprehension
List Comprehension is a concise and elegant way to create a list.
Syntax: [expression for item in list if condition == True]
List Methods
Nested List
The list can contain another list (sub-list), which in turn contains
another list and so on. This is termed a nested list.
Mutability in Lists
Mutability (can be changed) is
the ability for certain types of
data to be changed without
entirely recreating it.
An item can be changed in a
list by accessing it directly as
part of the assignment
statement.
Using the indexing operator
(square brackets[ ]) on the left
side of an assignment, one of
the list items can be updated
Aliasing (Copy) in Lists
Creating a copy of a list is
called aliasing.
Alaising refers to having
different names for same
list values.
When you create a copy Aliasing happens when multiple variables refer to
both the list will be having the same mutable object. Changes made to the
object through one alias are reflected in all other
same memory location. aliases, which can lead to unexpected behavior
Changes in one list will and bugs.
affect another list. Mutability can be dangerous in scenarios when you
use aliasing.
Cloning in Lists
To solve the issue of Aliasing, we can use cloning or deep
copy.
Cloning is a process of making a copy of the list without modifying the
original list.
Creating a copy of a same list of elements with two different memory
locations is called cloning.
To avoid the disadvantages of copying we are using cloning.
Changes in one list will not affect locations of another list.
We make a clone of an object, then it is stored in some other memory
location instead memory location of the original one, thus when we make
any changes in the original or copied object, it will not affect each other.
Need of Mutable and Immutable Objects:
Mutable objects are useful when you need to change or update the
data frequently. However, they can lead to unintended side effects if
not managed carefully, especially when passed to functions.
Immutable objects provide safety and predictability since their state
cannot be altered. These are helpful when you need to perform read-
only operations and do not want to change anything once written.
List as parameters(of functions)
In python, arguments are passed by reference.
If any changes are done in the parameter which refers within the
function, then the changes also reflects back in the calling function.
When a list to a function is passed, the function gets a reference to
the list.
Passing a list as an argument actually passes a reference to the list,
not a copy of the list.
Since lists are mutable, changes made to the elements referenced by
the parameter change the same list that the argument is referencing.
List as Arrays
Arrays:
An array is a special variable, which can hold more than one value (of same type)
at a time.
An element in array can be accessed by referring to the index number.
Lists can be converted to arrays using the built-in functions in the
Python numpy library.
numpy provides us with two functions to use when converting a list into an array:
• numpy.array()
• numpy.asarray()
numpy library takes a list as an argument and returns an array that contains all the
elements of the list.
While calling the numpy.array() function it is
passing copy=False. This is because by default the
value of copy in numpy.array() is true and this will
create a copy of the object and then it will convert
this object to an array. But on the other
hand numpy.asarray() will not create a copy of the
object and will convert the given list to an array.
Hence we should use numpy.asarray() when we
want to save memory.
This function calls the numpy.array() function inside itself.