#10 Sequence data | Part 3 | Python for Data Science

Brief Summary

This lecture explores operations on sequence data, focusing on slicing, concatenation, and multiplication. It details how to use slice objects and colon operators for various data types like strings, lists, arrays, tuples, sets, dictionaries and ranges, highlighting their specific behaviours and limitations. The lecture also covers concatenation using the plus operator and multiplication for repeating elements, noting differences in application across different data types.

  • Slicing can be performed on strings, lists, arrays, tuples and ranges.
  • Concatenation joins sequences, while multiplication repeats them.
  • Dictionaries and sets, being non-sequential, do not support slicing.

Introduction to Sequence Data Operations

The lecture introduces operations applicable to sequence data, focusing on slicing. A slice object is used to extract portions of a sequence, which can be strings, tuples, bytes, lists, or ranges. The slice object uses indices specified by the range function, defined by start, stop, and step values. The syntax involves using slice(stop) or slice(start, stop, step). The 'start' argument is the index to begin slicing, 'stop' is the index to end slicing (exclusive), and 'step' determines the increment between indices.

Slicing Strings

The lecture demonstrates string slicing using the example string "learning". The slice function is used with start, stop, and step values to extract substrings. For instance, slice(1, 4, 2) extracts characters starting from index 1 up to (but not including) index 4, with a step of 2. Alternatively, the colon operator within square brackets can achieve slicing without the slice function. Using just a colon inside square brackets fetches the entire string. Slicing is useful for modifying specific parts of a string without altering the whole string.

Slicing Lists

The lecture explains list slicing, noting Python's default functionalities. If no value is specified before the first colon, slicing starts from the beginning of the list. For example, [:3] slices from the start up to index 3 (exclusive). Conversely, omitting a value after the colon implies slicing to the end of the list from a specified start index. This method avoids manually specifying the list's length. The colon operator is used to define start, stop, and step values within square brackets, mirroring the slice function's behaviour.

Slicing Dictionaries and Sets

The lecture clarifies that slicing cannot be performed on dictionaries because they use key-value indexing without a specific order. Attempting to slice a dictionary results in a TypeError. Similarly, sets, which are also containers for non-sequential data, do not support slicing, leading to a TypeError as set objects are not subscriptable.

Slicing Arrays and Ranges

The lecture demonstrates slicing on arrays, which functions similarly to lists. Using a colon extracts values from a specified index to the end of the array. Negative indices can also be used to slice from the end. Slicing also works on ranges, where specifying a colon and a negative index excludes the last element of the range.

Concatenation of Strings

The lecture introduces concatenation and multiplication as fundamental sequence data operations. Concatenation is achieved using the plus operator to join strings or elements. When concatenating strings, the plus symbol combines them. However, strings are immutable; thus, concatenating and reassigning to a new variable is necessary to preserve the combined string.

Concatenation of Lists

The lecture explains list concatenation, where adding a list to an existing list appends the new list as an element to the original list. This updates the original list with the new element at the end.

Concatenation of Arrays

The lecture addresses concatenating arrays, highlighting that arrays can only be concatenated with other arrays, not lists. Attempting to concatenate a list to an array results in a TypeError. To concatenate a list-like sequence to an array, the sequence must first be converted into an array of the same data type as the original array.

Concatenation of Tuples and Sets

The lecture covers tuple concatenation using the += operator, which adds a new tuple to an existing tuple and updates the original tuple. For sets, concatenating involves adding a value to the existing set, which updates the set with the new value.

Multiplication of Strings

The lecture explains multiplication on sequential data, starting with strings. Multiplying a string repeats the string a specified number of times. The *= operator repeats the string and updates the original string variable.

Multiplication of Lists

The lecture details multiplication on lists, where multiplying a list repeats its elements. Accessing a particular element by its index and multiplying it will repeat that element. Python does not perform explicit type conversion; it repeats string values and multiplies integer values.

Multiplication of Tuples, Arrays and Ranges

The lecture discusses multiplication on tuples, demonstrating how slicing a tuple and then multiplying it repeats the sliced elements. Multiplying an array repeats the entire array. However, attempting to multiply a range results in a TypeError because multiplication is not supported for range objects.

Conclusion and Recap

