Python Chapter7 List.pdf
Python Chapter7 List.pdf
Classes
YoganandSharma
tu
Python has six built-in types of sequences, but the
most common ones are lists and tuples, which we
S
would see in this tutorial.
D
There are certain things you can do with all sequence
JM
types. These operations include indexing, slicing,
adding, multiplying, and checking for membership. In
addition, Python has built-in functions for finding the
length of a sequence and for finding its largest and
smallest elements.
Python Lists
Python Notes 1
Web Designing, Digital Marketing, SEO, SMO, C, C++. Call: 9649141215, www.jmdstudy.com
Python-Lists
Project Training in Python, Android, PHP, ASP.NET, Machine Learning, SQL, JAVA,
YoganandSharma
by Training.
y
separated values (items) between square brackets.
Training
d
Important thing about a list is that items in a list need
u
MCA/ M.Tech/ B.Tech/ BCA Internship/ Industrial/
St
Creating a list is as simple as putting different
D
comma-separated values between square brackets.
For example −
JM
list1 = ['physics', 'chemistry', 1997, 2000];
list2 = [1, 2, 3, 4, 5 ];
list3 = ["a", "b", "c", "d"]
Python Notes 2
Assistance Python-Lists:Accesing
TrainingClasses
YoganandSharma
y
by
D
list1 = ['physics', 'chemistry', 1997, 2000];
JM
list2 = [1, 2, 3, 4, 5, 6, 7 ];
print ("list1[0]: ", list1[0])
print ("list2[1:5]: ", list2[1:5])
Python Notes 3
MCA/ M.Tech/ B.Tech/ BCA Internship/ Industrial/ Live Project
Training by Training.
Project Training in Python, Android, PHP, ASP.NET, Machine Learning, SQL, JAVA,
YoganandSharma
Web Designing, Digital Marketing, SEO, SMO, C, C++. Call: 9649141215, www.jmdstudy.com
+91-9928016573
output:
list1[0]: physics
J
list2[1:5]: [2, 3, 4, 5]
M
D
Study
Python-Lists: Accesing
Python Notes
4
Placement Python-Lists: updating
Classes
YoganandSharma
Updating Lists
Training > Project >+91-9928016573
You can update single or multiple elements of lists by
y
Training by
tu
a list with the append() method. For example −
S
list = ['physics', 'chemistry', 1997, 2000];
D
print "Value available at index 2 : "
print list[2]
list[2] = 2001;JM
print "New value available at index 2 : "
print list[2]
Python Notes 5
MCA/ M.Tech/ B.Tech/ BCA Internship/ Industrial/ Live Project
Training by Training.
Project Training in Python, Android, PHP, ASP.NET, Machine Learning, SQL, JAVA,
YoganandSharma
Web Designing, Digital Marketing, SEO, SMO, C, C++. Call: 9649141215, www.jmdstudy.com
+91-9928016573
2001
1997
output:
JM
D
Value available at index 2 :
St
New value available at index 2 :
udy
Python Notes
Python-Lists: updating list
6
Assistance Python-Lists:Delete
TrainingClasses
YoganandSharma
y
by
Python Notes 7
MCA/ M.Tech/ B.Tech/ BCA Internship/ Industrial/ Live Project
Training by Training.
Project Training in Python, Android, PHP, ASP.NET, Machine Learning, SQL, JAVA,
YoganandSharma
Web Designing, Digital Marketing, SEO, SMO, C, C++. Call: 9649141215, www.jmdstudy.com
+91-9928016573
output:
JM
D
S
['physics', 'chemistry', 2000].
t
After deleting value at index 2 :
u
Python-Lists:Delete
['physics', 'chemistry', 1997, 2000]
dy
Python Notes
8
Placement Python-Basic List Operations
Classes
YoganandSharma
y
Training by
tu
S
In fact, lists respond to all of the general sequence
D
operations we used on strings in the prior chapter.
JM
Python Notes 9
MCA/ M.Tech/ B.Tech/ BCA Internship/ Industrial/ Live Project
Training by Training.
Project Training in Python, Android, PHP, ASP.NET, Machine Learning, SQL, JAVA,
Web Designing, Digital Marketing, SEO, SMO, C, C++. Call: 9649141215, www.jmdstudy.com YoganandSharma
+91-9928016573
['Hi!'] * 4
3 in [1, 2, 3]
len([1, 2, 3])
[1, 2, 3] + [4, 5, 6]
Python Expression
True
Python-Lists
123
M
D
S
[1, 2, 3, 4, 5, 6]
Results
t
['Hi!', 'Hi!', 'Hi!', 'Hi!']
udy
Python Notes
Length
Iteration
Repetition
Membership
Concatenation
Description
10
Assistance Python-Lists
TrainingClasses
YoganandSharma
y
by
L[2]
L[-2]
JM
SPAM!
Spam
Offsets start at zero
Python Notes 11
Web Designing, Digital Marketing, SEO, SMO, C, C++. Call: 9649141215, www.jmdstudy.com
Python-Lists
Project Training in Python, Android, PHP, ASP.NET, Machine Learning, SQL, JAVA,
YoganandSharma
by Training.
y
the same way for lists as they do for strings.
Training
L[2]
L[-2]
JM
SPAM!
Spam
Offsets start at zero
Python Notes 12
Placement Python-function-len()
Classes
YoganandSharma
Description
Training > Project >+91-9928016573
The len()method returns the number of elements in
y
Training by
the list.
d
JMD Study Computer
Syntax
tu
S
Following is the syntax for len()method −
len(list) D
Parameters
JM
list−This is a list for which number of elements to
be counted.
Python Notes 13
MCA/ M.Tech/ B.Tech/ BCA Internship/ Industrial/ Live Project
Training by Training.
Project Training in Python, Android, PHP, ASP.NET, Machine Learning, SQL, JAVA,
Web Designing, Digital Marketing, SEO, SMO, C, C++. Call: 9649141215, www.jmdstudy.com YoganandSharma
+91-9928016573
3
list.
Result
Example
Return Value
print (len(list1))
following result −
JM
D
Stud
Python-function-len()
Python Notes
When we run above program, it produces the
This method returns the number of elements in the
14
Assistance Python-function-max()
TrainingClasses
YoganandSharma
Description
+91-9928016573
y
by
Syntax
tu
S
Following is the syntax for max()method −
max(list) D
Parameters
JM
list−This is a list from which max valued element to
be returned.
Python Notes 15
Web Designing, Digital Marketing, SEO, SMO, C, C++. Call: 9649141215, www.jmdstudy.com
Python-function-max()
Project Training in Python, Android, PHP, ASP.NET, Machine Learning, SQL, JAVA,
YoganandSharma
Return Value
by Training.
+91-9928016573
maximum value.
y
Training
Example
ud
MCA/ M.Tech/ B.Tech/ BCA Internship/ Industrial/
St
list1, list2 = ['C++','Java', 'Python'], [456, 700, 200]
print ("Max value element : ", max(list1))
D
Result
JM
When we run above program, it produces the following
result −
Max value element : Python
Max value element : 700
Python Notes 16
Web Designing, Digital Marketing, SEO, SMO, C, C++. Call: 9649141215, www.jmdstudy.com
Python-function-min()
Project Training in Python, Android, PHP, ASP.NET, Machine Learning, SQL, JAVA,
YoganandSharma
Description
by Training.
y
Training
Syntax
ud
MCA/ M.Tech/ B.Tech/ BCA Internship/ Industrial/
min(list)
St
Following is the syntax for min()method −
Parameters D
JM
list−This is a list from which min valued element to be
returned.
Return Value
This method returns the elements from the list with
minimum value.
Python Notes 17
Placement Python-function-min()
Classes
YoganandSharma
Example
Training > Project >+91-9928016573
list1, list2 = ['C++','Java', 'Python'], [456, 700, 200]
y
Training by
tu
Result S
D
When we run above program, it produces the
J
following result −M
min value element : C++
min value element : 200
Python Notes 18
Web Designing, Digital Marketing, SEO, SMO, C, C++. Call: 9649141215, www.jmdstudy.com
Python-function-list()
Project Training in Python, Android, PHP, ASP.NET, Machine Learning, SQL, JAVA,
YoganandSharma
by Training.
Description
+91-9928016573
y
them to lists. This is used to convert a given tuple
Training
into list.
ud
MCA/ M.Tech/ B.Tech/ BCA Internship/ Industrial/
Syntax
St
Following is the syntax for list()method −
list( seq) D
Parameters JM
seq−This is a tupleor string to be converted into
list.
Python Notes 19
Assistance Python-function-list()
TrainingClasses
YoganandSharma
Example
+91-9928016573
y
by
method.
d
JMD Study Computer
Live Project & 100%Job
JM
list2 = list(str)
print ("List elements : ", list2)
Python Notes 20
MCA/ M.Tech/ B.Tech/ BCA Internship/ Industrial/ Live Project
Training by Training.
Project Training in Python, Android, PHP, ASP.NET, Machine Learning, SQL, JAVA,
YoganandSharma
Web Designing, Digital Marketing, SEO, SMO, C, C++. Call: 9649141215, www.jmdstudy.com
+91-9928016573
Result
following result −
JM
D
Stud
Python-function-list()
Python Notes
List elements : [123, 'C++', 'Java', 'Python']
When we run above program, it produces the
List elements : ['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd']
21
Placement Python-Methods-append()
Classes
YoganandSharma
Description
Training > Project >+91-9928016573
The append()method appends a passed objinto the
y
Training by
existing list.
d
JMD Study Computer
Syntax
tu
Following is the syntax for append()method −
list.append(obj) S
Parameters D
JM
obj−This is the object to be appended in the list.
Return Value
This method does not return any value but updates
existing list.
Python Notes 22
Web Designing, Digital Marketing, SEO, SMO, C, C++. Call: 9649141215, www.jmdstudy.com
Python-Methods-append()
Project Training in Python, Android, PHP, ASP.NET, Machine Learning, SQL, JAVA,
YoganandSharma
Example
by Training.
+91-9928016573
method.
y
Training
ud
MCA/ M.Tech/ B.Tech/ BCA Internship/ Industrial/
list1.append('C#')
St
list1 = ['C++', 'Java', 'Python']
D
print ("updated list : ", list1)
Result JM
When we run above program, it produces the
following result −
updated list : ['C++', 'Java', 'Python', 'C#']
Python Notes 23
Assistance Python-Methods-count()
TrainingClasses
YoganandSharma
Description
+91-9928016573
y
by
Syntax
tu
Following is the syntax for count()method −
list.count(obj) S
Parameters D
JM
obj−This is the object to be counted in the list.
Return Value
This method returns count of how many times obj
occurs in list.
Python Notes 24
Web Designing, Digital Marketing, SEO, SMO, C, C++. Call: 9649141215, www.jmdstudy.com
Python-Methods-count()
Project Training in Python, Android, PHP, ASP.NET, Machine Learning, SQL, JAVA,
YoganandSharma
Example
by Training.
+91-9928016573
method.
y
Training
ud
aList= [123, 'xyz', 'zara', 'abc', 123];
MCA/ M.Tech/ B.Tech/ BCA Internship/ Industrial/
St
print ("Count for 123 : ", aList.count(123))
print ("Count for zara: ", aList.count('zara'))
D
Result
JM
When we run above program, it produces the following
result −
Count for 123 : 2
Count for zara: 1
Python Notes 25
Placement Python-Methods-extends()
Classes
YoganandSharma
Description
The extend()method appends the contents of seqto
Training > Project >+91-9928016573
list.
y
Training by
Syntax
d
JMD Study Computer
tu
Following is the syntax for extend()method −
list.extend(seq)
S
Parameters D
JM
seq−This is the list of elements
Return Value
This method does not return any value but add the
content to existing list.
Python Notes 26
Web Designing, Digital Marketing, SEO, SMO, C, C++. Call: 9649141215, www.jmdstudy.com
Python-Methods-extends()
Project Training in Python, Android, PHP, ASP.NET, Machine Learning, SQL, JAVA,
YoganandSharma
Example
by Training.
+91-9928016573
method.
y
Training
ud
list1 = ['physics', 'chemistry', 'maths']
MCA/ M.Tech/ B.Tech/ BCA Internship/ Industrial/
list2 = list(range(5))
list1.extend(list2) St
D
print ('Extended List :', list1)
Result JM
When we run above program, it produces the following
result −
Extended List : ['physics', 'chemistry', 'maths', 0, 1, 2, 3, 4]
Python Notes 27
Assistance Python-Methods-index()
TrainingClasses
YoganandSharma
Description
The index()method returns the lowest index in list that
+91-9928016573
objappears.
y
by
d
JMD Study Computer
Syntax
Live Project & 100%Job
tu
Following is the syntax for index()method −
list.index(obj)
S
Parameters D
JM
obj−This is the object to be find out.
Return Value
This method returns index of the found object otherwise
raise an exception indicating that value does not find.
Python Notes 28
Web Designing, Digital Marketing, SEO, SMO, C, C++. Call: 9649141215, www.jmdstudy.com
Python-Methods-index()
Project Training in Python, Android, PHP, ASP.NET, Machine Learning, SQL, JAVA,
YoganandSharma
Example
by Training.
method.
Live Project
y
Training
d
list1 = ['physics', 'chemistry', 'maths']
u
MCA/ M.Tech/ B.Tech/ BCA Internship/ Industrial/
St
print ('Index of chemistry', list1.index('chemistry'))
print ('Index of C#', list1.index('C#'))
Result D
JM
When we run above program, it produces the following
result −
Index of chemistry 1
Traceback(most recent call last): File "test.py", line 3, in
<module> print ('Index of C#', list1.index('C#'))
ValueError: 'C#' is not in list
Python Notes 29
Placement Python-Methods-insert()
Classes
YoganandSharma
Description
Training > Project >+91-9928016573
The insert()method inserts object objinto list at offset
index.
y
Training by
Syntax
d
JMD Study Computer
list.insert(index, obj) tu
Following is the syntax for insert()method −
Parameters S
D
index−This is the Index where the object objneed to
be inserted.
JM
obj−This is the Object to be inserted into the given list.
Return Value
This method does not return any value but it inserts the
given element at the given index.
Python Notes 30
Web Designing, Digital Marketing, SEO, SMO, C, C++. Call: 9649141215, www.jmdstudy.com
Python-Methods-insert()
Project Training in Python, Android, PHP, ASP.NET, Machine Learning, SQL, JAVA,
YoganandSharma
Example
by Training.
+91-9928016573
method.
y
Training
ud
MCA/ M.Tech/ B.Tech/ BCA Internship/ Industrial/
St
list1 = ['physics', 'chemistry',
'maths'] list1.insert(1, 'Biology')
D
print ('Final list : ', list1)
Result JM
When we run above program, it produces the
following result −
Final list : ['physics', 'Biology', 'chemistry', 'maths']
Python Notes 31
Assistance Python-Methods-pop()
TrainingClasses
YoganandSharma
Description
+91-9928016573
Syntax
d
JMD Study Computer
Live Project & 100%Job
list.pop(obj= list[-1]) tu
Following is the syntax for pop()method −
S
Parameters D
JM
obj−This is an optional parameter, index of the object to
be removed from the list.
Return Value
This method returns the removed object from the list.
Python Notes 32
Web Designing, Digital Marketing, SEO, SMO, C, C++. Call: 9649141215, www.jmdstudy.com
Python-Methods-pop()
Project Training in Python, Android, PHP, ASP.NET, Machine Learning, SQL, JAVA,
YoganandSharma
Example
by Training.
y
list1 = ['physics', 'Biology', 'chemistry', 'maths']
Training
list1.pop()
ud
MCA/ M.Tech/ B.Tech/ BCA Internship/ Industrial/
Result
JM
When we run above program, it produces the following
result −
list now : ['physics', 'Biology', 'chemistry']
list now : ['physics', 'chemistry']
Python Notes 33
Placement Python-Methods-remove()
Classes
YoganandSharma
Parameters
Training > Project >+91-9928016573
obj−This is the object to be removed from the list.
y
Training by
d
JMD Study Computer
Return Value
tu
This method does not return any value but removes
S
the given object from the list.
D
JM
Python Notes 34
Web Designing, Digital Marketing, SEO, SMO, C, C++. Call: 9649141215, www.jmdstudy.com
Python-Methods-remove()
Project Training in Python, Android, PHP, ASP.NET, Machine Learning, SQL, JAVA,
YoganandSharma
Example
by Training.
method.
Live Project
y
Training
d
list1 = ['physics', 'Biology', 'chemistry', 'maths']
u
MCA/ M.Tech/ B.Tech/ BCA Internship/ Industrial/
list1.remove('Biology')
print ("list now : ", list1)
St
D
list1.remove('maths')
print ("list now : ", list1)
Result JM
When we run above program, it produces the following
result −
list now : ['physics', 'chemistry', 'maths']
list now : ['physics', 'chemistry']
Python Notes 35
Assistance Python-Methods-reverse()
TrainingClasses
YoganandSharma
Description
+91-9928016573
d
JMD Study Computer
Live Project & 100%Job
Syntax
tu
S
Following is the syntax for reverse()method −
D
list.reverse() Parameters
NA
JM
Return Value
This method does not return any value but reverse
the given object from the list.
Python Notes 36
Web Designing, Digital Marketing, SEO, SMO, C, C++. Call: 9649141215, www.jmdstudy.com
Python-Methods-reverse()
Project Training in Python, Android, PHP, ASP.NET, Machine Learning, SQL, JAVA,
YoganandSharma
Example
by Training.
+91-9928016573
method.
y
Training
ud
MCA/ M.Tech/ B.Tech/ BCA Internship/ Industrial/
'maths'] list1.reverse()
St
list1 = ['physics', 'Biology', 'chemistry',
D
print ("list now : ", list1)
Result
JM
When we run above program, it produces the
following result −
list now : ['maths', 'chemistry', 'Biology', 'physics']
Python Notes 37
Placement Python-Methods-sort()
Classes
YoganandSharma
Description
Training > Project >+91-9928016573
The sort()method sorts objects of list, use compare
function if given.
y
Training by
d
JMD Study Computer
Syntax
tu
S
Following is the syntax for sort()method −
D
list.sort([func]) Parameters
NA
JM
Return Value
This method does not return any value; it simply
sorts the contents of the given list.
Python Notes 38
Web Designing, Digital Marketing, SEO, SMO, C, C++. Call: 9649141215, www.jmdstudy.com
Python-Methods-sort()
Project Training in Python, Android, PHP, ASP.NET, Machine Learning, SQL, JAVA,
YoganandSharma
by Training.
Example
+91-9928016573
method.
y
Training
ud
MCA/ M.Tech/ B.Tech/ BCA Internship/ Industrial/
list1.sort() St
list1 = ['physics', 'Biology', 'chemistry', 'maths']
D
print ("list now : ", list1)
Result
JM
When we run above program, it produces the
following result −
list now : ['Biology', 'chemistry', 'maths', 'physics']
Python Notes 39