back

Answer key 1, 2, 3

1. Who created Python?

Python was created by Guido van Rossum.

2. What is Python?

Python is a Programming Language.

3. What is a variable in Python?

A variable in Python is a container for storing data values.

4. Which of the following is the correct statement to print "Hello, World!" in Python?

The correct statement to print "Hello, World!" in Python is print("Hello, World!").

5. Which of the following is a valid variable name in Python?

x is a valid variable name in Python.

6. What symbol is used to comment a single line in Python?

In Python, the symbol # is used to comment a single line.

7. Find the correct answer for this logical operator ( (4>3) and (5>2) )?

The logical expression (4 > 3) and (5 > 2) evaluates to True.

8. What is the output of the following code: print(2 + 3)?

The output of print(2 + 3) is 5.

9. Which operator uses +, -,*, %, /

The operators +, -, *, %, and / are Arithmetic Operators in Python.

10. To make nested if loop, which one is used?

To make nested if loops, if elif else is used.

11. Which of the following is a correct way to start a conditional statement in Python?

The correct way to start a conditional statement in Python is with the syntax if (x > y):.

12. What will be the output of the following code: `if 3 > 2: print("True") else: print("False")`?

The output of the code if 3 > 2: print("True") else: print("False") will be False.

13. Which operator is used to compare two values?

The operator used to compare two values in Python is ==.

14. How do you create a list in Python?

To create a list in Python, you use the syntax myList = [1, 2, 3].

15. How do you access the first element of a list `myList` in Python?

To access the first element of a list in Python, you use the syntax myList[0].

16. What will be the output of `len([1, 2, 3, 4])`?

The output of len([1, 2, 3, 4]) is 4.

17. Which method is used to add an element to a set in Python?

The method used to add an element to a set in Python is add().

18. Which of the following collections allows duplicate elements?

The collection that allows duplicate elements in Python is List.

19. What is the result of `5 % 2`?

The result of 5 % 2 is 1.

20. Which of the following is used to create a dictionary in Python?

To create a dictionary in Python, you use {}.

21. What will be the output of the following code?

def mystery_function(n):
    if n == 0:
        return 0
    return n + mystery_function(n-1)

print(mystery_function(4))
The function `mystery_function` is a recursive function that calculates the sum of all numbers from 1 to n. In this case, `mystery_function(4)` returns 4 + 3 + 2 + 1 = 10.

22. What does the `enumerate()` function do in Python?

The `enumerate()` function in Python adds a counter to an iterable and returns it as an enumerate object. Each element in the iterable is paired with its corresponding index.

23. What is the purpose of the `zip()` function in Python?

The `zip()` function takes iterables (like lists, tuples, etc.) and returns an iterator of tuples where the i-th tuple contains the i-th element from each of the input iterables.

24. How do you remove an item from a list in Python?

To remove an item from a list in Python, you can use the `pop()` method, which removes and returns the element at the specified index.

25. What is the output of the following code?

my_dict = {"a": 1, "b": 2, "c": 3}
result = sum(my_dict.values())
print(result)
The `sum()` function in Python returns the sum of all the values in a dictionary. In this case, it returns 1 + 2 + 3 = 6.

26. How do you check if a key exists in a dictionary?

To check if a key exists in a dictionary in Python, you can use the `in` keyword. It returns True if the key exists, otherwise False.

27. What does the `range()` function in Python do?

The `range()` function in Python returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and stops before a specified number.

28. How do you convert a string to uppercase in Python?

To convert a string to uppercase in Python, you can use the `upper()` method, which returns a copy of the string converted to uppercase.

29. What will be the output of the following code?

my_list = [1, 2, 3]
my_list[1] = 4
print(my_list)
The code modifies the second element of the list `my_list` to 4. Therefore, the output is `[1, 4, 3]`.

30. What does the `split()` method do in Python?

The `split()` method in Python is used to split a string into a list of substrings based on a delimiter. In this case, it splits the string `"Hello World"` into two elements: `"Hello"` and `"World"`, resulting in a list of length 2.