In conclusion, the lecture recaps slicing operations on sequential data and the limitations when applied to non-sequential containers. It also reviews concatenation and multiplication operations, including the nuances of multiplying strings versus numbers. The upcoming lectures will explore sequence data methods, focusing on operations and methods applicable to sequential data.

Summarize any YouTube video in seconds

Get summaries like this one for any video, in your language, with the free Summ app.

Download on the App Store
Get it on Google Play

Transcript

YouTube Transcript Generator

0:14 Hello all, welcome to the lecture on the sequence data. And in this lecture, we are going to look at some of the operations that can be performed on a sequence data. So, let's get started with slicing. So, the slice object is used to slice a given sequence and the sequence can be string, the sequence can be strings, tuples, bytes, list or range or any object which supports sequence protocol. And the slice object represents the indices specified by the range function given the start, stop and the step value. And the syntax of the slice operation, the syntax for the slice object is given here. And the syntax for the slice

1:03 function is given here. So, you can either use slice of stop or you can use slice of start, stop and step. So basically the slice function takes in three arguments which have the same meaning in both the constructs. So the first one is start which is a starting integer value where the straight which is an integer value which represents the starting from where the slicing of the object start. So the first argument is start. So that is the starting integer where the slicing of the object starts. The next one is stop. That is an integer until which the slicing takes

1:49 place. So, the slicing stops at the index n minus 1 or the minus 1. So, for example, I have given the second argument as 4. It means that is the stopped value. So, it is going to give me the sequence of values till 3. So, next one is step, an integer value which determines the increment between each index for slicing. So for example, if you are giving two, so you will fetch the, so you will be getting the values in the step size two. So by default, if you don't give anything, it is going to take it as one as the increment value. But if you are providing any value, then it is going to consider that as the increment value. So let's just get started with

2:35 the example on string. So we've already created a string called strsample. which has a string called learning. So now my objective is to get the substring from a given string str sample from the given string learning. So we can use the slice object to do that. So earn str sample. I am using the slice function inside the square brackets. Then inside the slice function I am just giving three values. One for start, stop and the step value. So here the output I've already printed that is e r which is e and r from learning so basically that we so basically we've got that by specifying the

3:22 starting index as 1 so actually the slicing started from e and it will actually end at the index 4 then it is 2 3 and 4 so n will have the index 4 but actually it is going to give you till n minus 1 right so it will stop at r And since I have given the increment value as 2, it is not going to give you all the sequence of strings from E as, it is not going to give you the sequence of strings as E, A, R, rather it is going to give you the sequence in terms of incrementing the strings by 2. So it is just going to give you as E, R alone. So it is just going to give you the output as E, R alone. So, this is how the slicing operator, so this

4:08 is how we use the slice function in order to slice the given sequence of elements or given sequence of substrings from the given string or the any sequence object. And the other method is, the other method for slicing the elements or the substrings from a given object is using the square bracket. You do not have to use the slice function inside it. you can either use it, you can either use the slice function or the colon operator. Here I am using just a colon inside the square bracket so that it gives me all the substrings from a string learning, that is learning. So, just by giving, just by just placing colon inside the

4:54 square brackets fetches me the learning, that is the complete set of strings from the main string, that is str sample. So, this is how we perform the slicing on the string sequence data. So, basically we use slicing whenever we want to perform any operations on a particular set of elements on your string or on your any or it can be applied on any sequence data. So, that all the values inside a string, all the values from a list won't get modified based on your calculation or based on your operation. Only the particular set of elements or substrings will be modified based on the operations that you are performing on it. Similarly, we can perform the slicing operations in other in any other sequence data.

5:44 For example, the next example to illustrate you is on list. So, list basically have a default bit of functionality when slicing. So, here let us just print what is there under the list sample. So, these are the values, these are the elements that is there under the LSP sample 1, 2, a, sam and 2. So, now I am going to perform slicing on the list sample. So, basically if there is no volume before the first colon, for example like this, I have not given any value before the first colon. and I have given value after the colon. In that case, it means to start at the beginning index of the list. It means to start

6:32 the slicing at the beginning index of the list. So let's just see what happens when we use colon 3. So the output is given here. So the slicing started from the index 1. So the slicing started from the beginning of the list that is from 1 itself and the next value is the stop value. So 0, 1, 2, 3. It is going to give you the sub elements from the list till or before SAM. So we've got the output as 1, 2 and A. Since the index, since the stop index is at 3, it is just going to give you

