1# Basic syntax:
2your_list[start:stop:step]
3
4# Note, Python is 0-indexed
5# Note, start is inclusive but stop is exclusive
6# Note, if you leave start blank, it defaults to 0. If you leave stop
7# blank, it defaults to the length of the list. If you leave step
8# blank, it defaults to 1.
9# Note, a negative start/stop refers to the index starting from the end
10# of the list. Negative step returns list elements from right to left
11
12# Example usage:
13your_list = [0, 1, 2, 3, 4, 5]
14your_list[0:5:1]
15--> [0, 1, 2, 3, 4] # This illustrates how stop is not inclusive
16
17# Example usage 2:
18your_list = [0, 1, 2, 3, 4, 5]
19your_list[::2] # Return list items for even indices
20--> [0, 2, 4]
21
22# Example usage 3:
23your_list = [0, 1, 2, 3, 4, 5]
24your_list[1::2] # Return list items for odd indices
25--> [1, 3, 5]
26
27# Example usage 4:
28your_list = [0, 1, 2, 3, 4, 5]
29your_list[4:-6:-1] # Return list items from 4th element from the left to
30# the 6th element from the right going from right to left
31--> [4, 3, 2, 1]
32# Note, from the right, lists are 1-indexed, not 0-indexed
1word = "Example"
2
3# Obtain the first 3 characters of "Example"
4# E x a m p l e
5# 0 1 2 3 4 5 6
6# First 3 characters = "Exa"
7sliced_word = word[:3] # Gives you "Exa"
8
9# Everything after character #3 = "mple"
10second_sliced_word = word[3:] # Gives you "mple"
1a = [1,2,3,4,5]
2a[m:n] # elements grrater than equal to m and less than n
3a[1:3] = [2,3]
1# [start:stop:step]
2# for example ...
3l = [1, 2, 3, 4, 5]
4l[1:4] # elements from 1 (inclusive) to 4 (exclusive)
5l[2:5:2] # elements from 2 (inclusive) to 5 (exclusive) going up by 2
1ing in python listPython By Cruel Capuchin on Jun 29 2020
2a = [1,2,3,4,5]
3a[m:n] # elements grrater than equal to m and less than n
4a[1:3] = [2,3]