Lists, Ranges, and tuples in Python

Purushoth Anandaraja
Nerd For Tech
Published in
4 min readSep 25, 2020

--

In this let’s see the data structures lists, Ranges, and tuples in python. below mentioned methods and style are some of the common practices that followed within the python developers in the present time.

Lists

A list is a collection that is ordered and changeable. In Python, lists are written with square brackets.

We can access a list item by mentioning its index in the list and also by calling the list backward (-1, -2)we can print the list items in python.

Print the lists within the ranges is also possible in python. [beginning index: end index]

Print the list from the desired index and print up to a required index is also possible in python lists [starting index: ] and [: ending index].

Access list elements
Access the list elements by listing the elements.
Output of the above code snippet
The output of the code snippet

There are more important methods you can see in lists in order to make your tasks simple and efficient.

Append method: joins another element into the list.

Len method: displays the length of the list.

Remove method: removes the element which you want to remove.

Pop method: remove the specified item or removes the last item of the list if no element specified.

Clear method: empties the whole list.

Copy method: Make a copy of a list.

Sort method: sorts the list.

Other commonly used methods in lists
The output of the code snippet

We also can join two lists. There are several ways to join or concatenate, two or more lists in Python.

One of the easiest ways is by using the + operator. In the below diagram I have described two simple ways of sorting lists and printing them. Also, an approach to print a list in a backward manner.

Joining two lists and sorting them with printing it in backward.
The output of the code snippet

Range

The range() function returns a sequence of numbers, starting from 0 by default, and increments by a user-defined value and stops before a specified number. Range(start,stop,step)

start: integer starting from which the user has defined.

stop integer before which the sequence of integers is to be returned.
The range of integers ends at stop — 1.

step: integer value which determines the increment between each integer in the sequence.

Range function
The output of the above code snippet

Tuples

A Tuple is a collection of Python objects separated by commas. In some ways, a tuple is similar to a list in terms of indexing, nested objects, and repetition. Once a tuple is created, you cannot change its values. Tuples are unchangeable.

Print a tuple

Print a tuple within a range.

Print a tuple within a range

We cannot change a tuple but there is a workaround. You can convert the tuple into a list, change the list, and convert the list back into a tuple.

Add an element to a tuple

By using len() method we can find the length of the tuple.

finding the length of a tuple

To remove an item use the del keyword to delete the tuple.

remove a tuple.

To create a tuple you have to add a comma after the item, otherwise, Python will not recognize it as a tuple.

create a tuple

We can also join a tuple in the following way.

join two tuples

There are other methods we can use it tuple to make our life easier.

Count() — Returns the number of times a specified value occurs in a tuple.

Index() — Searches the tuple for a specified value and returns the position of where it was found.

I hope that’s all for this article now. let’s discuss in another article about Python Dictionaries and Sets!, Until then cheers to all.!!!

--

--