7:18 a value, it is just going to give you a sequence of elements or sequence of sub elements from your main list which is just before the index of the which is just before the third index that is 1 2 and a the other thing to note here is if there is not if there is no if you have not provided a value after the if you have not provided a value after the first colon like this then it means to go all the way to the end of the list starting from the index 2. So, I am just saying to slice starting from the index 2 and to go all the way to

8:04 the end of a list. And this basically saves this time so that we do not have to manually specify length of a which basically so that we do not have So this saves us time so that we don't have to manually specify length of LEN of LSD sample in order to specify that is the end of a list or that is to specify the, that is to specify or to check how many elements are there in a list. So in order to eradicate, in order to get rid of that, you can just use a colon after the you do not have to specify anything after the first colon so that it automatically takes all the sub elements till the end of your list.

8:55 So, this is how we perform the slicing operation on a list. So, here if you have noted down I have not used the slice function rather I have just used the colon operator to slice the sub elements from the list by just separating by just giving the start stop and the step value in terms of using colon operator and if you can recall here whatever order that you are giving here inside the slice option inside the slice function the same applies here but instead of using slice function i have just used the square bracket and if i give 2 colon 3 colon 1 then it means that starting value the starting index is 2 and the ending index is 3 and the

9:41 increment value is 1. But you can as well specify without even giving any value just to denote that I need all the values from 2 and I need all the values till 3. For example this is a start value and the stop value let's check what is the output. So the slicing operation is being done from the index 2 of the list that is 0 1 2 from a and till 4 3 and 4 and it is not going to consider the 4 as the index but it just considered to n minus 1 so that will be 3 so it is going to give you a and sam as the output so i hope this is clear

10:27 so let's move on to performing the slicing on the other sequential data So, here we have also created a dictionary sample, right? So, let us just perform slicing on it, but it gives you an error saying type error and hashable type that is slice. So, if you can recall the Python dictionary object provides a key value indexing facility and the values in the dictionary are indexed by keys and they are not held in any order. In that case, we won't be able to perform slicing because it requires the index to slice the given sequence of values or given sequence of elements to slice the sequence of values or sequence

11:15 of keys from your dictionary. So, slice So we can't perform slicing on a dictionary because it is a container for holding non-sequential data. So in that case, we won't be able to apply that onto a set as well. So let's just see what happens when we apply that onto a set. So it also throws an error saying set object is not subscriptable as we know that. set is also a container to hold non-sequential data. So, all the elements inside a set are not indexed by any indices rather they are not held in any order.

12:13 we cannot perform the slicing operation on a set also because even set is also a container for holding the non-sequential data in that case all the elements inside the set won't have any index for index corresponding to each element so so the slide so we can't perform the slicing in index as well so next one is the so the next one is slicing on the array. So let's just print what we have it under the array sample which has the values 1, 2, 3 and 4. So if you want to basically access or if you want to fetch or slice the values inside your array then you can just perform

13:00 the same slicing operation on it. I have just given one colon so that it gives me the output giving us all the values from the first index that is 2, 3 and 4 because the value because the index corresponding to the value 2 is 1. You can also slice it by giving the reverse index or the negative index. I have given access the values from starting from the index 1 and the end index should be minus 1 that is the second last element of your array. So let us just see what is the output. So in that case you will just have 2 and 3 because you have excluded the first element that corresponds to the index 0 and the last value because we have given minus 1.

13:51 Similarly, you can perform the same on a range as well. Since I have given range sample and inside the square bracket I have given colon and minus 1. So, basically you will be fetching or you will be slicing all the values from the first but you don't you won't be able to but then you will just exclude the last element of your array last element of the range then it gives you only 1 comma 9 comma 4. So, now we have seen how to basically perform the slicing operation on the sequential data and now we will learn some of the most fundamental sequence data

14:38 operation that is concatenation and multiplication. So, basically this will be helpful when this will be helpful in. So, this will be helpful when you are looping over your looping over elements in your containers. So, now we will learn some of the most fundamental sequence data operations that is concatenation and multiplication which can be applied onto any sequential data. So, we are going to do concatenation using the plus operator. And there are a few ways of doing this depending on what you are trying to achieve. And the simplest and the most

