Você está na página 1de 7

>>>x=[1,2,3,4,5,6]

>>>x
[1,2,3,4,5,6]
First index:-
x[0]=1
End index:-
X[-1]=6
From a to b
X[a:b]
Note that the end number not taken :-
>>>X[0:5]
[1,2,3,4,5]
From start to end
>>>x[:]
[1,2,3,4,5,6]
>>>x[-2]
5
>>>x[:2]
[1,2]
The length of list :-
>>>len(x)
6
To add number :-
>>>x.append(7)
[1,2,3,4,5,6,7]
To choose the position of the element:-
x.insert(position,element)
>>>x.insert(1,2)
[1,2,2,3,4,5,6,7]
To delete element from the list:-
x.pop(position)
>>>x.pop(1)
[1,2,3,4,5,6,7]
To make list from a to b :-
Using range
>>>range(10)
[0,1,2,3,4,5,6,7,8,9]
Start from zero to the (end number-1)
>>>x[5:]
[5,6,7,8,9]
>>>x[:5]
[0,1,2,3,4]
Using range but youll define the start
and end element
Range(start,end)
>>>range(0,5)
[0,1,2,3,4]
Note that end element Not taken.
Using range but youll define the start
,end and steps of elements:-
-Range(start,end,step)
Note:-if you dont write the step become
(1)
>>>range(0,10,2)
[0, 2, 4, 6, 8]
To change the first and end number :-
>>>y=[1,2,3,4,5]
>>>y[::-1]
[5,4,3,2,1]
To start from smallest to the largest
number:-
>>>y=[100,99,98,97,96,95,94,93]
[100,99,98,97,96,95,94,93]
>>>y.sort()
[93, 94, 95, 96, 97, 98, 99, 100]
To return
>>>y[::-1]
[100,99,98,97,96,95,94,93]

>>> S=Hello World


To separate between the words :-
>>>s.spilt( )
['Hello', 'World']
To know the position of index number :-
>>>s.find(W)
6
>>>s[ : :-1]
'dlroW olleH'

Você também pode gostar