Software engineering and personal development

Tag: console

How to Quickly Get Sub-Tuples in Python

Python provides a way to get a sub-tuple from an existing tuple by specifying the starting index of the sub-tuple. The syntax for this is similar to that used for lists. We use the slice notation [start_index:] to specify the starting index of the sub-tuple.

Here’s an example:

my_tuple = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
print(my_tuple[3:])  # (4, 5, 6, 7, 8, 9, 10)

In this example, we created a tuple called my_tuple that contains ten elements. We then used the slice notation [3:] to get a sub-tuple starting from index 3. The resulting sub-tuple contains all elements from index 3 to the end of the tuple.

We printed the resulting sub-tuple to the console using the print() function. The output of the program is (4, 5, 6, 7, 8, 9, 10).

If we want to get a sub-tuple that contains a specific number of elements, we can use the slice notation [start_index:end_index]. Here’s an example:

my_tuple = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
print(my_tuple[3:7])  # (4, 5, 6, 7)

In this example, we used the slice notation [3:7] to get a sub-tuple containing the elements from index 3 to index 6 (inclusive).

That’s basically it.

I hope you find this useful.

Underestimated testing tool for Rails applications: Execute console commands with real data without fearing of losing it

Img source: careerfoundry.com

There can be cases when you need to test something with the actual data, in a production environment, but you do not want to risk anything. You do not want to even think about the possibility for a data loss of the project that you are working it. Luckily, there is a really beneficial feature that is not that much known from Rails developers is a quick tool that you can use to test out some code without changing any data in your real database.

Continue reading

© 2024 Fatos Morina

Theme by Anders NorenUp ↑