You are on page 1of 24

The Yenepoya Institute of Arts, Science,

Commerce and Management (YIASCM)

Course: BCA BIG DATA- LH 19

BCA303 : Python Programming

Unit-3 PART-1 Jamuna K M


Dept.of Computer Science, YIASCM

1
Objective
• Upon completion of this session the leaner
will able to understand
 Lists in python introduction
Introduction
• The list is a most versatile data-type available in
Python which can be written as a list of comma-
separated values (items) between square brackets.
• Important thing about a list is that items in a list
need not be of the same type.
• Creating a list is as simple as putting different
comma-separated values between square brackets.
• For example −
list1 = ['physics', 'chemistry', 1997, 2000]; list2 = [1,
2, 3, 4, 5 ]; list3 = ["a", "b", "c", "d"]
Syntax
• In many programming languages, you can
create a list by using square brackets [] and
separating elements with commas.
• For example, in Python:
my_list = [1, 2, 3, 4, 5]
Empty Lists
• You can create an empty list by using empty
square brackets:
empty_list = []
Dynamic Sizing
• Lists are often dynamically sized, meaning
they can grow or shrink as needed to
accommodate elements.
Indexing
• Elements in a list are accessed using their
index. Indexing usually starts at 0.
• For example, to access the first element in a
Python list:
first_element = my_list[0]
Negative Indexing
• Some programming languages support
negative indexing, where -1 refers to the last
element, -2 to the second-to-last, and so on.
Length
• You can determine the number of elements in
a list using the len() function or a similar
method provided by your programming
language.
length = len(my_list)
Mixed Data Types
• Lists can contain elements of different data
types.
• For instance, a Python list can hold integers,
strings, and other data types in the same list.
mixed_list = [1, "apple", True, 3.14]
Appending Elements
• You can add elements to the end of a list using
the append() method (in Python) or similar
methods in other languages.
my_list.append(6)
Inserting Elements
• You can insert an element at a specific
position in a list using the insert() method (in
Python) or equivalent functions in other
languages.
my_list.insert(2, 7) # Inserts 7 at index 2
Concatenation
• Lists can be combined by concatenating them
together.
combined_list = list1 + list2
Copying Lists
• Be cautious when copying lists, as some
languages might create a reference (shallow
copy) rather than a new copy (deep copy).
• To create a new copy, you may need to use a
specific method or library function.
List Comprehensions
• Many programming languages provide list
comprehensions, a concise way to create lists
using a single line of code.
• They are particularly useful for transforming
or filtering elements.
squares = [x**2 for x in my_list]
Immutable Lists
• Some languages offer immutable lists, where
you cannot change the elements once the list
is created.
• Immutable lists have benefits in functional
programming and multi-threaded
environments.
Arrays vs. Linked Lists
• In some programming languages, like Python,
lists are implemented as dynamic arrays,
which provide fast random access but may
require copying when resizing.
• In contrast, linked lists allow for efficient
insertions and deletions but have slower
random access.
Accessing Values in Lists
• To access values in lists, use the square
brackets for slicing along with the index or
indices to obtain value available at that index.
• For example −
list1 = ['physics', 'chemistry', 1997, 2000]; list2 =
[1, 2, 3, 4, 5, 6, 7 ]; print "list1[0]: ", list1[0] print
"list2[1:5]: ", list2[1:5]
Output
list1[0]: physics
list2[1:5]: [2, 3, 4, 5]
Basic List Operations
• Lists respond to the + and * operators much
like strings; they mean concatenation and
repetition here too, except that the result is a
new list, not a string.
Summary
• Understanding how to create and manipulate
lists is crucial for working with collections of
data in programming, and it serves as a
foundation for more advanced data structures
and algorithms.
Program
• Do the following operations using list in python
a. creating
b. accessing
c. modifying
d. appending,
e. extending,
f. inserting,
g. removing,
h. popping,
i. slicing,
j. searching,
k. sorting,
l. reversing,
m. getting the length,
n. clearing
Reference
• Brown, F., & George, P. (2001). Python: the
complete reference.
• Matthes, E. (2019). Python crash course: A
hands-on, project-based introduction to
programming. no starch press

You might also like