How to do indexing and slicing in python?

Description

To implement the concept of indexing
and slicing in python3.

Indexing
  • Every data type in python3 like list,tuple
    variables  stores numbers, strings
    and characters.
  • Every element that starts with index 0.

Positive index:

  • Its starts with left to right with positive
    index number.

Negative index:

  • Its starts with right to left with negative
    index  number.

Eg.

Picture1

Slicing

  • we can call multiple element in a list
    or tuple or any  other data type
    in pythonby creating a range
    of index  numbers separated by
    a colon [x:y]

#simple list and tuple data type
list1=[‘bala’,’naren’,’vivek’,’sathish’,10,45,85,20]
tuple1=(‘bala’,’naren’,’vivek’,’sathish’,10,45,85,20)
list2=[“banana”,”mango”,”jackfruit”]
print(“******Indexing examples******”)

#positive indexing
print(list1[0])

#negative indexing
print(tuple1[-1])
print(“******Slicing examples*******”)

#slicing
print(list1[0:6])
print(tuple1[2:5])

Leave Comment

Your email address will not be published. Required fields are marked *

clear formSubmit