Skip to main content
Open navigation menu
Close suggestions
Search
Search
en
Change Language, English
Upload
Sign in
Sign in
0 ratings
0% found this document useful (0 votes)
52 views
11 pages
Python List
Uploaded by
Exotic Paper Planes
AI-enhanced title
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as PDF or read online on Scribd
Download
Save
Save python list (1) For Later
Share
0%
0% found this document useful, Mark this document as useful
0%
0% found this document not useful, Mark this document as not useful
Print
Embed
Report
0 ratings
0% found this document useful (0 votes)
52 views
11 pages
Python List
Uploaded by
Exotic Paper Planes
AI-enhanced title
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as PDF or read online on Scribd
Go to previous items
Download
Save
Save python list (1) For Later
Share
0%
0% found this document useful, Mark this document as useful
0%
0% found this document not useful, Mark this document as not useful
Print
Embed
Report
Go to next items
Outline
Page view
Download
Introduction to Lists
{A Introducing Lists ‘A What ore Python Lists @ 4.1 INTRODUCTION Till now you have enjoyed writing simp i is i a 'ple Programs in Python. This session will enhance your Python knowledge little more by talking about a useful datatype of Python called Lists. Lists are used to represent, use and manipulate a sequence of values in multiple ways and this session will talk about the same. 4.2 INTRODUCING LISTS Before we talk about the Python lists, let us first perform an activity. AA, ~~ Activity [A | PREPARE A SHOPPING LIST os oy Prepare a shopping list after consulting your mother. Say your shopping list looks somewhat like : 7 tell hoy pany items are theme in the shopping list? That what he Now, can you we length of the shopping list ? —8 itews___—_——_ =236 ARTIFICIAL INTELLIGENCE-K / 3. What are the first and last serial numbers of the shopping list items ? __1 (first) and 8 (last) 4 ‘What is the third item of the shopping __rice with serial number 3. 5. What is the serial number of the 6th item in the list ? list and what is its serial number ? _ ee 6. What are the first and last items of the shopping list ? __45¢ item milk__{with serial number 2) Last item onion_(with serial number 8) PB, saivin 1 1B SHOPPING LIST WITH A TWIST 2B Prepare shopping list like earlier but this time, start the item numbering from 0 onwards, i-e., as 0, 1, 2, ... instead of 1 onwards. 1. Shopping list this time would look like : Shopping List 0. milky 1. butter 2. rice 3. Cagy 4. juice S. bread 6. fruity 7. oniow 2. What is the length of the shopping list this time ? —8 tem, 3. What are the first and last serial numbers of the shopping list items ? —0 (first) amd 7 (Los 4, What i ird i is the third item of the shopping list and what is its serial number ? rice with serial n aSession 4 5. What is the serial number of the 6th item in the list 2 s. INTRODUCTION To Usts 237 6. What are the first and last items of the shopping list ? tt teen mile ( with serial number 0) Last ite onion. _( withe serial number 7)_ Can you see any difference between the two lists of above two activities ? Well'you are right. There is not much of a difference except that the second list begi numbering ofits items 0 onwards. Thus, for every item its position and serial number bose different. That is, For ist item , serial number is 2, For 3rd item, serial number is 2 ‘And so on. 4.3 WHAT ARE PYTHON LISTS ? } ee Python lists are sequence of values somewhat like list of 2 ean muti Ee Bae on list is simply activity 18 above. In other words, a Python ist is simply elicah cf ealitetined a sequence of values stored in a specific order with each a specific order with each value identified by its index number in that order. The value identified by its index index numbers of Python lists start 0 onwards and go pipes ia aeroey like 0, 1, 2, ... 4.3.1 How to Create Python Lists ? To create Python lists, you can put a list of values with comma in between the items, enclosed in square brackets, e.g., >>> mylist = (2, 4, 6, 8, 18] It will create a list with five numbers Lists are created by enclosing and name it as mylist. comma-separated values inside You can display the list items by simply _| [B Python 3.79Shetl_ square typing its name on the prompt in the | Ble Est Shell Debug ptions Window Help interactive mode, e.g., (see figure) >>> mylist = [2, 4, 6, 8, 10] >>> mylist >>> mylist —*> [2, 4, 6, 8, 18) [2, 4, 6, 8, 10] >>> print (mylist) [2, 4, 6, 8, 10) You can also print the list by using the >>> list name with print() (see figure) — SSSession 4 INTRODUCTION TO Usts 237 5. What is the serial number of the 6th item in the list ? S. 6 What are the first and last items of the shopping list? tt ten mile ( with serial number O)_ Last item onion. _( with serial number 7)__ Can you see any difference between the two lists of above two activities ? Well, you are right. There is not much of a numbering of its items 0 onwards. Thus, for different. That is, For Ist item , serial number is @, difference except that the second list begins ‘every item its position and serial number become eneenaninttenaainaeinansiaaeenctmaamne For 3rd item, serial number is 2 ‘And so on. i 4.3. WHAT ARE PYTHON LISTS ? -_ Python lists are sequence of values somewhat like list of ean ssa ee aaah on list is simply a activity 1B above. In other words, a Python list is simply, gF¥thom lst simply a sequence of values stored in a specific order with each a specific order with och value identified by its index number in that order. The _value identified by its index index numbers of Python lists start 0 onwards and go Humber in that ordes. like 0, 1, 2, ... 4.3, How to Create Python Lists @ To create Python lists, you can put a list of values with comma in between the items, enclosed in square brackets, €.9., >>> mylist = [2, 4, 6, 8, 10] 4 ect fai 3 It will create a list with five number in aa ae) AOA and name it as mylist. comma-separated values inside You can display the list items by simply | [4 Python 37.9She square brackets, typing its name on the prompt in the | Ble _Edit_Shell Debug ptions Window Help _ interactive mode, e.g., (see figure) >>> mylist = (2, 4, 6, 8, 10) >>> mylist >>> mylist [2, 4, 6, 8, 10) easel >>> print (mylist) (2, 4, 6, 8, 10) © You can also print the list by using the | >>> list name with print() (see figure) ...238 ARTIFICIAL INTELUGENCE-K ir i int-numbers, Python lists can store any type of values in them - integers, floating Po” strings, and even a combination of values, such as + >>> mylista = (2, "Rudy", 93-5+ va") >>> mylist] [2, 'Rudy', 93.5, 'A') >> If you use the type( ) with type as list : list name, Python will show its Ld Python 3.7.9 Shell >> >>> type (mylist) >> “The type() function when used with a list name 4.3.2. Checking the Length (Number of Items) of the List The length or the size of a list means the number of items in the list. To check the length (number of items in the list), you can use the len( ) function with list name as : [d Python 3.7.9 Shell Ble Est Shel Debug Options Window Hep wan >>> mylist 12, 4, 6, 8, 10) >>> len (mylist) 5 >>> mylistl [2, ‘Rudy’, 93-5, A‘) >>> len(mylisti) 4 ‘The len() function when used wit alist name gives its length, “e., no. of tems in the ist Refer figure 1 Note The len(
) function returns the size or length of the list, i.e., the number of elements in the list. 4.3.3. Accessing Individual Items of a List Python list’s items are intemally numbered as 0, 1, 2, 3...(Positive Indexes). That is, ‘the 4 item has the index number as @ | ‘the 2"! item has the index number as 1 ‘the 3" item has the index number as 2 The last item has index number as length-1 op size-1 1-1 or size~Session 4 : INTRODUCTION TO Lists 239 python sequences such as lists have positive indexes as well as the negative indexes. ‘That is, ‘the last item has the index number as -1 the 2" last item has the index number as -2 the 3" last item has the index number as -3 The first item has index number as length or -size) Graphically, it can be depicted as : th = 5 ———_} ipl ed | eta] TN | tO" | Positive index for the first item in the list = 0 | Positive index for last item of a list = size -1 | Negative index for the first item in the list = -size Negative index for last item of a list = To access an element, just give its index (positive or negative, any) in the square brackets along with the list name, ie., as :
[] For example, consider our lists mylist and mylist1 as created above : enn [& Python 3.7.9 Shell | Bie Est She Debug Options window Hep _ >>> mylist [2, 4, 6, 8, 10) >>> mylisti [2, "Rudy', 93-5, ‘a'l >>> Now to access the first items of mylist and mylist!, you can write mylist[OJand mylisti [0] because the index number of the first item is 0 (see below). 35> mylist {0}, mylisti(0] (2, 2) >>>260 ARTIFICIAL INTELUGENCEX 4 the negative index for ‘You may also use the negative index forthe frst eit For my teem is 6 as the the fist item is -5 while for mytistt, the negative ve fo 5 and 4. lengths of mytist and mylist1 respectively are >>> mylist[-5] (2, 2) >> 4.3.4 Accessing all Items of a List fe To access all items of a list one by one, you can use a for statement of Python, which picks To access Mery om a sequence anit ou se tem a ou want JER used as: for
in < ‘listname>: using
as individual For example, consider the list mylist containing item 25 [2, 4, 6 8 you need to write : statement with it to print individual items, for oneitem in mylist = print (oneiter) It will print the items of the mentioned list (mylist) one by one as = iten 10]. If you use for Before we proceed to more examples, you need to know about the following important things about the for statement (also called for loop of sequences) : © The
used with for loop must be a legal identifier. «© There must be a colon at the end the for statement’ first line (the header line of for loop)- © The lines below the header line of for loop, which are using the
must be indented inwards. es Jv It must be a legal identifier and it will access Following figure explains it : gas individual elements of the list one by one The code lines using the open lemons of i for oneiten in aylist 7 Should be a valid ist ana must Pe, Tincand ~~, Print (oneitem) be crested betine here all be at th should all be at the print("__") It must be at the end of header line of for loop same indent levelSession 4: INTRODUCTION TO usts 241 Let us use the for loop for another list mylist1 and understand how it works : item in mylistl = =, item) © print ("#..- + ' # t 4.3.5 Modifying an Item in the List To modify an item in a list, just assign the new value to its accessing name, i.e., [ ] = For example, to change the 3rd item of mylisti to 95.5, you may write : >>> mylistl [2, tRudy', 93-5) 1311 355 mylisei [2] = 95-5 d>> mylist2 (2, ‘Rudy', 98.5, 'A'T mylistt [2] = 95-5 Note Changing the value of an it variable, i.e., assign a new val fem in a list is the same as changing the value of @ ue to item's accessing name. 4.3.6 Adding an tem in the List | To add an item to a list, use append(
>> mylist: 4, 6, 10) mylist append (12) mylist-append(12) CZheck Point Sn 1. The function to check the type of a list is = (@ append( ) © type). % (© length( ) (@) len) 2. The function to check the size of a list is : @ append( ) © type() (©) length( ) @ let | 3. The index of the first item in a list is__ @o @1 (© size @ -size a iiiictct.2H2_ ARTIFICIAL INTELUGENCE-Wx 4, The index of the last item itv a list is (@0 (b) 1 oe il) entre oa 7 (0) size (a) onl 5, The statement that can be used to access all items ‘of a list one by one I* (a) all () items (0 for Gee 6. The function to add new item to @ list is @ append) () type() (© length) (Wd) ten) < Let Us REVISE «© APython list is simply a sequence of values stored in a specific order with each value identified by its index number in that order. 4 The len(listname>) function returns the size or length of the list , list. Positive index for the first item in the list is 0. Positive index for last item of a list is stze-1. Negative index for the first item in the list is ~slze ‘Negative index for last item of a list is ~1. 4. Using a for loop all items from a list can be ‘accessed one by one, Using append(), a new item is added to a list Splution Time ion Ting 1. Take this list of items animals « ['cat", ‘dog’, ‘mouse’, ‘hamster’ the number of elements in the ‘wa7>w print out each item in the lst using a for loop Ans. ‘animals = [‘cat", ‘dog’, ‘mouse’, ‘hamster’ ] for one in animals : print (one) pop annals © ['cat's "dop', ‘@ouse's ‘hamater’) Sop for one ip animals t print (one) cat dog mouse hamster 2. Print the firstanimal in the animals list Ans. print (animals [0]) ———Session 4 : INTRODUCTION TO Lists 243. +3. Print the first animal in the animals list (without using the index 0). Ans size = len(animals) print(animals[ -size]) 4, Print the last animal in the animals list. Ans. print (enimals[-1]) Print the last animal in the animals list (without using the index —1). Ans. size = len(animals) print (animals[size-1]) 6. Add animal “python” to the list of animals Ans. animals .append(“python”) 7. Change the last element of the animals list (after adding python) to “rabbit”. Ans. Size = len(animals) aninals[size-1] = "rabbit" 8. There is an error in the answer of the previous question. Find the error and correct it. ‘Ans. The variable size used in the 2nd line of code does not exist, hence it will give error. Variable Size exists (S capital) not size, thus by changing size to size, we get the desired result : Size = len(animals) animals[Size-1] = "rabbit" 9. Correct the error in the given code. for name in [‘Isha', 'Misha', ‘Trisha']: print(name, len(name)) Ans. There is wrong indentation in the given code. The second line of the code is part of for loop used for a list, hence it must be idented below it. The corrected code will be : for name in ["Isha', ‘Misha’, Trisha’): print(name, len(name) ) 10. What will be the output produced by the corrected version of previous question, given that len( ) gives the number of characters in a string ? Ans. The output will be : Isha 4 Misha 5 Trisha 6 < GLOSSARY UUst_ A named sequence of items with items stored in a specific order with each value identified by i ‘umber in that order. its indexa. | 4a>v ow 244 ARTIFICIAL INTELLIGENCE-IX Assignment — SeEenaneuene i. 12. 1B 14, Wat are lies tn Python he result of the following statement ? Consider list things = “clowns”, “snakes”], what will be # print(things[1]) Why did the above code print “snakes” and no! What changes would you make in above code ( Write code to modify the 1st item of the list as “clowning”. What will be the output of the following code ? “clowns” ? (Q. 2) 40 that “snakes” gets printed 7 things = ["clowns", “snakes"] print(things) ‘things[1] = "bats" print (things) Write code to add “spiders” to the list things used above, What will be the output of the following code ? things = ["clowns", "snakes", "spiders"] print(things) things[1] = "bats" things. append("beetles") print (things) PRACTICAL ASSIGNMENT Create a list namely test having at least 3 integers, 2 floating point numbers and two strings. Print the first element from the list test. Print the last element from the list test. . Print the 2nd element from the list test. Print the 2nd last element from the list test, Print the 3rd element and the 3rd last element from the list test. ’. Print all elements from the list test. . Print the size of the list test now. . Add element “Ditto” to the list test. 10, Print the size of the list test now. Print the last element from the list test, Print the 2nd last element from the list test. Modify the 2nd last element of the list test as “new”, Print the size of the list test now, '
You might also like
Understanding Python Lists Basics
PDF
No ratings yet
Understanding Python Lists Basics
24 pages
Python Lists: Creation and Manipulation
PDF
No ratings yet
Python Lists: Creation and Manipulation
88 pages
Python List Creation and Manipulation Guide
PDF
No ratings yet
Python List Creation and Manipulation Guide
18 pages
Python Lists: Creation and Methods
PDF
No ratings yet
Python Lists: Creation and Methods
30 pages
List and Tuples in Python
PDF
No ratings yet
List and Tuples in Python
11 pages
Python Identifiers and Literals Guide
PDF
No ratings yet
Python Identifiers and Literals Guide
74 pages
Python Lists and Tuples Explained
PDF
No ratings yet
Python Lists and Tuples Explained
21 pages
Understanding Python Lists and Operations
PDF
No ratings yet
Understanding Python Lists and Operations
38 pages
Understanding Python Lists and Their Properties
PDF
No ratings yet
Understanding Python Lists and Their Properties
19 pages
Understanding Python Lists and Operations
PDF
No ratings yet
Understanding Python Lists and Operations
63 pages
Python Nit IV
PDF
No ratings yet
Python Nit IV
20 pages
Python Lists and Tuples Explained
PDF
No ratings yet
Python Lists and Tuples Explained
78 pages
Python Lists: Creation and Usage Guide
PDF
No ratings yet
Python Lists: Creation and Usage Guide
33 pages
Python Lists and Tuples Explained
PDF
No ratings yet
Python Lists and Tuples Explained
12 pages
I BSC Unit IV Python Notes
PDF
No ratings yet
I BSC Unit IV Python Notes
17 pages
Python Built-in Data Types Overview
PDF
No ratings yet
Python Built-in Data Types Overview
86 pages
Lists Tuples Dictionaries LbTfP89o
PDF
No ratings yet
Lists Tuples Dictionaries LbTfP89o
75 pages
Python Lists: Structure & Methods
PDF
No ratings yet
Python Lists: Structure & Methods
67 pages
Python Lists and Tuples Explained
PDF
No ratings yet
Python Lists and Tuples Explained
12 pages
Gaddis Python 4e Chapter 07 PPT - combinedForLesson4
PDF
No ratings yet
Gaddis Python 4e Chapter 07 PPT - combinedForLesson4
123 pages
Unit 3
PDF
No ratings yet
Unit 3
34 pages
Unit III
PDF
No ratings yet
Unit III
50 pages
Python Lists: Creation, Access, and Methods
PDF
No ratings yet
Python Lists: Creation, Access, and Methods
17 pages
Python Lists and Tuples Overview
PDF
No ratings yet
Python Lists and Tuples Overview
105 pages
Understanding Python Lists and Methods
PDF
No ratings yet
Understanding Python Lists and Methods
73 pages
Unit 4-1
PDF
No ratings yet
Unit 4-1
26 pages
Python Lists and Tuples Explained
PDF
No ratings yet
Python Lists and Tuples Explained
32 pages
Python Lists Overview for CSE 1001
PDF
100% (1)
Python Lists Overview for CSE 1001
16 pages
Python Unit 4
PDF
No ratings yet
Python Unit 4
24 pages
Python List Operations Guide
PDF
No ratings yet
Python List Operations Guide
110 pages
Python List Data Types Explained
PDF
No ratings yet
Python List Data Types Explained
18 pages
Python Lists: Creation and Access Guide
PDF
No ratings yet
Python Lists: Creation and Access Guide
18 pages
Understanding Python Lists and Their Uses
PDF
No ratings yet
Understanding Python Lists and Their Uses
20 pages
BPP Unit 4
PDF
No ratings yet
BPP Unit 4
74 pages
Python Lists and Tuples Overview
PDF
No ratings yet
Python Lists and Tuples Overview
26 pages
Python List Basics and Operations
PDF
No ratings yet
Python List Basics and Operations
10 pages
Python Program: Count Repeated Characters
PDF
No ratings yet
Python Program: Count Repeated Characters
25 pages
Python List Basics and Operations
PDF
No ratings yet
Python List Basics and Operations
23 pages
Python Lists: Indexing, Slicing, Updating
PDF
No ratings yet
Python Lists: Indexing, Slicing, Updating
8 pages
Python List Data Structure Overview
PDF
No ratings yet
Python List Data Structure Overview
74 pages
Python Lists: Access, Update, Remove
PDF
No ratings yet
Python Lists: Access, Update, Remove
28 pages
Understanding Python Lists and Errors
PDF
No ratings yet
Understanding Python Lists and Errors
13 pages
Lists and Dictionaries
PDF
No ratings yet
Lists and Dictionaries
21 pages
Python List Data Type Explained
PDF
No ratings yet
Python List Data Type Explained
36 pages
Python List Data Structure Guide
PDF
No ratings yet
Python List Data Structure Guide
21 pages
CSC AI Lab Manual 2
PDF
No ratings yet
CSC AI Lab Manual 2
24 pages
Sahil (Exp - 6)
PDF
No ratings yet
Sahil (Exp - 6)
9 pages
Python 3 List Operations Cheat Sheet
PDF
No ratings yet
Python 3 List Operations Cheat Sheet
4 pages
Python Lists, Tuples, and Dictionaries Guide
PDF
No ratings yet
Python Lists, Tuples, and Dictionaries Guide
17 pages
BVM Global Schoollist in Python
PDF
No ratings yet
BVM Global Schoollist in Python
5 pages
Python List Basics and Operations
PDF
No ratings yet
Python List Basics and Operations
15 pages
CSC232 Informaton Security Lab-2
PDF
No ratings yet
CSC232 Informaton Security Lab-2
27 pages
Understanding Python Lists Basics
PDF
No ratings yet
Understanding Python Lists Basics
16 pages
Python Loops and List Operations Guide
PDF
No ratings yet
Python Loops and List Operations Guide
24 pages
Python List Operations and Methods
PDF
No ratings yet
Python List Operations and Methods
141 pages
Python Data Types Overview
PDF
No ratings yet
Python Data Types Overview
23 pages
Bushra (Exp 6)
PDF
No ratings yet
Bushra (Exp 6)
9 pages
Python List Operations Guide
PDF
No ratings yet
Python List Operations Guide
9 pages
Introduction To Programming 2 - Python - Week 3
PDF
No ratings yet
Introduction To Programming 2 - Python - Week 3
59 pages
Computer Practical Filecomplete
PDF
No ratings yet
Computer Practical Filecomplete
30 pages
Extra Questions on Beautiful White Horse
PDF
100% (3)
Extra Questions on Beautiful White Horse
4 pages
Invitation Formats for Events
PDF
No ratings yet
Invitation Formats for Events
13 pages
Understanding "The Voice of the Rain"
PDF
No ratings yet
Understanding "The Voice of the Rain"
3 pages
Laburnum Top: Poem Analysis and Themes
PDF
No ratings yet
Laburnum Top: Poem Analysis and Themes
40 pages
Exploring "The Adventure" Text Analysis
PDF
No ratings yet
Exploring "The Adventure" Text Analysis
6 pages
Gaitonde's Journey Through Alternate History
PDF
No ratings yet
Gaitonde's Journey Through Alternate History
23 pages
Ict Notes
PDF
No ratings yet
Ict Notes
7 pages
Understanding Artificial Neural Networks
PDF
No ratings yet
Understanding Artificial Neural Networks
10 pages
Python 1
PDF
No ratings yet
Python 1
2 pages