Python: Tuplas 4/12

in #programming6 years ago

Source.



A tuple is defined as a variable that allows you to store several immutable data.

That is to say that once created they can not be modified, and these data can be different types, so immutability is the main characteristic of tuples.

We declare a tuple of preference must be something descriptive, that is to say that we place a name that identifies the tuple.

Example.

The words are separated by a low script.

Mi_tupla.

In the variables with complex names, the _

We are going to assign a sequence of data that we just wrap it in parentheses and we will separate it by a comma.

For example.


My first element would be a text string, and I separate the commas.

(Index zero') It is said that each number that is inside the tuple is an item, so we can continue adding items.

Remembering that the types of data could be varied.

This chain would have.

(Index zero', 2, 3.0,'indice three')


This string has an integer, a real number and to end another string Index three'.
This would be a tuple with 4 items.

How can we access the elements of our tuples?


Source.

As we have already seen, it is a sequence of data, to each data corresponds a position in said sequence to identify the positions we begin counting from zero.

(Index zero'2, 3.0,'indice three')

Index zero = belongs to position 0.

2 = It belongs to position 1.

3.0 = It belongs to position 2.

Index three = It belongs to position 3.

Therefore it is said that our data is indexed, that is.

To each data belongs an index, the index that corresponds to the first element would be 0, and so it goes successively until the last element.

To execute, we place Print.

Formula:

Print + Variable + [] + index corresponding to the element.

Example.

Print (my_tuple [0])


Index zero.

Print (my_tuple [2])

3.0

We print our position 2 and it shows us the 3.0 that is our corresponding to index 2.

Python also offers the possibility of handling with negative numbers taking into account that the -1 index corresponds to the last element and thus you go backwards.

For example.

Print (my_tuple [-1]) We should print index three, which is the last one.

Index three.

To print the penultimate we just have to put the -2 and so on, we just have to go in reverse.

So we can decide how to go to our elements, whether in a regressive order or the other way around.

We can access portions or what could be known as slices or as they call it in Python as:

Slicing:


Portion of the tuple indicating, optionally, from the start index to the final index, what portion of the tuple we want to access.

What allows us to tell Python to interpret that we only want a certain part of the tuple, we simply put a start index and a final index, without including the element that corresponds to the final index, that is; inside the brackets we simply place an index of beginning, the 1 and we separate by two points and we place the final index 3, but the value that corresponds to this index is not going to be shown. It will go from index 1 to 2, since 3 is not taken into account.

Example.


Print (my_tupla [1: 3])

2.3.0

We can even write the start index or the final index, and it will be interpreted in such a way that if we do not set the start index it will automatically take us from the beginning to the element that we put at the end, and if we do not put the final index automatically It will include us from the index that we place, from the beginning to the end of the tuple.

For example.


Print (my_tupla [: 3])

Here we omit our start index and it will automatically include us from the beginning of the list up to 1 before the final index, as we saw we give the enter and we see.

(Index zero'2, 3.0) It takes us from the beginning of the tuple.

On the contrary we can also omit the final index, and it will take us from the final index, and it will take us from the initial index to the end of the tuple.

Print (my_tuple [1:])

Take from the start index to the end of the tuple and take us to the end.

(2.3.0 Index three')


Well, this is all for today, we should be putting into practice the tuples; They are very simple but we forget their importance every day, and their function could be useful.

Coin Marketplace

STEEM 0.20
TRX 0.12
JST 0.029
BTC 61795.04
ETH 3458.89
USDT 1.00
SBD 2.52