mobile theme mode icon
theme mode light icon theme mode dark icon
Random Question Random
speech play
speech pause
speech stop

Subscripting in Python: Accessing Elements within Sequences

In Python, subscripting is the process of accessing a specific element or index within a sequence (such as a list, tuple, or string) using square brackets (`[]`).

For example, if we have a list `my_list` with three elements:
```
my_list = [1, 2, 3]
```
We can access the first element of the list by using the index `0`:
```
print(my_list[0]) # prints 1
```
Similarly, we can access the second element of the list by using the index `1`:
```
print(my_list[1]) # prints 2
```
And we can access the third and final element of the list by using the index `2`:
```
print(my_list[2]) # prints 3
```
We can also use negative indices to access elements from the end of the list. For example, `my_list[-1]` will give us the last element of the list, and `my_list[-2]` will give us the second-to-last element.

It's important to note that subscripting in Python is zero-based, meaning that the first element of a list has an index of 0, not 1. This can be a bit tricky to wrap your head around at first, but it's an important concept to understand when working with lists and other sequences in Python.

Knowway.org uses cookies to provide you with a better service. By using Knowway.org, you consent to our use of cookies. For detailed information, you can review our Cookie Policy. close-policy