Legacy – Creating Array from Tuple Workout
- 02:28
Workout demonstrating how to create a NumPy Array from a tuple.
Glossary
Array from Tuple Arrays NumPy Library TupleTranscript
So now how do we create a tuple? And how do we create an array from a tuple? Firstly, what's a tuple? A tuple is an object in Python, which has three different items within it. So firstly, let's create a tuple of prices. Let's say that the price tuple contains the opening price, closing price. And also the interest variation or the variation of the price, say it's one j, where j is the variation. Now, let us print tuple and run this. So that's our table looks like. Let's say it's two j rather than one j. And now how do we convert this tuple into an array? So we say, prize tuple array? Is standard np dot array of the price tuple? Oh, no, we've clearly made a mistake. We haven't imported the numpy package before using the numpy function. So before using any libraries function, we need to import the library import NumPy as np now that we've converted the tuple into an array, let's look at what the tuple looks like. Let's run this. This is the array of the tuple. What the array function does is that it converts the tuple into a similar object. So every object or item within the tuple of the array looks similar. So now, why do we have to convert a tuple into an array rather than using a tuple as a tuple? Let's look at an example. If we want to multiply 10 across the tuple, what will happen? Price tuple. Instead, what does this do? It simply replicates the tuple into 10 times but rather if we replicate the tuple array by I mean multiply the tuple array by 10. It's going to multiply the individual objects in the tuple by 10, which is what we want simple