Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the simple-sitemap domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/u246705441/domains/gangofcrypto.com/public_html/wp-includes/functions.php on line 6114

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the simple-sitemap domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/u246705441/domains/gangofcrypto.com/public_html/wp-includes/functions.php on line 6114

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the ultimate-member domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/u246705441/domains/gangofcrypto.com/public_html/wp-includes/functions.php on line 6114

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the updraftplus domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/u246705441/domains/gangofcrypto.com/public_html/wp-includes/functions.php on line 6114

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the wpforms-lite domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/u246705441/domains/gangofcrypto.com/public_html/wp-includes/functions.php on line 6114

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the wordpress-seo domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/u246705441/domains/gangofcrypto.com/public_html/wp-includes/functions.php on line 6114
Lists & Tuples - Gang of Crypto

Lists & Tuples

These are called compound data types and are one of the key types of data structures in Python.Tuples, are an ordered sequence. Here is a tuple ratings.
rating=(’10’,’5′,’7′,’1′,’8′)
Tuples are expressed as comma separated elements within parentheses, so these are values inside the parentheses.

In Python, there are different types,strings, integer, float. They can all be contained in a tuple. But the type of the variable is tuple. Each element of a tuple can be accessed via an index.
rating[0]//10
The first element can be accessed by the name of the tuple, followed by a square bracket with the index number-in this case, 0.We can also access the last element. In Python, we can use negative index. The relationship is as follows: The corresponding values are shown here. We can concatenate or combine tuples by adding them. The result is the following with the following index.
rating2=rating+(‘5′,’1′,’9′)//result:(’10’,’5′,’7′,’1′,’8′,’5′,’1′,’9′)
If we would like multiple elements from a tuple, we could also slice tuples. For example, if we want the first three elements we use the following command:
rating[0:3]
The last index is one larger than the index you want.
Similarly, if we want the last two elements, we use the following command.
rating[7:9]
Notice how the last index is one larger than the length of the tuple.

We can use the len command to obtain the length of the tuple. As there are 8 elements. The result is 8.
len(rating)

Tuples are immutable, which means we can’t change them.
Each variable does not contain a tuple but references the same immutable tuple object.

Let’s say we want to change the element at index 2. Because tuples are immutable, we can’t. Therefore, ratings 2 will not be effected by a change in rating because the tuple is immutable-i.e, we can’t change it. We can assign a different tuple to the ratings variable. The variable ratings now references another tuple. As a consequence of immutability,if we would like to manipulate a tuple, we must create a new tuple instead. For example, if we would like to sort a tuple, we use the function sorted. The input is the original tuple. The output is a new sorted tuple.

ratingsSorted=sorted(rating)

A tuple can contain other tuples as well as other complex data types. This is called nesting. We can access these elements using the standard indexing methods. If we select an index with the tuple, the same index convention applies. As such, we can then access values in the tuple. For example, we could access the second element.
rating[1]//accessing the value of the second element
for example:
tuple1=(‘1′.'(13,56)’);
tuple1[1:1]//to access the value 56
We can apply this indexing directly to the tuple variable tuple1.

We can visualize this nesting as a tree. The tuple has indexes. If we consider indexes with other tuples, we see the tuple at index 1 contains a tuple with two elements. We can access those two indexes. We can access the elements in those tuples as well. We can continue the process.

Lists are also an ordered sequence.

A list is represented with square brackets. In many respects, lists are like tuples. One key difference is they are mutable. Lists can contain strings, floats, integers. We can nest other lists. We also nest tuples and other data structures. The same indexing conventions apply for nesting. Like tuples, each element of a list can be accessed via an index. We can concatenate or combine list by adding them. The result is a new list.Lists are mutable, therefore, we can change them.

# Create your first tuple
tuple1 = (“disco”,10,1.2 )
tuple1
(‘disco’, 10, 1.2)

# Print the type of the tuple you created
​type(tuple1)
tuple

# Print the variable on each index
​print(tuple1[0])
print(tuple1[1])
print(tuple1[2])
disco
10
1.2

# Print the type of value on each index
​print(type(tuple1[0]))
print(type(tuple1[1]))
print(type(tuple1[2]))
<class ‘str’>
<class ‘int’>
<class ‘float’>

# Use negative index to get the value of the last element
tuple1[-1]
1.2

# Use negative index to get the value of the second last element
tuple1[-2]
10

# Use negative index to get the value of the third last element
tuple1[-3]
‘disco’

# Concatenate two tuples
tuple2 = tuple1 + (“hard rock”, 10)
tuple2
(‘disco’, 10, 1.2, ‘hard rock’, 10)

# Slice from index 0 to index 2
tuple2[0:3]
(‘disco’, 10, 1.2)

# Slice from index 3 to index 4
tuple2[3:5]
(‘hard rock’, 10)

# Get the length of tuple
len(tuple2)
5