Tutorial Playlist

Python Tutorial for Beginners

Overview

The Best Tips for Learning Python

Lesson - 1

How to Install Python on Windows?

Lesson - 2

Top 10 Python IDEs in 2020: Choosing The Best One

Lesson - 3

A Beginnerā€™s Guide To Python Variables

Lesson - 4

Understanding Python If-Else Statement

Lesson - 5

Python Numbers: Integers, Floats, Complex Numbers

Lesson - 6

Python Strings | Simplilearn Python Tutorial

Lesson - 7

The Basics of Python Loops

Lesson - 8

Python For Loops Explained With Examples

Lesson - 9

Introduction to Python While Loop

Lesson - 10

Everything You Need to Know About Python Arrays

Lesson - 11

All You Need To Know About Python List

Lesson - 12

How to Easily Implement Python Sets and Dictionaries

Lesson - 13

A Handy Guide to Python Tuples

Lesson - 14

Everything You Need to Know About Python Slicing

Lesson - 15

Python Regular Expression (RegEX)

Lesson - 16

Learn A to Z About Python Functions

Lesson - 17

Objects and Classes in Python: Create, Modify and Delete

Lesson - 18

Python OOPs Concept: Here's What You Need to Know

Lesson - 19

An Introduction to Python Threading

Lesson - 20

Getting Started With Jupyter Network

Lesson - 21

PyCharm Tutorial: Getting Started with PyCharm

Lesson - 22

The Best NumPy Tutorial for Beginners

Lesson - 23

The Best Python Pandas Tutorial

Lesson - 24

An Introduction to Matplotlib for Beginners

Lesson - 25

The Best Guide to Time Series Analysis In Python

Lesson - 26

An Introduction to Scikit-Learn: Machine Learning in Python

Lesson - 27

A Beginner's Guide To Web Scraping With Python

Lesson - 28

Python Django Tutorial: The Best Guide on Django Framework

Lesson - 29

Top 10 Reason Why You Should Learn Python

Lesson - 30

10 Cool Python Project Ideas For Beginners in 2021

Lesson - 31

The Best Ideas for Python Automation Projects

Lesson - 32

12 Tips On How To Become a Python Developer

Lesson - 33

Top 50 Python Interview Questions and Answers in 2020

Lesson - 34

The Best Guide for RPA Using Python

Lesson - 35

Comprehending Web Development With PHP vs. Python

Lesson - 36

The Best Tips for Learning Python - REMOVE

Lesson - 37

The Best Way to Learn About Box and Whisker Plot

Lesson - 38
Everything You Need to Know About Python Slicing

Python is a widely used, high-level, general-purpose programming languageā€”often used to develop GUI applications and web applications. It is incredibly appealing in the discipline of rapid application development because it offers dynamic typing and binding options. In this article, we will be learning about an essential topic in Python programming: Python slicing.

What Is an Index?

An index is a position of an individual character or element in a list, tuple, or string. The index value always starts at zero and ends at one less than the number of items.

index-position

Fig: Index position

Negative indexes enable users to index a list, tuple, or other indexable containers from the end of the container, rather than the start.

Python Training Course

Learn Data Operations in PythonExplore Course
Python Training Course

What Is Slicing?

Slicing is the extraction of a part of a string, list, or tuple. It enables users to access the specific range of elements by mentioning their indices.Ā 

Syntax: Object [start:stop:step]

  • ā€œStartā€ specifies the starting index of a sliceĀ 
  • ā€œStopā€ specifies the ending element of a slice
  • You can use one of these if you want to skip certain items

Example:

slicing

Fig: Python slicing

Note: The search will start at index one (included) and ends at index six (not included).

The following image shows some examples of slicing in a string and tuple:

additional

Fig: Additional slicing examples

Slice with Negative Indices

Negative slicing begins at the end of the list.

Example:

Fig: Slicing using negative indices

Specify Step of the Slicing

You can follow a specific step in order to skip certain items.Ā 

Example:

stepped

Fig: Stepped slicing

In the above example, the string ā€˜SIMPLILEARNā€™ is sliced from indexes one through seven. However, because the step size was set as number two, you will receive every second character in the output starting from the first index.

Reversing Elements of Data Structure

We can use a negative step to reverse the elements of a data structure.

Example:

reversing

Fig: Reversing elements using negative step

Free Course: Introduction to Numpy

Master the Fundamentals of NumpyEnroll Now
Free Course: Introduction to Numpy

Slice() Function in Python

The slice() method extracts a section of data and returns it as new data, without modifying it. This means users can take a specific range of elements without changing it.

Syntax: slice(start,stop,step)

Example:

slice-function

Fig: slice() function

Insert and Delete Multiple Items Using Slicing

1. Slice insertion in Python

Users can insert items into a list without replacing other elements.Ā 

Example:

inserting

Fig: Inserting elements using Python slicing

2. Slice deletion in Python

Users can delete multiple items out of the data structure by using a del statement.

Example:

deletion

Fig: Delete list items using Python slicing

Note: Tuple object does not support item deletion.

Looking forward to make a move to programming? Take up theĀ Python Training CourseĀ and begin your career as a professional Python programmer.

Conclusion

In this article, we discussed two key concepts: Python slicing and indexing. We looked at how slicing, negative slicing, and step-indexing works through various examples. Both concepts are essential for understanding Python.Ā 

If you have any questions, please leave them in the comments section, and we'll have our experts answer them for you.

Want to Become a Pro in Python?

Python skills are in high demand in many industries today and is one of the core tools used in the exciting fields of data science and data analytics. If you want to boost your career or simply get started, enroll in our Python Training Course today!

About the Author

SimplilearnSimplilearn

Simplilearn is one of the worldā€™s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.