[go: up one dir, main page]

0% found this document useful (0 votes)
3 views58 pages

Introduction to python

This document provides an introduction to Python programming, specifically focusing on lists, tuples, and dictionaries. It covers how to create, access, and manipulate these data structures, including methods for adding, removing, and modifying elements. Additionally, it discusses concepts such as mutability, aliasing, and copying of data structures.

Uploaded by

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

Introduction to python

This document provides an introduction to Python programming, specifically focusing on lists, tuples, and dictionaries. It covers how to create, access, and manipulate these data structures, including methods for adding, removing, and modifying elements. Additionally, it discusses concepts such as mutability, aliasing, and copying of data structures.

Uploaded by

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

Introduction to Python

Programming Unit 2
Akshata S. Bhayyar
Assistant Professor, Dept. of CSE,
MSRIT
LIST
 A list is an ordered set of values, where each
value is identified by an index
 The values that make up a list are called its
elements.
 Also called as sequences
LIST VALUES
 Ways to create a new list
 enclose the elements in square brackets [ ]:

Eg1 [10, 20, 30, 40]

Eg2 ["spam", "bungee", "swallow"]

Eg3 ["hello", 2.7, 5]


LIST VALUES
 Lists that contain consecutive integers:

 Empty list it is denoted [].


ACCESSING ELEMENTS
ACCESSING ELEMENTS

If you try to read or write an


element that does not exist, you
get a runtime error:
ACCESSING ELEMENTS
 If an index has a negative value, it counts
backward from the end of the list:
Using Loops
LIST LENGTH
 The function len returns the length of a list
LIST within another LIST
 The function len returns the length of a list
List membership
 in is a boolean operator that tests
membership in a sequence.
List operations
 The + operator concatenates lists
List operations
 Similarly, the * operator repeats a list a given
number of times
List slices
Lists are mutable
 lists are mutable, which means we can
change their elements.
List deletion
Objects and values
Objects and values
Aliasing
Cloning lists
If we want to modify a list and also keep a copy
of the original
Cloning lists
If we want to modify a list and also keep a copy
of the original
List parameters
Nested lists
Matrices
sted lists are often used to represent matrices
Strings and lists
Strings and lists- delimiter
Strings and lists- join
List Methods
List Methods

1.append()
syntax
list.append (element)

['Mathematics', 'chemistry', 1997, 2000, 20544]


List Methods

insert()
syntax
list.insert(position,
element)

Mathematics', 'chemistry', 10087, 1997, 2000]


List Methods

extend()
syntax
List1.extend(List2)

[1, 2, 3, 2, 3, 4, 5]
[2, 3, 4, 5, 1, 2, 3, 2, 3, 4, 5
List Methods

sum()
syntax
sum(List)
List Methods

count()
syntax
List.count(element)

4
List Methods

Length()
syntax
len(list_name)

10
List Methods

index()
syntax

List.index(element[,start[,end]
])

1
List Methods
Deletion of List Elements
To Delete one or more elements, i.e. remove
an element, many built-in functions can be
used, such as pop() & remove() and
keywords such as del.

pop()
The index is not a necessary parameter, if
not mentioned takes the last index.

Syntax:
list.pop([index])
List Methods
List Methods

reverse()

1
List Methods

Sort( )
Pure functions and Modifiers
Pure functions : It does not modify any of
the objects passed to it as arguments and it
has no side effects, such as displaying a value
or getting user input.

Modifiers: the caller keeps a reference to


the objects it passes, so any changes the
function makes are visible to the caller.
Functions that work this way are called
modifiers.
Tuples
Tuple
 A tuple that is similar to a list except that it is
immutable.
 Syntactically, a tuple is a comma-separated list
of values:
Tuple
 To create a tuple with a single element, we have to
include the final comma:

>>> t1 = (’a’,)
• Without the comma, Python treats (’a’) as a string
in parentheses:
Tuple
If we try to modify one of the elements of
the tuple, we get an error:
Tuple packing and
unpacking
Tuple packing is nothing but the creation of
tuple, whereas tuple unpacking means to
extract tuple values and store them in individual
variables.
Tuple assignment
Tuple assignment
Tuples as return values
 Functions can return tuples as return values
Composability of Data
Structures
 Tuples items can themselves be other tuples.
Dictionary
Dictionaries
 In a dictionary, the indices are called keys, so
the elements are called key-value pairs.
 One way to create a dictionary is to start
with the empty dictionary and add elements.
 The empty dictionary is denoted {}
Dictionaries
Dictionaries
nother way to create a dictionary

print(len(Subjects)
)
Dictionary operations
1. The del statement removes a key-
value pair from a dictionary

2.
print(len(Subjects))
Dictionary methods
 A method is similar to a function, it
takes arguments and returns a value
but the syntax is different.

 print(Subjects.keys())
dict_keys([2, 3, 4])
Dictionary methods
 The items method returns both, in the
form of a list of tuples—one for each
key-value pair:

print(Subjects.items())

dict_items([(2, 'Physics'), (3,


'Electronics'), (4, 'Python')])
Dictionary methods
 the method has key takes a key and
returns true (1) if the key appears in
the dictionary:
Aliasing and copying
 Whenever two variables refer to the
same object, changes to one affect the
other.
 If you want to modify a dictionary and
keep a copy of the original, use the
copy method.

You might also like