Good Luck! Congratulation. Advanced Python Quiz 1 / 45 How do you shuffle a list of items in Python? my_list = [1, 2, 3, 4, 5] What is the best way to shuffle above list ? None of the above my_list.shuffle() random.shuffle(my_list) random.shuffleList(my_list) import random my_list = [1, 2, 3, 4, 5] random.shuffle(my_list) 2 / 45 What will be the output of the following Python code? 5 1 10 15 3 / 45 What is the output? 0 4 16 0 1 4 9 16 0 4 9 16 0 16 4 / 45 What is the output ? my_list = [1,2,3,4,5,6] for i in range(len(my_list)):     my_list[i-1] = my_list[i] print(my_list) [6,5,4,3,2,1] [2,3,4,5,1,1] Error [1,2,3,4,5,6] Step-by-step breakdown: Initialization: my_list = [1, 2, 3, 4, 5, 6] Loop Iteration (i = 0): my_list[0-1] is equivalent to my_list[-1], which is the last element (6). my_list[0] is the first element (1). my_list[-1] = my_list[0] assigns the value of the first element (1) to the last element. my_list becomes [1, 2, 3, 4, 5, 1]. Loop Iteration (i = 1): my_list[1-1] is my_list[0] (1). my_list[1] is the second element (2). my_list[0] = my_list[1] assigns the value of the second element (2) to the first element. my_list becomes [2, 2, 3, 4, 5, 1]. Loop Iteration (i = 2): my_list[2-1] is my_list[1] (2). my_list[2] is the third element (3). my_list[1] = my_list[2] assigns the value of the third element (3) to the second element. my_list becomes [2, 3, 3, 4, 5, 1]. Loop Iteration (i = 3): my_list[3-1] is my_list[2] (3). my_list[3] is the fourth element (4). my_list[2] = my_list[3] assigns the value of the fourth element (4) to the third element. my_list becomes [2, 3, 4, 4, 5, 1]. Loop Iteration (i = 4): my_list[4-1] is my_list[3] (4). my_list[4] is the fifth element (5). my_list[3] = my_list[4] assigns the value of the fifth element (5) to the fourth element. my_list becomes [2, 3, 4, 5, 5, 1]. Loop Iteration (i = 5): my_list[5-1] is my_list[4] (5). my_list[5] is the sixth element (1). my_list[4] = my_list[5] assigns the value of the sixth element (1) to the fifth element. my_list becomes [2, 3, 4, 5, 1, 1] 5 / 45 What is the output? hello ['hello'] [] hello world The re.findall() function searches for all non-overlapping occurrences of a pattern in a string and returns them as a list. In this case, the pattern 'hello world' is being searched for within the string 'hello'. Since 'hello world' is not found within 'hello', the function returns an empty list [] 6 / 45 What will be the output of the following code? [1, 2, 3, 4] [5, 4, 3, 2, 1] [4, 3, 2, 1] [5] The expression my_list[:-1] uses list slicing. The colon : indicates a slice operation, and -1 specifies the end index of the slice. When the end index is negative, it counts from the end of the list. In this case, -1 refers to the last element. The slice [:-1] extracts all elements from the beginning of the list up to, but not including, the last element. 7 / 45 What is the output? ['Hello', 'World!', 'Celebrate', 'Freedom'] Hello World! Celebrate Freedom HelloWorld!CelebrateFreedom ['Hello', 'World', 'Celebrate', 'Freedom'] The code initializes a string variable my_string with the value "Hello World! Celebrate Freedom". Then, it splits the string into a list of words using the space character as a delimiter. Finally, it prints the resulting list. 8 / 45 What is the output? Sir Sir Don Don SirDon Here's how the provided Python code works and what it outputs: 1. some_function() Definition: def some_function(): defines a regular Python function called some_function. title = 'Sir' creates a local variable title within some_function and assigns it the string value 'Sir'. name = (lambda x: title + '' + x) defines a lambda function (an anonymous function) and assigns it to the variable name. This lambda function takes one argument, x. It uses title, which is defined in the enclosing function's scope (some_function), and concatenates it with an empty string and the lambda's argument x. return name returns the lambda function itself to the caller. 2. myvar = some_function(): This line calls some_function(). The function returns the lambda function (assigned to name inside some_function). This returned lambda function is then assigned to the variable myvar. 3. print(myvar('Don')): myvar('Don') calls the lambda function that was returned by some_function and assigned to myvar. The argument 'Don' is passed to the lambda function as x. The lambda function executes: title + '' + x which becomes 'Sir' + '' + 'Don', resulting in the string 'SirDon'. 9 / 45 What will following code print? Error None ['Bob', 'Sam', 'Chris', 'Dave'] ['Dave', 'Chris', 'Sam', 'Bob'] The code initializes a list called my_list with four string elements. Then, my_list2 is assigned to my_list, which means they both reference the same list in memory. Next, my_list3 is assigned a slice of my_list2 using the [:] notation. This creates a shallow copy of my_list2, meaning my_list3 is a new list with the same elements as my_list2, but it's a separate object in memory. Finally, the code prints my_list3, which outputs the elements of the new list. 10 / 45 What will below code output ? Error 12 15 1, 2, 3, 4, 5] The code my_list = [1, 2, 3, 4, 5] creates a list named my_list containing the numbers 1 through 5. The sum(my_list) function calculates the sum of all elements in the list, which is 15. However, this result is not stored or used in any further operations. Finally, print(my_list) displays the original list, which remains unchanged. Therefore, the output is: [1, 2, 3, 4, 5]. 11 / 45 What is the output of following code? [2,2,2,2,2] [10,10,10,10,10] Error [10] In Python, the code my_list = [2] * 5 uses the list repetition operator (*) to create a new list by repeating a given list a specified number of times. 12 / 45 What is the output? my_list = [1,2,1,2,3,4,5,1] print(my_list.count(1)) 4 3 1 2 Number of 1's are 3 in the list. 13 / 45 # What is the output of the following code? print(r"\nhello") letter r and then hello nhello a new line and hello Error In Python, r"..." represents a raw string literal. This means that the backslash character (\) is treated as a literal character and not as an escape character. 14 / 45 What will be the output of below code? True False [2,11,23] Error The comparison starts with the first elements (11 and 11), which are equal. Then, the second elements (2 and 2) are compared, which are also equal. When the third elements are compared, 23 from my_list1 is greater than 2 from my_list2. Therefore, the condition my_list1 < my_list2 is evaluated to False. 15 / 45 What is the output of the following Python code ? [2] Error [2, 4, 6] [1, 2, 3, 4, 5, 6] 16 / 45 What is the output? 3 1 2 4 The provided Python code initializes a list my_list with the values [6, 7, 1, 8, 2]. It then iterates through the list, comparing each element to the current max value. If an element is greater than max, it updates max to that element and stores the element's index in the index variable. Finally, it prints the value of index. In this specific case, the code will find that the maximum value in the list is 8, which is located at index 3. Therefore, the code will print 3. 17 / 45 What is the output of the following code? print(0xA + 0xB + 0xC) 33 0x22 0xDx0xEx0xF Error In the context of programming, 0xA (or 0x0A) represents the hexadecimal value 10, which corresponds to the line feed character (LF) in the ASCII character set. 0xB is equivalent to the decimal number 11. 0xC is equivalent to the decimal number 12. Sum of 10+11+12 = 33 18 / 45 Lamda is a statement? False True Lambda functions in Python are expressions, not statements. This means they return a function object and can be used wherever expressions are allowed, such as in function calls or assignments. Lambda functions can evaluate to True or False based on the expression they contain. 19 / 45 # What is the output of the following code? print("hello"[2:]) lo he llo hel In Python, [2:] is a slicing notation used to extract a portion of a sequence (like a string, list, or tuple). It means "start at index 2 and go to the end of the sequence". "Hello"[2:] is a string slicing operation in Python. It extracts a substring from the string "Hello" starting from the character at index 2 (inclusive) up to the end of the string. 20 / 45 What is the output? 2 Error 4 5 What does this code do? 1. Initialization: my_list = [1, 2, 3] creates a list named my_list with initial elements 1, 2, and 3. 2. Extension: my_list.extend([4, 5, 6]) adds the elements of the list [4, 5, 6] to the end of my_list, resulting in my_list becoming [1, 2, 3, 4, 5, 6]. 3. Reversal: my_list.reverse() reverses the order of elements in my_list in place. After this operation, my_list is [6, 5, 4, 3, 2, 1]. 4. Access and Print: print(my_list[2]) accesses the element at index 2 of the reversed list, which is the third element (0-indexed). The value at index 2 is 4, so it will be printed to the console. 1. Initialization: my_list = [1, 2, 3] creates a list named my_list with initial elements 1, 2, and 3. 2. Extension: my_list.extend([4, 5, 6]) adds the elements of the list [4, 5, 6] to the end of my_list, resulting in my_list becoming [1, 2, 3, 4, 5, 6]. 3. Reversal: my_list.reverse() reverses the order of elements in my_list in place. After this operation, my_list is [6, 5, 4, 3, 2, 1]. 4. Access and Print: print(my_list[2]) accesses the element at index 2 of the reversed list, which is the third element (0-indexed). The value at index 2 is 4, so it will be printed to the console. 21 / 45 What is the output ? [6, 7, 1, 8, 2] [0] [0, 7, 1, 8, 2] 0 The code will output [0, 7, 1, 8, 2]. The line my_list = my_list is essentially a no-op, as it assigns the list to itself. The important part is my_list[0] = 0, which changes the first element of the list (at index 0) to 0. Therefore, the final list will be [0, 7, 1, 8, 2]. The print(my_list 2) statement will then output the list. Python lists are mutable, meaning their contents can be changed after creation. 22 / 45 What is the output of the following Python code ? False True 10 5 Step by step: x > y = False, x and y = 10 (truthy value), so False or 10 = 10. The trick: Python's 'or' returns the first truthy value, not just True/False! This catches many beginners off-guard. 23 / 45 What is the output ? [] Error [1, 2, 3, 4, 5] [2, 3, 4, 5, 6] The pop() method, when called without an index, removes and returns the last element of the list. In this case, the last element, 6, is removed from my_list. The print() statement then displays the modified list. 24 / 45 What will be the output? ['Hello', 'World!', 'Celebrate', 'Freedom'] Error ['Hello', 'World', 'Celebrate', 'Freedom'] Hello World! Celebrate Freedom This Python code assigns the string "Hello World! Celebrate Freedom" to the variable my_string and then prints the value of my_string to the console. In Python, strings are immutable. This means that once a string is created, its value cannot be changed. Any operation that appears to modify a string actually creates a new string object. 25 / 45 Which of the following will create a list? mylist = list() All the above mylist = list([1,2,3]) mylist = [] 1. Using Square Brackets This is the most straightforward method. You enclose a comma-separated sequence of items within square brackets []. The list() constructor can be used to create a list from an iterable (e.g., a tuple, string, set, or range). List comprehensions provide a concise way to create lists based on existing iterables. 26 / 45 What is the output? 9 4 2 9 3 1 9 8 7 6 5 9 4 2 1 27 / 45 What is the output? Error 2673 False 10000 The provided code defines a lambda function named myfunc that takes two arguments, x and y. It returns the smaller of the two arguments. The code then calls myfunc with the arguments 100 * 100 (which is 10000) and 27 * 99 (which is 2673). The function evaluates these expressions and returns the smaller value, which is 2673. 28 / 45 What will below code print? Error 12345 5 4 max() is a built-in Python function that finds the largest value in an iterable, such as a list, tuple, or set. The correct syntax to call max() with a list is max(list_name). 29 / 45 What is the output? [('banana', 1)] [('apple', 3), ('banana', 1), ('cherry', 2)] Error [('banana', 1), ('cherry', 2), ('apple', 3)] 30 / 45 What will the below code print? ['Alice', 'Sam', 'Chris', 'Dave'] ['Bob','Sam','Chris','Dave'] ['Bob', 'Bob', 'Chris', 'Dave'] Error Here's a breakdown of the Python code and why it produces the output it does: my_list = ['Bob','Sam','Chris','Dave']: This line creates a list named my_list containing four strings. my_list2 = my_list: This line doesn't create a new list. Instead, it makes my_list2 refer to the same list object in memory as my_list. Think of it like giving the same list two different names. So, any changes made to my_list2 will also affect my_list. my_list3 = my_list2[:]: This line uses list slicing with an empty slice [:]. This is a common Python trick to create a shallow copy of a list. A shallow copy means my_list3 is a new list, but the elements within it (the strings 'Bob', 'Sam', etc.) are still the same objects as in the original list. my_list2[0] = 'Alice': This line modifies the first element of my_list2 (which is also the first element of my_list) to 'Alice'. Since my_list2 and my_list refer to the same list, both will now start with 'Alice'. my_list3[1] = 'Bob': This line modifies the second element of my_list3 to 'Bob'. Crucially, because my_list3 is a copy created by slicing, this change only affects my_list3, not my_list or my_list2. print(my_list3): This line prints the contents of my_list3. 31 / 45 Which of the following command will insert number 6 in the third position of my_list = [1, 2, 3, 4, 5] ? my_list.insert(3,6) my_list.append(6,3) my_list.insert(6,3) my_list.insert(3,6) The Python code my_list.insert(3, 6) will insert the integer 6 into the list my_list at index 3, shifting the existing element at index 3 (which was 4) and all subsequent elements to the right by one position. The resulting list will be [1, 2, 3, 6, 4, 5]. 32 / 45 What will be the output? 100 23 2 Error List does not have 3, so will return Error. 33 / 45 What is the output? ['a b c'] Error ['a', 'b', 'c'] ['abc'] The split() method is used to split a string into a list of substrings based on a specified delimiter. In this case, the delimiter is '#'. The string "a#b#c" is split at each occurrence of '#', resulting in a list of three strings: 'a', 'b', and 'c'. The print() function then displays this list. 34 / 45 What is the output of following code? [2, 3, 4, 5] [2, 3, 4] [1, 2, 3] [1, 5] [1:4]: This is the slice notation. 1: This is the starting index (inclusive). It indicates the first element you want to include in the new list is at index 1. 4: This is the stopping index (exclusive). It indicates that the slice should end before the element at index 4. 35 / 45 What is the output? Error 1 0 False 36 / 45 What will below code print? 22 21 12 11 Tricky Question, here are three lists looks like: ['Alice', 'Sam', 'Chris', 'Dave'] ['Alice', 'Sam', 'Chris', 'Dave'] ['Bob', 'Bob', 'Chris', 'Dave'] As you can see Alice is in 2 list and Bob 1, the answer will be 12. Confusing part could be 11 as how my_list original changed ? As this assignment, my_list2[0] = 'Alice', changes my_list2 but also my_list1 as they point to same memory location. While my_list3 is completely new list at its own location. 37 / 45 From below my_list, how do you remove "Hello"? my_list = ["Hello", "World"] my_list.removeOne("Hello") remove("Hello") my_list.remove("Hello") remove(my_list["Hello"]) The code initializes a list named my_list with two string elements: "Hello" and "World". The remove() method is then used to remove the first occurrence of the element "Hello" from the list. Finally, the modified list is printed to the console. The output will be ['World'] because the remove() method modifies the original list in place. 38 / 45 What is the output? [2, 3, 4, 5, 6] [1, 3, 4, 5, 6] Error [1, 2, 3, 4, 5] Here's how it works: my_list = [1, 2, 3, 4, 5, 6] : This line initializes a list named my_list with the elements 1, 2, 3, 4, 5, and 6. my_list.pop(1): This calls the pop() method on my_list. The pop() method removes and returns an element from a list. When you provide an index as an argument, as in pop(1), it removes the element at that specified index (index 1 in this case, which is the second element since lists are zero-indexed). print(my_list): This line prints the updated my_list to the console. 39 / 45 What is the output? 0 2 1 3 2 3 0 1 2 3 40 / 45 What will following code print? my_list = [1, 2, 3]*2 print(my_list) Error [1, 2, 3, 1, 2, 3] [2, 4, 6] [1, 2, 3] The code initializes a list named my_list by repeating the list [1, 2, 3] twice, resulting in [1, 2, 3, 1, 2, 3]. Then, the print() function displays the contents of my_list on the console. 41 / 45 What is the output of the following ? 0,1,2,3,4 [0, 1, 2, 3, 4] 1,2,3,4,5 Error for i in range(5): is a for loop in Python that iterates five times. The range(5) function generates a sequence of numbers from 0 up to (but not including) 5. In each iteration, the variable i takes on the next value from this sequence. Therefore, i will take the values 0, 1, 2, 3, and 4, in that order. 42 / 45 What is the output? ['Hello', 'hello', 'hello', ''] ['Hello', 'hello', 'hello'] ['Hello', 'hello', 'hello', '.'] ['Hello', 'hello', 'hello.'] Here's how it works: re.split(r'\W+', 'Hello, hello, hello.'): This line splits the string 'Hello, hello, hello.' using the regular expression pattern \W+. \W: This matches any non-alphanumeric character (i.e., anything that is not a letter, number, or underscore). +: This means "one or more occurrences" of the preceding character or pattern. Therefore, \W+ matches one or more non-alphanumeric characters. In the input string, this would match the comma and space after each "hello" and the period at the end. The re.split() function returns a list of strings, where each string is a substring from the original string that was separated by the matched pattern. 43 / 45 # What is the output of the following code? my_string = "HelloWorld" print(my_string[::-1]) Hello HelloWorld dlroWolleH World [::-1] will reverse the string in Python. 44 / 45 What is the output? 9 Error 3 27 The code defines a lambda function named myfunc that takes one argument x and returns x raised to the power of 3. The code then calls this function with the argument 3 and prints the result, 27, this is because 3 raised to the power of 3 is 27. 45 / 45 What is the output of the following code? my_list = ['h','e','l','l','o'] print(len(my_list)) 5 4 6 Error The code initializes a list named my_list with the elements 'h', 'e', 'l', 'l', and 'o'. The len() function is then used to determine the number of elements in my_list, which is 5. The output of the code will be the integer 5. Your score isThe average score is 42% 0% Restart quiz