You are on page 1of 10

COMPUTER SCIENCE

with
PYTHON

Computer Science Department(Krishna Public School)


CHAPTER- 1
Sujeet Tiwari
Mr. Liju K John Review of Python Basics: List
Mrs Rubeena Mirza
Mrs Disha Dhupar
(PART-VI)
Topics
1. Flow of Execution
2. String
3. List
4. Tuples
5. Dictionary
6. Sorting Techniques

Computer Science with PYTHON class XII Chaper-1: List 2


3. List
Definition: List are the sequence of values. List can be used to store any type
and number of variable and information. The value of list are called elements
or items or list member.
Syntax:
List_Variable=[Item1,item2….]
Items can be as following,
1. [1,2,3,4,….]
2. [a,b,c,d,….]
3. [“I1”,”I2”,”I3,…]
4. [[item1,itm2,itm3],[itm4,itm5],[….],[….]]
5. Combination of above or other like tuple, dict. Etc.

Computer Science with PYTHON class XII Chaper-1: List 3


3. List: Indexing
• In list, to specify each element uniquely an index value is used
Ex: LIST=[10,20,30,40,50]
0 1 2 3 4 Positive Index
10 20 30 40 50 LIST
-5 -4 -3 -2 -1 Negative Index
To retrieve data directly from list, index used as following
>>>print(LIST[2])
Output=>30
>>>print(LIST[-3])
Output=>30

Computer Science with PYTHON class XII Chaper-1: List 4


3. List: Indexing into nested list
• In list, to specify each element uniquely an index value is used
Ex: LIST=[[10,20,30],[40,50,60]] List-2
List-1
0 1 Positive Index
0 1 2 0 1 2 LIST
10 20 30 40 50 60
-3 -2 -1 -3 -2 -1
-1 -2 Negative Index
To retrieve data directly from list, index used as following
>>>print(LIST[1])
Output=>[40,50,60]
>>>print(LIST[1][2])
Output=>60

Computer Science with PYTHON class XII Chaper-1: List 5


3. List: Comprehension
List comprehension is a way of creating a new list from an existing list in
python.
Syntax: new_list=[ expression for item in list if condition ]
On basis of which items should inserted into list

Existing list, from which item taken

By Which new value inserted

Computer Science with PYTHON class XII Chaper-1: List 6


3. List: Slicing
syntax: list[start: stop: step]
Error, while passing empty index

Only start index, it only gives specific index value


Only stop index, which take value from zero index to
stop index-1 , output=>[10,20]
Start and stop index, which take value from start index
up to stop index-1, output=>[30,40,50,60,70]
Start index, stop index and step, here it start from start
index, takes value up to stop index -1 with difference of
index from start index +/- step value, here index 3 and
5skeepd, so we get output=>[30,50,70] of index 2,4,6
Copy one list to another list

Try the Trick: list2=list1[ : :-1]


Output=> ?
Computer Science with PYTHON class XII Chaper-1: List 7
3. List: Built-in Functions and Methods
Sr Function Description
.
1 len(list) Return the total length of
the list
2 max(list) Returns the maximum
item value of list
3 min(list) Returns the minimum
item value of list
4 list(seq) Converts tuple into list
5 sum(list) Sum up all the numeric
values present in the list

Computer Science with PYTHON class XII Chaper-1: List 8


3. List: Built-in Functions and Methods
Sr. Method Description Program>>>l1=[40,3,20,0,14]
1 append(item) Add items to the end of the list >>>l1.append(25)
>>>print(l1)
2 index(item) Returns index of specific item, when no >>>l1.index(20)
item found a ValueError exception occur
3 insert(index, Add item to the list on specific index, >>>print(l1)
item) the value of existing index will shift to >>>l1.insert(2,90)
next index >>>print(l1)
4 sort() Sort items of list into ascending order, >>>l1.sort()
>>>print(l1)
>>>l1.sort(reverse=True)
>>>print(l1)
5 remove(item) Remove specified item from list, if item >>>print(l1)
not exist into list a ValueError exception >>>l1.remove(0)
occur >>>print(l1)
6 reverse() Reverse the order of items in the list >>>l1.reverse()
>>>print(l1)
Computer Science with PYTHON class XII Chaper-1: List 9
Thankyou
Computer Science Department
(Krishna Public School, Koni, Bilaspur)
Sujeet Tiwari
Mr. Liju K John
Mrs Rubeena Mirza
Mrs Disha Dhupar

Computer Science with PYTHON class XII Chaper-1: List 10

You might also like