[Virtual Presenter] Welcome to our topic on lists. In this session, we will explore the world of lists in Python programming. We will learn how to create lists, manipulate them, and use various functions to extract information from them. Let's get started!.
[Audio] A list is a sequence of values, which can be of any type. It is a data structure that holds an ordered collection of items, where you can store a sequence of items. The values within a list are referred to as elements or items. And there are various methods to create a new list, the most straightforward being to enclose the elements in square brackets, like this: ListName = [element1, element2,...]..
[Audio] Since we can add and remove items, we say that a list is a mutable data type, meaning it can be changed..
[Audio] A list can contain different types of data. One example is a list containing strings like shark, cuttlefish, and squid. This list is named sea_creatures. Another example is a list containing integers ranging from 0 to 12. This list is named numbers..
[Audio] The elements of a list do not have to be the same type. For instance, we can look at the example of L3, which contains a string, a float, an integer, and even another list. This demonstrates that lists can hold different types of values. Moreover, we can create an empty list using empty brackets, such as: Empty_List = [ ]..
[Audio] We create a list by enclosing its elements in square brackets. For example, we can create a list of cheeses like this: ['Cheddar', 'Edam', 'Gouda']. Or, we can create a list of numbers like this: [17, 123]. We can also create an empty list using the same syntax: []. To print a list, we use the print function. For instance, if we have a list of cheeses and we want to print it, we would write: print(['Cheddar', 'Edam', 'Gouda']). This will output: ['Cheddar', 'Edam', 'Gouda']. Similarly, if we have a list of numbers and we want to print it, we would write: print([17, 123]). This will output: [17, 123]. And finally, if we have an empty list and we want to print it, we would write: print([]). This will output: []..
List indexing.
[Audio] The elements in a list can be accessed using the index operator which is denoted by square brackets. To access an element specify its position or index inside the brackets. Remember the indices start at zero. For example if we have a list called Number with the values 1 2 3 4 5 we can access each value using its corresponding index..
[Audio] To access the first element, we use the index zero. Therefore, Number[0] equals one. To access the second element, we use the index one. Consequently, Number[1] equals two. Similarly, to access the third element, we use the index two. Hence, Number[2] equals three..
[Audio] When working with lists, it's essential to remember that if you attempt to access an element that doesn't exist, Python will raise an IndexError. This can happen when you use an index number greater than the length of the list minus one. For instance, suppose we have a list called Number containing the values 1 through 5. If we try to access the sixth element using the index 5, Python will throw an error because there is no such element. We can see this by trying to print Number[5], which would result in an IndexError. Remember, any index number above 4 will result in an IndexError..
[Audio] When we use negative indexes, we are actually counting backwards from the end of the list. Let's consider the example Number = [1, 2, 3, 4, 5]. If we use an index of -1, we will get the last element, which is 5. If we use an index of -2, we will get the second-to-last element, which is 4. And so on. This way, we can access elements from the end of the list instead of starting from the beginning..
[Audio] The first element of the list is printed as 2 because it's num[0]. The third element from the end is printed as 1 because it's num[-3]. The last element of the list is printed as 5 because it's num[-1]. Trying to access the sixth element from the end raises an error because there are only five elements in the list. The third element of the list is printed as 1 because it's num[2]. The fifth element of the list is printed as 5 because it's num[4]. Trying to access the sixth element of the list raises an error because there are only five elements in the list. The second element from the end is printed as 2 because it's num[-5]..
slicing.
[Audio] Slicing allows us to extract a subset of elements from a list. This can be done by specifying a range of indices. For example, if we have a list called numbers, we could use the slice operator to get the first three elements like this: numbers[:3]. This would give us the elements at indices 0, 1, and 2. We could also specify a range of indices using the colon operator. For example, numbers[1:4] would give us the elements at indices 1, 2, and 3..
[Audio] To slice a list, we can use the simple slicing operator, which is denoted by a colon (:). The syntax for this operator is List[start:end:step], where start represents the starting index, end represents the ending index, and step represents the increment or decrement value for the index..
[Audio] The list contains six elements, each being a single character from the alphabet. The first element is the letter "a", followed by "b", then "c", "d", "e", and finally "f". This list is frequently used in programming to illustrate fundamental principles..
[Audio] We are examining a particular method for extracting elements from a list utilizing slicing. This enables us to define a range of indices to retrieve from the list. We will employ this concept to extract a portion of our list, "Numbers," beginning from index 1, extending up to but excluding index 5, and advancing by 2. Let's observe how this functions..
[Audio] We can extract specific parts of this list using the syntax [start:end]. The start value is inclusive, but the end value is exclusive. To include the end value, we need to add one to it. For example, to get the first five elements, we would use [0:5]. We can also get the last three elements using [2:], which gives us the elements starting from index 2 to the end of the list. Additionally, we can get every other element in the list using [::2], which gives us the elements at indices 0, 2, 4, etc..
[Audio] The list [start:] starts with the last element. The list [:end] starts from the first element until the end-1. For example, given the list Numbers = [4, 7, 24, 14, 25, 9], using the list [start:], we get the last element, which is 9. Similarly, using the list [:end], we get the first element, which is 4. With the list [0, 1, 2, 3, 4, 5], the list [start:] gives us the last element, which is 5. The list [:end] gives us the first element, which is 0. When using a negative index like -2, we count from the end of the list. Thus, the list [-2] yields the second last element, which is 4. The list [-1] yields the last element, which is 5. Furthermore, the list [-2] yields the third last element, which is 9..
[Audio] When we move through a list, we can specify the step value. This means we can start from any element and move towards the last element, but only by the specified step. We have two lists here. The first one has values ranging from 4 to 25, and the second one has numbers from 0 to 5. Notice how the step value is 1 in both cases. This allows us to traverse these lists in a specific way. For instance, if we start from the first element of the first list, we'll move to the next element, then the next, and so on, until we reach the last element. Similarly, in the second list, we'll move from 0 to 1, then to 2, and so on, until we reach the last element. The step value helps us control the movement through the list..
[Audio] We can extract specific elements using slicing. The first three elements of the list are obtained using 'list[0:3]', which gives us ['a', 'b', 'c']. The elements from index 3 to the end of the list are obtained using 'list[3:]', which gives us ['d', 'e', 'f']. To get the elements from index 0 to index 3, we use 'list[:4]', which gives us ['a', 'b', 'c', 'd']. Stepping through the list starts at index 1, goes up to index 4, and steps by 2 using 'list[1:5:2]', giving us ['b', 'd']. We can also start at the beginning of the list, go to the end, and step by 2 using 'list[:]', which gives us ['a', 'c', 'e']. Finally, getting the elements in reverse order uses 'list[::-1]', which gives us ['f', 'e', 'd', 'c', 'b', 'a']..
[Audio] The slicing operation on this list involves selecting specific parts of the list. The first slice is lst[1:3], which refers to the second and third elements of the list, namely 50 and 20. The second slice is lst[:-2], which means we're taking everything except the last two elements. This would give us 40, 50, 20, and 30. The third slice is lst[:-2:2], which is a bit more complex. It says take every other element starting from the beginning, excluding the last two elements. This would result in 40, 30. The fourth slice is lst[2:-2], which is similar to the previous one but starts from the third element instead of the beginning. This would also give us 30, 90. Finally, the fifth slice is lst[-2:], which takes the last two elements of the list, namely 90 and 30..
List Functions.
[Audio] Lists can have many functions. The len function gives us the number of items in a list. For example, if we create a list called L1 with the numbers 1 through 7, we can use the len function to find out how many items are in the list. In this case, there are seven items. The sum function adds up all the values in a list. Again, let's use our list L1. When we add up all the numbers from 1 to 7, we get 28..
[Audio] The Python function min returns the smallest value in a list. This can be useful when you need to find the lowest point in a set of data. For instance, if you have a list of exam scores, min could help you identify the student who scored the lowest. Similarly, the max function finds the largest value in a list. In this case, we see that the maximum value in our list is 7, while the minimum value is 1. These functions can be very helpful when working with lists in Python..
[Audio] The code snippet calculates various statistics about a given list of numbers. It defines a list called nums containing several integers, then uses four built-in functions to calculate the length, maximum value, minimum value, and total sum of the list. The length is obtained using the len function, which returns the number of items in the list. The maximum value is found using the max function, which returns the largest item in the list. The minimum value is calculated using the min function, which returns the smallest item in the list. Finally, the total sum is computed using the sum function, which adds up all the items in the list. After calculating these statistics, it divides the total sum by the length to find the average value, performing this calculation using integer division..
[Audio] In this list, we have a collection of elements. Integers and strings are mixed together, which is common in programming where data types can vary within a single list. We need to iterate through this list and perform some operations. A while loop will help us achieve this. As long as the index i is less than the length of the list num, we'll continue executing the code inside the loop. Inside the loop, we access each element in the list using its index. These elements can be used for various tasks, such as calculations or string manipulations. The possibilities are endless..
[Audio] The maximum value in this list is 24. The minimum value is 3. To calculate the average, we first need to add up all the values in the list. This gives us a total of 3 + 5 + 8 + 12 + 24 = 52. There are five numbers in the list, so to get the average, we divide the total by 5. This gives us 52 divided by 5, which is 10.4..
[Audio] When working with lists, there are several special operations we can perform. One common operation is checking whether an item is present in a list. We use the keyword "in" followed by the name of the list, and the item we're looking for. This will return True if the item is found, and False otherwise. Conversely, we can also check if an item is not in a list using the keyword "not in". Another useful operation is concatenating two lists together. We do this by using the "+" operator between the two lists. This creates a new list containing all the elements from both original lists. We can also duplicate a list by multiplying it by an integer. For example, if we have a list called "lst", we can create a new list with twice as many elements by doing "lst * 2". Finally, if we no longer need a list, we can delete it using the "del" statement..
[Audio] In this list, we have six elements. We can check if a specific value exists within the list by using the 'in' keyword. Let's see what happens when we search for the value 6. As expected, since 6 is indeed present in the list, the expression '6 in lis' evaluates to True. Now, let's look for the value 10. Since 10 is not present in the list, the expression '10 in lis' also evaluates to False. We can use these expressions to conditionally execute code based on whether a value is found in the list. For example, we could print a message if the value is found, like so: 'print (A)' or 'print (B)'. The value A would be printed only if 6 is found in the list, while B would be printed if 10 is not found. This demonstrates how we can use conditional statements to manipulate data based on its presence in a list..
[Audio] The list L1 contains the values 4, 6, 12. We can create this list using the assignment operator. L1 equals [4, 6, 12]. Next, we assign the value 14 to another list called L2. L2 equals [14]. Now, we have two separate lists. We concatenate these lists using the plus operator. L1 plus L2 equals [4, 6, 12, 14]. Finally, we print the concatenated list. The result is [4, 6, 12, 14]..
[Audio] Bienvenue sur la diapositive 32 de notre présentation sur les listes. Dans cet exercice, nous allons travailler avec des listes en utilisant le langage Python. Nous allons utiliser deux listes, x et y, pour comprendre comment les listes fonctionnent en Python. Dans la première ligne de code, nous définissons la liste x avec une série de nombres. Ensuite, nous définissons une deuxième liste, y, avec une série de lettres. En utilisant l'opérateur +, nous pouvons fusionner ces deux listes pour obtenir une troisième liste qui contient à la fois les nombres et les lettres. En utilisant l'opérateur *, nous pouvons multiplier la liste y par 3, ce qui va simplement répéter les éléments de la liste 3 fois. De même, en utilisant l'opérateur in, nous pouvons vérifier si un élément est présent dans une liste. Dans cet exemple, nous vérifions si le nombre 5 est présent dans la liste x et le résultat est faux. Nous pouvons également utiliser l'opérateur not in pour vérifier si un élément n'est pas présent dans une liste. Dans ce cas, nous vérifions si la lettre C n'est pas présente dans la liste y et le résultat est également faux. En utilisant l'opérateur +, nous pouvons également concaténer les listes y et x pour obtenir une nouvelle liste avec les lettres suivies des nombres. Cela conclut notre exercice sur les listes en Python. Nous espérons que vous en avez appris davantage sur la manipulation des listes à l'aide de différents opérateurs. Nous allons maintenant passer à la diapositive suivante pour continuer notre exploration des listes en Python..
[Audio] To add new elements to a list, we use the append method, which adds a single element to the end of the list. Alternatively, we can use the extend method to add multiple elements to the list. Additionally, we can use indexing to insert an element at a specific position in the list by specifying its index. For instance, if we want to insert the element 'hello' at the third position, we would use the syntax lst.insert(2,'hello')..
[Audio] The operation on the list "X" before execution is to append the value 8. After execution, the list becomes [2, 4, 1, 5, 8]. Then, the extend method is used to add the list [3,9]. The result is [2, 4, 1, 5, 3, 9]. Next, the insert method is used twice, first inserting 20 at index 1, resulting in [2, 20, 4, 1, 5], then inserting 20 at index 3, resulting in [2, 4, 1, 20, 5]..
[Audio] When working with lists, we often need to remove specific elements. We can do this using the remove function, which removes the first occurrence of a specified value from the list. For example, if we have a list called numbers and we want to remove the value 5, we would use the code numbers.remove(5), which will remove the first instance of 5 in the list. Alternatively, we can use the pop function, which can take one argument, the index of the element we want to remove. If we don't specify an index, it will remove and return the last item in the list. For instance, if we want to remove the last item in the list, we would use the code numbers.pop(). If we want to remove the item at a specific index, such as index 2, we would use the code numbers.pop(2)..
[Audio] When we want to add elements to a list, there's a simple way to do so. We can use the plus sign, or the concatenation operator, to combine our existing list with another list or a single element. We can read in some input from the user, convert that input to a list, and then append it to our original list. The syntax for doing this would be something like this: "lis = lis + [x]" where "x" is the input from the user. This will take the current contents of "lis", add the new elements to it, and store the result back in "lis". With this technique, we can easily build up complex data structures by adding elements one by one..
[Audio] When we modify a list, it's essential to understand how different operations affect its contents. We have a list "X" initially containing the values 2, 4, 1, and 5. Before performing any operation, the list looks like this: [2, 4, 1, 5]. Now, let's remove the value 4 from the list. After executing the removal operation, the list becomes [2, 1, 5]. Notice that the removed value is no longer present in the list. However, if we attempt to remove the value 6, which is not part of the original list, Python raises a ValueError. This is because the value 6 was never present in the list to begin with. Moving on, let's pop an element from the list. Initially, the list remains unchanged: [2, 4, 1, 5]. When we pop an element without specifying an index, Python removes the last element from the list, resulting in [2, 4, 1]. If we were to specify an index, such as 2, the popped element would be the one at that position, leaving us with [2, 4, 5]. These examples demonstrate how various list operations can alter the contents of our list..
[Audio] When we have a list of items, we can use the sort method to arrange them in a specific order. We can sort the list in ascending order by using the lst.sort() method, which means that the smallest item will come first, followed by the next smallest, and so on. Alternatively, if we want to sort the list in descending order, we can use the lst.sort(reverse=True) method, which means that the largest item will come first, followed by the next largest, and so on. By using these methods, we can easily reorder our list to suit our needs..
[Audio] The list initially contains the values 2, 4, 1, and 5. After sorting the list in ascending order, the result is [1, 2, 4, 5]. Then, the list is sorted in descending order, resulting in [5, 4, 2, 1]..
[Audio] Create an empty list called num. We will use the list method append to add the elements 5, 7, and 2 to the list. Next, we will insert the element 50 into the list at index 2. After that, we will sort the list in descending order using the list method sort. Finally, we will display the sum of the list elements using the built-in function sum..
[Audio] The list num has been initialized with the elements 1 through 9. We will extend the list by adding the elements 5, 7, and 2. This can be done using the extend method. The updated list will be printed. Next, we will insert the element 50 at index 2. After that, we will sort the list in reverse order. Finally, we will calculate and display the sum of the elements in the list..
[Audio] Now we write a Python program to solve this exercise. We ask the user how many elements they want to input into our list using the input function. We then create an empty list called numbers. Next, we loop through the range of the user's input, asking them to input each element one by one. We use the append method to add these elements to our list. After that, we calculate the sum of all the list elements using a for loop. To find the minimum value in the list, we use the min function. Finally, we check if the value 10 is present in the list or not using a for loop. Here's the code: numbers = [] n = int(input("Enter the number of elements: ")) for i in range(n): numbers.append(int(input(f"Enter element : "))) sum_of_elements = sum(numbers) minimum_value = min(numbers) print(f"The sum of all the list elements is: ") if 10 in numbers: print("The value 10 is present in the list.") else: print("The value 10 is not present in the list.").
[Audio] Here is how we can create a list with user-defined elements. We ask the user to enter the number of elements they want in their list. Then, we use a loop to ask the user to input each element one by one. We store these elements in a list called L. After that, we calculate the sum of all the elements in the list and find its minimum value. Finally, we check if the value 10 is present in the list or not. If it is, we print a message indicating its presence; otherwise, we print another message stating its absence. We use the following code to achieve this: n = int(input("Enter the number of elements")) L = [] for i in range(n): x = int(input(f"Enter element ")) L.append(x) print(f"The list L is ") print(f"Sum of the List elements is ") print(f"The minimum value in L is ") if 10 in L: print("10 is present in L") else: print("10 is not present in L").
[Audio] The list we are considering today is numbers = [1, 5, 3, 8, 5, 0, 7]. To print this list in reverse order using slicing, we can use the following code: numbers[::-1]. This will give us the list in the reverse order, starting from the last element. Next, let's order the list in descending order. We can do this by converting the list to a tuple, sorting the tuple in descending order, and then converting it back to a list. Here's how we can do it: sorted(numbers, reverse=True). Now, let's find the maximum value in the list. The maximum value is simply the largest number in the list, which is 8. To remove the item with index 5, we can use the del function. The item with index 5 is the number 5, so we'll delete that. Here's how we can do it: del numbers[5]. Next, let's insert the number 4 in index 3. We can do this using the insert function. Here's how we can do it: numbers.insert(3, 4). After that, let's remove the last element in the list. We can do this using the pop function. Here's how we can do it: numbers.pop(). Finally, let's create a new list containing the letters in our name. Let's assume our name is "John", so we'll create a new list called "john_list" and add the letters J-O-H-N to it. Here's how we can do it: john_list = ['J', 'O', 'H', 'N']. And finally, let's concatenate two lists together. Let's concatenate the original list with the new list "john_list". We can do this using the + operator. Here's how we can do it: combined_list = numbers + john_list..
[Audio] Here is how we can create this list. We create an empty list called Numbers. Then we add three numbers to it. We add 3, then 5, and finally 9. Next we add one more number, 25, to the end of the list. Now we insert the number 11 between 5 and 9. After that we remove 5 from the list. The list now looks like this: [3, 9, 11, 25]. To sort this list in descending order we use the sorted function. This gives us the list [25, 11, 9, 3]. Now we find the highest and lowest values in the list. The highest value is 25 and the lowest value is 3. Finally we calculate the sum and average of the list elements. The sum is 38 and the average is 19/2 or 9.5..
[Audio] The code creates a list of numbers entered by the user, asking them to specify the number of elements first, followed by individual input for each element. Once all elements are entered, the program computes the sum and average of these elements, displaying both results as output..
[Audio] The code we need to write will read the list size and then read the values to the list. Then, we'll iterate over the list and check if each value is positive. If it's positive, we'll calculate its square and cube and print them. Here's how we can do it: n = int(input("Enter the size of the list: ")) lst = [] for i in range(n): val = int(input(f"Enter value : ")) lst.append(val) print("Your list has", n) print(lst) for i in range(len(lst)): if lst[i] > 0: print(f"") print(f"The square of is ") print(f"The cube of is ").