CLASS -11 COMPUTER SCIENCE
CHAPTER- 7 LIST MANIPULATION
🔔To download textbook pdf click on the ''DOWNLOAD'' button below -
DOWNLOAD TEXTBOOK
NOTES FOR COMPLETE UNDERSTANDING -
- A list is like a container that stores collection of any kind of values.
- A List is a mutable data type i.e. any value from the list can be updated later if wanted. For updated values , Python does not create a new list.
- List is a series like a string and a tuple except of the fact that list is mutable whereas string and tuple are immutable.
- List Creation :
- List is a standard data type of Python. It is a sequence/series which can store values of any variety.
- List is represented by square brackets “ [ ] “ For eg. - • [ ] <---- Empty list • [1, 2, 3] <---- integers list • [1, 2.5, 5.6, 9] <----- numbers list (integer and float) • [ ‘a’, ‘b’, ‘c’] <------ characters list • [‘a’, 1, ‘b’, 3.5, ‘zero’] <----- mixed values list • [‘one’, ’two’, ’three’] <------- string list
- In Python, only list and dictionary are mutable data types and other data types are immutable data types.
- Creation of List :
- List can be created in following ways- • Empty list - L = [ ]
- List can also be created with the following - • list1 = list( )
- If we want to pass values to a list in numeric form then we have to write following function : -L=eval(input(“Enter list to be added “)) *eval ( ) function identifies type of the passed string and then return it.
- Accessing a List :
- List is a sequence like a string.
- List also have index of each of its element. List also has 2 type of index :
- forward indexing (from 0, 1, 2, 3, 4, 5, 6, .......to L-1 )
- backward indexing (from -L to - 1).
- In a list, values can be accessed like string.
- len( ) function is used to get the length of a list.
- L[ i ] will return the values exists at i index.
- L [ i : j ] will return a new list with the values from i index to j index excluding j index. *NOTE : membership operator (in, not in) works in list alike they work in strings. *NOTE : + operator adds a list at the end of other list whereas * operator repeats a list.
- Difference between a List and a String :
- A string is immutable whereas list is mutable.
- Individual values in string can’t be change whereas it is possible with list
- Traversal of a list :
- It means to access each and every element of that list.
- It is done with for loop.
- Comparison of Lists :
- Relational operators are used to compare two different lists.
- Python compares lists or tuples in lexicographical order,
means comparing sequences should be of same type and
their elements should also be of similar type
- List Operations (+, *) :
- Main operations that can be performed on lists are joining list,
replicating list and list slicing.
- To join Lists,+ operator , is used which joins a list at the end of
other list. With + operator, both the operands should be of list
type otherwise error will be generated.
- To replicate a list, * operator , is used.
- List Slicing :
- To slice a List, syntax is seq = list [ start : stop ]
- Another syntax for List slicing is –
seq=list[start:stop:step]
- List Manipulation
- Element Appending in List list.append(item)
- Updating List elements list[index]=new value
Deletion of List elements del list[index] *NOTE :if we write del list complete list will be deleted. *NOTE : del command can be used to delete an element of the list or a complete slice or a complete list.
- Only one element will be deleted on pop() from list.
- pop ( ) function can not delete a slice.
- pop ( ) function also returns the value being deleted.
- 🔔MORE MATERIALS TO BE UPLOADED STAY TUNED🔔