Python Question / Quiz; What is the output of the following Python code, and why? 🤔🚀 Comment your answers below! 👇 #python #programming #developer #morioh #programmer #coding #coder #webdeveloper #webdevelopment #pythonprogramming #pythonquiz #machinelearning
@Python_Dv Here extend will add 2 elements So the answer is 5 elements
@Python_Dv There is answer is 5 a starts as [1, 2, 3]. b is [4, 5]. a.extend(b) appends elements from b to a, making a equal to [1, 2, 3, 4, 5]. print(len(a)) prints the length of a, which is 5.
Here’s what each line does: 1. `a = [1, 2, 3]` initializes list `a` with three elements: 1, 2, and 3. 2. `b = [4, 5]` initializes list `b` with two elements: 4 and 5. 3. `a.extend(b)` extends list `a` by appending all elements from list `b` to it. After this operation, list `a` becomes `[1, 2, 3, 4, 5]`. 4. `print(len(a))` prints the length of list `a`, which now contains 5 elements. Therefore, the output of the code is `5`. This corresponds to option `c) 5`.
@Python_Dv a = [1, 2 ,3] # a <- [1, 2 ,3] b = [4, 5] # b <- [4, 5] a.extend(b) # a <- [1, 2, 3, 4, 5] len(a) # 5 <- len(a) Answer: c) 5
@Python_Dv a.extend(b) The extend method is used to add elements from one list (b) to the end of another list (a). print(len(a)) 'a'. len() function returns the number of items in an object, Given input: 1. a=[1,2,3] 2. b=[4,5] 3. a.extend(b) 4. print(len(a)) The correct answer is: b) 4
@Python_Dv C. 5 is the output. Explanation: 1. The extend() method appends the elements of list b to list a. 2. After extending, a becomes [1, 2, 3, 4, 5]. 3. The len(a) returns the length of the modified list, which is 5.
@Python_Dv # c) 5 Reason: Line 3: The extend() method adds the specified list elements to the end of the current list. Line 4: The len() function returns the number of items in list - a = [1, 2, 3, 4, 5]. Output: 5
@Python_Dv answer "b" will be when you use insert against extend
@Python_Dv 5. After a.extend(b) => a = [1,2,3,4,5] => len(a) => 5