Accessing index and value in list | Python

we will discuss how the data types such as list, tuple, set, and dictionary to define and how to manipulate them. But before we know some simple things.

List

A list in Python is known as a “sequence data type” like strings. It is an ordered collection of values enclosed within square brackets [ ]. Each value of a list is called an element. It can be of any type such as numbers, characters, strings, and even nested lists as well.

A-List is an ordered, indexed, and sequence data type. A list is a mutable data type (changeable). The elements can be modified or mutable which means the elements can be replaced, added, or removed. Before it modified, we will know some things.

Indexing

Python assigns an automatic index value for each element of a list that begins with zero. The index value can be used to access an element in a list. In python, the index value is an integer the number that can be positive or negative.

A positive value of index counts from the beginning of the list and a negative value means counting backward from the end of the list (Reversed order).

Python virtual image

Reverse Indexing

Python also allows reverse or negative indexing for the list elements. The python sets -1 as the index value for the last element in a list and -2 for the preceding element and so on. This is called Reverse Indexing.

Python virtual image

slicing

A slice is a substring of the main string. A substring can be taken from the original string by using [ ] operator and index or subscript values. Thus, [ ] is also known as the slicing operator.

Syntax of Slicing:

str_name [start : end : step]

Where start is the beginning index and end is the last index value of a character in the
string and step is how many elements we want to skip. It takes the last element as 1-n.

Example:

  • >>>blog = “Iterathon”
  • >>>blog[0]
  • >>>’I’ as output
python virtual example

This topic is known before we modifying data types such as lists, tuple, set and dictionary. I hope, this concept is very easy to understand.

My Previous Post

Fundamentals of Python Programming: https://iterathon.tech//fundamentals-of-python-programming-with-examples/

LEARN LITTLE BY LITTLE TO CHANGE THE TECH WORLD

Similar Posts

Leave a Reply

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