15:26 common method is to use the plus symbol to add multiple strings together or to add multiple elements together. It can be applied onto any sequential data as well. And simply place a plus between as many as simply place a plus between as many strings as you want to join together. So let's see an example to do that. So for example, So we know what is there under str sample, it just has a word learning, right? So I am going to concatenate two strings together using the plus operator here. So I am going to concatenate str sample with another string that is python. But before python, I have given a space inside

16:16 single code. So it is going to have a space in between learning and python so that is why i have given a space so this is just to join this is just to join two strings together so let us just see the output so the first line is the output of the print str sample and the And in the next line, I'm just checking whether it has been updated in the str sample or not because I've added or I've joined a new string to the existing string. So let me just check. So let me just print str sample and see what is there. But unfortunately, it has only learning. The Python doesn't

17:02 get updated to the existing str sample. That is why because remember strings are immutable. if you concatenate or repeat a string stored in a variable you will have to assign the new string to another variable in order to keep it right so let's just do that so whatever operation that i'm performing here i'm just updating it to a new variable so that it gets updated So now if you print the new string then it will have learning comma python as specified in the concatenation. So this is how we do concatenation on a string. So this will be very handy when you want to when you are performing when you are looping through the objects in your data and if you want to perform some of the operations like concatenation or

17:52 multiplication this will be very handy. So next let's see some of the, so next let's see the concatenation on list. So this is the original list that we have 1, 2, a, sam, 2. And in the next line, I am concatenating a list to the existing list LST sample. And the list contains a string that is PY. I am giving that inside the square bracket because you will be able to concatenate two lists So if you can see here, the py list

18:41 has been added as an element of the existing, has been added as an element to the existing list that is LST sample. So we do not have a separate list which contains a py string, rather it has been added or gets updated in the original list itself or gets updated as a last element in the original list itself. And let's see an example on how we can concatenate the arrays. Let's see an example whether we will be able to concatenate two different sequence data. For example, let's try an example to concatenate the list and the arrays together. So first let's see

19:27 what is there under the array that it has basically values 1, 2, 3 and 4 which are of integer data type. And by using a plus operator, I am going to concatenate a list together. I am going to concatenate a list to the existing array. So let's see whether that happens or not. So it basically throws you a traceback errors. It basically throws you a type error saying that it can only append array to list. It can't append array to a. So the type error is throwing. Basically it says it can, the type error saying can only append array to array, not list to array. So, we because we are trying to concatenate or we are trying to append the list to

20:15 an existing array that is not possible. So, what we have to do here means if you want to concatenate to an array, if you want to concatenate a sequence data to an array, then that sequence data should be of the same kind that is array. So, what we can do here is we can use the same values but just put them as array. basically using the array function and keeping the data type as integer and giving the values as array and giving the values as list inside the array. Array sample will get concatenated with the given values that is 50 and 60 since you created it as array and then concatenate it.

21:01 So, the next one is concatenation on concatenating two tuples together so here if you can see here I have just used plus and equal to then it means that I am going to add a new tuple to the existing tuple tube sample and that is automatically and that gets updated in the tube sample itself so the tube sample basically had 1 2 3 4 3 and P Y I have added TH ON separately as a and I have added a new tuple called and I have added a new tuple having two strings th and on so and that gets added to the existing tuple

21:47 sample and that gets updated on the existing tuple sample because I have given plus and equal to. The next one is concatenation on set. So basically here I am already have a set sample which has values 24 data, example and 87.5 as values of set and I am doing concatenation here. I am just converts and I am just going to convert to tuple with comma separated elements of set and dictionary.

22:41 So, I am just concatenating a value to the existing set sample by just placing a comma after set sample and then updating it to the set sample. So, that value will get updated in the actual set sample. So, if you can see here, I have run multiple times. So, each and every time you run, it is going to update it in the original set sample. So similarly you can perform the operation called multiplication on any sequential data. For example, multiplication in the sense if you are applying the

23:27 multiplication on a string then it means that you are going to multiply a string, you are going to repeat a set of strings by just giving a number. Multiplication in the sense if you have strings and it has some if you have a string then if you are doing some multiplication on it then if you are performing multiplication operation on it then it is going to repeat the values to the number of times that you've given as a multiplier so here just let here let's see what happens so i have a str sample here i have given aster equal to and three

