Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Use of slice() in Python

  • Last Updated : 02 Jul, 2021

Sometimes the code becomes an unreadable mess of hardcoded slice indices. Consider the following code which stores the details of students(Name, Phone Number, Address, Marks in 5 subjects) in list, displays them one by one and finally displays the average total marks.
 

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

Python3




L = [('Student A', 9876543210, 'Address of A', 99, 82, 97, 78, 69),
     ('Student B', 9999999999, 'Address of B', 90, 83, 87, 72, 67),
     ('Student C', 8888888888, 'Address of C', 88, 41, 56, 61, 93)]
 
 
# Display (name, address, phone no., avg marks) for each student
for student in L:
    print(student[0:3], "Marks:", sum(student[3:])/5)
 
# Average total marks of the class
sum_of_marks = 0
for student in L:
    sum_of_marks += sum(student[3:])
     
print(sum_of_marks / 3)

Output: 
 

('Student A', 9876543210, 'Address of A') Marks: 85.0
('Student B', 9999999999, 'Address of B') Marks: 79.8
('Student C', 8888888888, 'Address of C') Marks: 67.8
387.6666666666667

The above code does its work but a problem that might occur with this code is that if in future we decide to add an entry “Roll no” at index 1. Then the whole code will need to be changed. Also, this code is not so good at readability either. This is just a small example. In large projects we write thousands of lines of codes. Therefore, it becomes our duty to write readable and maintainable code. 
The same code can be made readable and maintainable to an extent by naming the slices using the slice() constructor.
 



Syntax: 
 

  • slice(stop) 
     
  • slice(start, stop, step) 
     

Parameters: 
start: Starting index where the slicing of object starts. 
stop: Ending index where the slicing of object stops. 
step: It is an optional argument that determines the increment between each index for slicing.
Return Type: Returns a sliced object containing elements in the given range only. 
 

Note: slice(1, 5, 2) corresponds to a[1:5:2] and slice(1, 4) corresponds to a[1:4].
Let’s rewrite our code.
 

Python3




l = [('Student A', 9876543210, 'Address of A', 99, 82, 97, 78, 69),
     ('Student B', 9999999999, 'Address of B', 90, 83, 87, 72, 67),
     ('Student C', 8888888888, 'Address of C', 88, 41, 56, 61, 93)]
 
# Naming slice
details = slice(0, 3)
marks = slice(3, 8)
 
# display (name, address, phone no., avg marks) for each student
for student in l:
    print(student[details], "MArks:", sum(student[marks])/5)
 
# display average total marks of the class
sum_of_marks = 0
for student in l:
    sum_of_marks += sum(student[marks])
print(sum_of_marks / 3)

Output: 
 

('Student A', 9876543210, 'Address of A') MArks: 85.0
('Student B', 9999999999, 'Address of B') MArks: 79.8
('Student C', 8888888888, 'Address of C') MArks: 67.8
387.6666666666667

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :