Day 5
Lists are also a popular data structure in Python.
Lists are also an ordered sequence.
Here is a list L. A list is represented with square brackets.
In many respects lists are like tuples, one key difference is they are mutable.
Lists can contain strings, floats, integers.
We can nest other lists.
We also nest tuples and other data structures; the same indexing conventions apply for nesting.
Like tuples, each element of a list can be accessed via an index.
list1=[10,20,30,40,50,60]
print("list
is",list1)
run the
function CTL+shift+fn+F10
list is [10, 20, 30,
40, 50, 60]
list1=[10,20,30,40,50,60]
print("Length of
list is",len(list1))
run the
function CTL+shift+fn+F10
Length of list is 6
list1=[10,20,30,40,50,60]
list1.append(70)
print(list1)
run the
function CTL+shift+fn+F10
[10, 20, 30, 40, 50,
60, 70]
To add multiple
entries to the list
list1=[10,20,30,40,50,60]
list1.extend([80,90,100])
print(list1)
run the
function CTL+shift+fn+F10
[10, 20, 30, 40, 50,
60, 80, 90, 100]
list1=[10,20,30,40,50,60]
list2=list1.copy()
print(list2)
run the
function CTL+shift+fn+F10
[10, 20, 30, 40, 50,
60]
To remove last element
from the list
list1=[10,20,30,40,50,60]
list2=list1.copy()
print(list2.pop())
run the
function CTL+shift+fn+F10
60
list1=[10,20,30,40,50,60]
list3=list1
print("List3 is
",list3)
print(list3.pop())
print(list3)
run the
function CTL+shift+fn+F10
List3 is [10, 20, 30, 40, 50, 60]
60
[10, 20, 30, 40, 50]
Any change in one list
will automatically make change in other list when you use = instead of copy
list1=[10,20,30,40,50,60]
list2=list1.copy()
print("List2
is",list2)
print(list2[2])
run the
function CTL+shift+fn+F10
List2 is [10, 20, 30,
40, 50, 60]
30
list1=[10,20,30,40,50,60]
list2=list1.copy()
print(list2[2:7])
run the
function CTL+shift+fn+F10
[30, 40, 50, 60]
list1=[10,20,30,40,50,60]
list2=list1.copy()
print(list2[2:7:2])
run the
function CTL+shift+fn+F10
[30, 50]
list1=[10,20,30,40,50,60]
list2=list1.copy()
print(list2[::-1])
run the
function CTL+shift+fn+F10
[60, 50, 40, 30, 20,
10]
list1=[10,20,30,40,50,60]
list2=list1.copy()
print(list2[-2])
run the
function CTL+shift+fn+F10
50
To insert a particular
element at a particular index
list1=[10,20,30,40,50,60]
list2=list1.copy()
list2.insert(2,110)
print(list2)
run the
function CTL+shift+fn+F10
[10, 20, 110, 30, 40,
50, 60]
To remove particular
element
list1=[10,20,30,40,50,60]
list2=list1.copy()
list2.insert(2,110)
print(list2)
list2.remove(110)
print("list2 is
",list2)
run the
function CTL+shift+fn+F10
[10, 20, 110, 30, 40,
50, 60]
list2 is [10, 20, 30, 40, 50, 60]
Removing any element
from specific index
list1=[10,20,30,40,50,60]
list2=list1.copy()
print(list2.pop(3))
run the
function CTL+shift+fn+F10
40
To find index of any
element
list1=[10,20,30,40,50,60]
list2=list1.copy()
print(list2.index(50))
run the
function CTL+shift+fn+F10
4
To count no of times element
is repeating
list4=[1,2,3,2,1,2,1,2,3,2,1,2,3,2]
print(list4.count(2))
run the
function CTL+shift+fn+F10
7
list4=[1,2,3,2,1,2,1,2,3,2,1,2,3,2]
list4.sort()
print(list4)
run the
function CTL+shift+fn+F10
[1, 1, 1, 1, 2, 2, 2,
2, 2, 2, 2, 3, 3, 3]
list4=[1,2,3,2,1,2,1,2,3,2,1,2,3,2]
list4.reverse()
print(list4)
run the
function CTL+shift+fn+F10
[2, 3, 2, 1, 2, 3, 2,
1, 2, 1, 2, 3, 2, 1]
list5=[2,7,4,3,5]
print(max(list5))
run the
function CTL+shift+fn+F10
7
print(min(list5))
print(sum(list5))
list6=[i for i in range(10)]
print(list6)
run the
function CTL+shift+fn+F10
[0, 1, 2, 3, 4, 5, 6,
7, 8, 9]
list7=[i for i in range(2,8)]
print(list7)
run the
function CTL+shift+fn+F10
[2, 3, 4, 5, 6, 7]
Converting string to
list
str1="hello This is Python string"
list1=str1.split(" ")
print(list1)
run the
function CTL+shift+fn+F10
['hello', 'This',
'is', 'Python', 'string']
Converting list to
string
str1="hello This is Python
string"
list1=str1.split(" ")
print(list1)
str2=" ".join(list1)
print(str2)
run the
function CTL+shift+fn+F10
['hello', 'This', 'is',
'Python', 'string']
hello This is Python
string
str1=input("Enter a
string")
print("String is
",str1)
list1=str1.split(" ")
print("List1 is
",list1)
list1.reverse()
print("List1 is
",list1)
str2=" ".join(list1)
print(str2)
run the
function CTL+shift+fn+F10
Enter a stringthis is
a sting of india
String is this is a sting of india
List1 is ['this', 'is', 'a', 'sting', 'of', 'india']
List1 is ['india', 'of', 'sting', 'a', 'is', 'this']
india of sting a is
this
Tuples
tuple1=(10,20,30,40,50)
print(len(tuple1))
print(tuple1[2])
print(tuple1[1:4])
print(tuple1[1:4:2])
print(tuple1[::-1])
print(tuple1.index(40))
print(tuple1.count(30))
print(min(tuple1))
print(max(tuple1))
print(sum(tuple1))
t1=(100,)
print(t1,type(t1))
run the
function CTL+shift+fn+F10
5
30
(20, 30, 40)
(20, 40)
(50, 40, 30, 20, 10)
3
1
10
50
150
(100,) <class
'tuple'>
Learning Python Day 6