24:13 So I have strsample as strict equal to 3. It means that I am going to repeat whatever is there under strsample and then I am going to update it to the existing strsample itself. So let us just run. So learning is a string under strsample. Since I have given 3 asterisk 3 it has repeated the string tries and it gets updated in the str sample and that is why while printing the str sample you got the output as learning learning and learning but what happens when you perform it on the list it also has both numbers

25:02 and strings to it but in this case since it has string to it it is but in this case it is just going to repeat the elements of your list twice since you've given asterisk and 2. And you can as well repeat a particular element to the number of times by accessing them using the index. So, here I am just going to repeat a value corresponding to the first index twice that is 4. But you will be able to, for example,

25:49 but you will be, but if you can print the list sample and see, but if you can print the list sample and see, the value is not repeated twice because you have not updated that there. So, if you just give equal to symbol here and then execute it, So basically the python doesn't do explicit conversion. For example, if you are performing

26:38 an operation which has string, then it is not going to multiply a number with a given value. Rather it is just going to repeat the values to the number of times that you have given. But if you are trying to access a particular value which is of integer and then you are performing a multiplication operation on it, and then in that case it is going to multiply that particular value to the given number of times. Let's see an example. Here we have a value that is 2 that corresponds to the first index and I am just trying to multiply that with the value 2 and I am also trying to update it in the original list. In that case it is going to give me an output saying 8.

27:57 So now let's see what will be the output. So in this case, originally you had 1, 2, a, sam and 2 and since I have given LST sample of 1 as to equal to 2, that particular value has been multiplied with the given value that is 2. So, 2 times 2 will give you a value called 4 and that gets dated in your additional list. So, this is what happens when you are performing a multiplication operation on an integer or a float. But whereas if you have any string value and if

28:45 you are trying to perform some multiplication operation on it, for example, we have a value called a right. So, 0, 1, 2, 2 is the index corresponding to it. let us just try what happens so it is going to repeat that particular element to the number of times that you've given here since i've given to the value gets repeated twice so this is what happens when you do multiplication operation on any kind of sequential data that you have yeah the same example i've repeated in the below cell so already it is double A and if you are trying to multiply that with the number 2 then it is going to repeat it twice again. Let us

29:33 see what happens when you do the multiplication on a tuple. So, initially it had, so initially, so let us just print what is there in the tuple sample. So here what I am doing here is, so what I am doing here is I am going to slice a particular set of elements from the tuple and then going to do and then perform the multiplication operation. So before that let us just print what is there in the tuple sample.

30:22 So what is there in your tuple sample is 1, 2, 3, 4, 3 and PY. and 2 colon 4 is nothing but 3 and 4. So 2 colon is 2 colon 4 slices the elements that is 3 and 4 and I am going to multiply that twice. In that case it is just going to multiply them it is just going to repeat that particular element twice.

31:14 And the next thing is performing the multiplication on array. So it also basically multiplies, it also basically repeats the given array twice since you've given asterisk 2. And the next one is range sample. We are just multiplying the range sample by 2. It throws an error saying type error and supported operand type for asterisk range and i. range and integer because you would not be able to perform the multiplication operation on a range because using range you will be able to perform you will be you will be able to only create a sequence of values given range

32:00 and we have come to the end of the script and in this lecture we have seen in continuation with the previous lecture In this lecture we have seen how to perform some of the slicing operations on any sequential data and we have also seen what happens when we do the sequential and we have also seen what happens when we do slicing on a container which stands for holding non-sequential data. And followed by that we have seen how to perform some of the operations like incatenation and multiplication on any sequential data and we have also seen some of the checks for example what happens when we try to multiply two numbers and what happens when we do the multiplication on a string.

32:51 And in the upcoming lectures we will be seeing some of the methods that can be methods that are dedicated for and in the upcoming lectures we will be looking at sequence data methods where we will be concentrating on, where we will be concentrating more on what we can do with the sequential data and what are the operations or what are the methods that you can. So, methods, so in the upcoming lectures we will be looking at how we can use the sequential data to perform any operations. So, we are wrapping up. in the upcoming lectures we will be seeing about sequential data methods that is nothing but how to apply the methods on any sequential data thank you

Share

YouTube Video Summaries