Skip to content

Python Indexing and Slicing




Python – Indexing and Slicing


As we read earlier about Python if else elif Now we will study about Indexing and Slicing in Python. In python programming language, Now we will read the concepts further, all of them will use python indexing and slicing. so lets start.





What is Indexing in python ?

Indexing means referring to an element of an iterable by using its position within the iterable.

To retrieve an element of the list, we can use the index operator [ ] :


list_1 = [ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h' ]




Python Indexing : Indexing from the start

python-indexing-from-the-start


[ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h' ] 
   ↓    ↓    ↓    ↓    ↓    ↓    ↓    ↓ 
   0    1    2    3    4    5    6    7



Example :

list_1 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
x = list_1[3]
print(x)

# or

list_1 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
print(list_1[3])



Output :

d
d




Python Negative Indexing : Negative Indexing starts from the end

Python-indexing-from-the-end

[ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h' ]
   ↓    ↓    ↓    ↓    ↓    ↓    ↓    ↓ 
  -8   -7   -6   -5   -4   -3   -2   -1



Example :

list_1 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
x = list_1[-4]
print(x)

# or

list_1 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
print(list_1[-4])



Output :

e
e





What is Slicing in Python ?


Python Indexing and Slicing

Slicing is a technique by which we can get a sub – string using indexing is known as Slicing.



Python Slicing syntax :

Variable [ Start : End : Step ]

In the above syntax, the “Start” is the index of the first element and “End” is the index of the element to stop at without including it in the slice.



Example :

list_1 = [ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h' ]
print(List_1[1:6])



Output :

[ 'b', 'c', 'd', 'e', 'f' ]




You can leave either slice boundary blank means start (or go to) the end of the list.



Example : 1

list_1 = [ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h' ] 
print(list_1[3:])



Output : 1

[ 'd', 'e', 'f', 'g', 'h' ]



Example : 2

list_1 = [ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h' ]
print(list_1[:5])



Output : 2

[ 'a', 'b', 'c', 'd', 'e' ]




To using a negative indexing, you will set the Start/Stop bounds relative to their position from the end of the list.



Example :

list_1 = [ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h' ]
print(list_1[-6:-1])



Output :

['c', 'd', 'e', 'f', 'g']





A “for” loop works exactly the same way; the first example below has no output, but the second example does :



Example : 1

for i in range(-1, -6):
    print(i)



Output : 1





Example : 2

for i in range(-6, -1):
    print(i)



Output : 2

-6 
-5 
-4 
-3 
-2




Stepping in Python

In python, the slicing can take an third argument, which is sets the interval at which elements are included in the slicing is known as stepping.



Example : 1

list_1 = [ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h' ] 
print(list_1[::3])



Output : 1

['a', 'd', 'g']



Example : 2

list_1 = [ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h' ] 
print(list_1[1::3])



Output : 2

['b', 'e', 'h']




The negative step values reverse the direction of the original list.



Example :

list_2 = [ 'h', 'e', 'l', 'l', 'o' ] 
print(list_2[: : -1])



Output :

['o', 'l', 'l', 'e', 'h']




How to check the datatype in Python?



General syntax to check the data type :

Type(x)



For Integer



Example :

x = 24 
print(type(x))



Output :

<class 'int'>



For Float



Example :

x = 24.5 
print(type(x))



Output :

<class 'float'>



For Complex



Example :

x = 24j 
print(type(x))



Output :

<class 'complex'>



For String



Example :

x = 'Hello' 
print(type(x))



Output :

<class 'str'>



For List



Example :

x = [1,2,3] 
print(type(x))



Output :

<class 'list'>



For Tuple



Example :

x = (1,2,3) 
print(type(x))



Output :

<class 'tuple'>



For Dictionary



Example :

x = {'a':1, 'b':2, 'c':3} 
print(type(x))



Output :

<class 'dict'>



For Set



Example :

x = {1,2,3} 
print(type(x))



Output :

<class 'set'>




How to check the length in python ?

Length function is used to calculate total element of any collection or sequence or iterable data types.



General syntax :

len(iterable)

Length function always return Integer datatype.




Example :

x = 'Hello' 
print(len(x))



Output :

5

String is a immutable data type.

Immutable : We can’t modify content / values of immutable data type because original string is immutable. we can’t modify the original string.


>