Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Class notes

Tuples in python

Rating
-
Sold
-
Pages
35
Uploaded on
08-04-2025
Written in
2024/2025

This PDF contains comprehensive notes covering the fundamental concepts of Python programming. It is designed for beginners who are new to coding or looking to build a strong foundation in Python. The notes are organized in a clear and structured format, making them ideal for self-study, classroom learning, or quick reference.

Show more Read less
Institution
Course

Content preview

Python Application Programming (15CS664) Module III


MODULE III

3.1 LISTS
A list is a sequence, Lists are mutable, Traversing a list, List operations, List slices, List Methods,
Deleting elements, Lists and functions, Lists and strings, Parsing lines, Objects and values , Aliasing,
List arguments, Debugging


3.2 DICTIONARIES
Introduction, Dictionary as a set of counters, Dictionaries and files, Looping and Advanced text
parsing, Debugging


3.3 TUPLES
Tuples are immutable, Comparing tuples, Tuple assignment Dictionaries and tuples, Multiple
assignment with dictionaries, The most common words, Using tuples as keys in dictionaries, Sequences:
strings, lists, and tuples, Debugging

3.4 REGULAR EXPRESSIONS

Character matching in regular expressions, Extracting data using regular expressions, Combining searching
and extracting Escape character, Summary, Bonus section for Unix / Linux users




Mamatha A, Asst Prof, Dept of CSE, SVIT Page 1

, Python Application Programming (15CS664) Module III


MODULE III
3.1 LISTS
 A list is an ordered sequence of values.
 It is a data structure in Python. The values inside the lists can be of any type (like integer, float,
strings, lists, tuples, dictionaries etc) and are called as elements or items.
 The elements of lists are enclosed within square brackets.
 For example,
ls1=[10,-4, 25, 13]
ls2=[“Tiger”, “Lion”, “Cheetah”]

 Here, ls1 is a list containing four integers, and ls2 is a list containing three strings.
 A list need not contain data of same type.
 We can have mixed type of elements in list.
 For example,
ls3=[3.5, „Tiger‟, 10, [3,4]]

 Here, ls3 contains a float, a string, an integer and a list.
 This illustrates that a list can be nested as well.
 An empty list can be created any of the following ways –
>>> ls =[]
>>> type(ls)
<class 'list'>
or
>>> ls =list()
>>> type(ls)
<class 'list'>
 In fact, list() is the name of a method (special type of method called as constructor – which will be
discussed in Module 4) of the class list.
 Hence, a new list can be created using this function by passing arguments to it as shown below –

>>> ls2=list([3,4,1])
>>> print(ls2)
[3, 4, 1]

 Lists are Mutable
 The elements in the list can be accessed using a numeric index within square-brackets.
 It is similar to extracting characters in a string.

>>> ls=[34, 'hi', [2,3],-5]
>>> print(ls[1])
hi
>>> print(ls[2])
[2, 3]


Mamatha A, Asst Prof, Dept of CSE, SVIT Page 2

,Python Application Programming (15CS664) Module III

 Observe here that, the inner list is treated as a single element by outer list. If we would like to
access the elements within inner list, we need to use double-indexing as shown below –

>>> print(ls[2][0]) 2
>>> print(ls[2][1]) 3

 Note that, the indexing for inner-list again starts from 0.
 Thus, when we are using double- indexing, the first index indicates position of inner list inside
outer list, and the second index means the position particular value within inner list.
 Unlike strings, lists are mutable. That is, using indexing, we can modify any value within list.
 In the following example, the 3rd element (i.e. index is 2) is being modified –

>>> ls=[34, 'hi', [2,3],-5]
>>> ls[2]='Hello'
>>> print(ls)
[34, 'hi', 'Hello', -5]
 The list can be thought of as a relationship between indices and elements. This relationship is
called as a mapping. That is, each index maps to one of the elements in a list.
 The index for extracting list elements has following properties –

 Any integer expression can be an index.
>>> ls=[34, 'hi', [2,3],-5]
>>> print(ls[2*1])
[2,3]
 Attempt to access a non-existing index will throw and IndexError.
>>> ls=[34, 'hi', [2,3],-5]
>>> print(ls[4])
IndexError: list index out of range

 A negative indexing counts from backwards.
>>> ls=[34, 'hi', [2,3],-5]
>>> print(ls[-1])
-5
>>> print(ls[-3])
hi

 The in operator applied on lists will results in a Boolean value.
>>> ls=[34, 'hi', [2,3],-5]
>>> 34 in ls
True
>>> -2 in ls
False

 Traversing a List
 A list can be traversed using for loop.
 If we need to use each element in the list, we can use the for loop and in operator as below
>>> ls=[34, 'hi', [2,3],-5]

Mamatha A, Asst Prof, Dept of CSE, SVIT Page 3

, Python Application Programming (15CS664) Module III

>>> for item in ls:
print(item)

34
hi
[2,3]
-5
 List elements can be accessed with the combination of range() and len() functions as well –

ls=[1,2,3,4]
for i in range(len(ls)):
ls[i]=ls[i]**2

print(ls)

#output is
[1, 4, 9, 16]

 Here, we wanted to do modification in the elements of list. Hence, referring indices is suitable
than referring elements directly.
 The len() returns total number of elements in the list (here it is 4).
 Then range() function makes the loop to range from 0 to 3 (i.e. 4-1).
 Then, for every index, we are updating the list elements (replacing original value by its square).

 List Operations
 Python allows to use operators + and * on lists.
 The operator + uses two list objects and returns concatenation of those two lists.
 Whereas * operator take one list object and one integer value, say n, and returns a list by repeating
itself for n times.

>>> ls1=[1,2,3]
>>> ls2=[5,6,7]
>>> print(ls1+ls2) #concatenation using +
[1, 2, 3, 5, 6, 7]

>>> ls1=[1,2,3]
>>> print(ls1*3) #repetition using *
[1, 2, 3, 1, 2, 3, 1, 2, 3]

>>> [0]*4 #repetition using *
[0, 0, 0, 0]


 List Slices
 Similar to strings, the slicing can be applied on lists as well. Consider a list t given below, and a
series of examples following based on this object.


Mamatha A, Asst Prof, Dept of CSE, SVIT Page 4

Written for

Course

Document information

Uploaded on
April 8, 2025
Number of pages
35
Written in
2024/2025
Type
Class notes
Professor(s)
Bayregowda
Contains
All classes

Subjects

$6.99
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
sanathmogaveer13

Also available in package deal

Get to know the seller

Seller avatar
sanathmogaveer13 VTU
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
1 year
Number of followers
0
Documents
5
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions