Professional Documents
Culture Documents
Python@1709
Python@1709
-----------
--> If we want to represent a group of elements as single entity there we will use
List.
--> To represent elements in list we have to use [].
EX:
---
list = ["AAA", "BBB", "CCC", "DDD", "EEE", "FFF"]
print(list)
EX
---
['AAA', 'BBB', 'CCC', 'DDD', 'EEE', 'FFF']
--> List is index based, we are able to read elements from List on the basis of
index values, We are able to read elements in bot forward direction and backward
direction, to provide elements in forward direction we have to provide +ve index
values, to provide elements in backward direction we will use -ve index values.
EX:
---
list = ["AAA", "BBB", "CCC", "DDD", "EEE", "FFF"]
print(list)
print(list[0])
print(list[1])
print(list[2])
print(list[3])
print(list[4])
print(list[-1])
print(list[-2])
print(list[-3])
OP
---
['AAA', 'BBB', 'CCC', 'DDD', 'EEE', 'FFF']
AAA
BBB
CCC
DDD
EEE
FFF
EEE
DDD
OP:
---
['AAA', 'FFF', 'BBB', 'EEE', 'CCC', 'DDD']
--> It able to allow duplicate elements, we can recognize the duplicate elements on
the basis of index values.
EX:
----
list = ["AAA", "BBB", "CCC", "BBB", "AAA"]
print(list)
OP:
---
['AAA', 'BBB', 'CCC', 'BBB', 'AAA']
--> To retrive elements from List we are able to use both for loop and while loop.
EX:
---
list = ["AAA", "BBB", "CCC", "DDD", "EEE", "FFF"]
print(list)
print()
for x in range(0,len(list)):
print(list[x])
print()
for x in list:
print(x)
print()
x = 0
while x < len(list):
print(list[x])
x = x + 1
OP:
---
['AAA', 'BBB', 'CCC', 'DDD', 'EEE', 'FFF']
AAA
BBB
CCC
DDD
EEE
FFF
AAA
BBB
CCC
DDD
EEE
FFF
AAA
BBB
CCC
DDD
EEE
FFF
--> We are able to read elememts from List type by using slice operator.
Syntax:
list[start_Index: end_Index: Step_Length]
start_Index: it will take start index value in both +ve and -ve values.
end_Index : It will take end index value in both +ve and -ve values.
step_Length: It will take step length to move pointer to the elements in order to
retrieve.
If step_Length value is +ve then it will retrieve elements in forward
direction, if step_Length value is -ve value then it will retrive
elements in backward direction.
EX:
---
list = ["AAA", "BBB", "CCC", "DDD", "EEE", "FFF"]
print(list)
print(list[0:5:1])
print(list[:5:1])
print(list[::1])
print(list[::])
OP:
---
['AAA', 'BBB', 'CCC', 'DDD', 'EEE', 'FFF']
['AAA', 'BBB', 'CCC', 'DDD', 'EEE']
['AAA', 'BBB', 'CCC', 'DDD', 'EEE']
['AAA', 'BBB', 'CCC', 'DDD', 'EEE', 'FFF']
['AAA', 'BBB', 'CCC', 'DDD', 'EEE', 'FFF']
EX:
---
list = ["AAA", "BBB", "CCC", "DDD", "EEE", "FFF"]
print(list)
print(list[::-1])
print(list[:-5:-1])
print(list[-2:-5:-1])
OP:
---
['AAA', 'BBB', 'CCC', 'DDD', 'EEE', 'FFF']
['FFF', 'EEE', 'DDD', 'CCC', 'BBB', 'AAA']
['FFF', 'EEE', 'DDD', 'CCC']
['EEE', 'DDD', 'CCC']
EX:
----
list = ["AAA", "BBB", "CCC", "DDD", "EEE", "FFF"]
print(list)
print(list[2:-2:1])
print(list[-2:1:-1])
print(list[0:-3:-1])
print(list[len(list)-1:-1:1])
OP:
---
['AAA', 'BBB', 'CCC', 'DDD', 'EEE', 'FFF']
['CCC', 'DDD']
['EEE', 'DDD', 'CCC']
[]
[]
len()
--> To get no of elements which are existed in list.
EX:
---
list = ["AAA", "BBB", "CCC", "DDD", "EEE", "FFF"]
print(list)
print(len(list))
OP:
---
['AAA', 'BBB', 'CCC', 'DDD', 'EEE', 'FFF']
6
count()
--> It able to return an integer value, it will represent the no of times a
particular element is repeated.
append()
--> It can be used to add the specified element to the list.
EX:
---
list = []
print(list)
list.append("AAA")
list.append("BBB")
list.append("CCC")
list.append("DDD")
print(list)
OP:
---
[]
['AAA', 'BBB', 'CCC', 'DDD']
EX:
---
list = eval(input("Enter List Elements : "))
print(list)
print(type(list))
OP:
---
Enter List Elements : ["AAA","BBB","CCC","DDD","EEE","FFF"]
['AAA', 'BBB', 'CCC', 'DDD', 'EEE', 'FFF']
<class 'list'>
EX:
---
list = []
for x in range(0,6):
list.append(input("Enter %d element : "%x))
print(list)
OP:
---
['AAA', 'BBB', 'CCC']
['AAA', 'BBB', 'CCC', 'DDD']
['AAA', 'BBB', 'CCC', 'DDD', 'EEE']
['AAA', 'BBB', 'CCC', 'DDD', 'EEE', 'FFF']