What will the following code print?
my_set = {1, 2, 3}
my_set.add(3)
print(my_set)
Explanation:
Sets in Python are unordered collections of unique elements. Adding an element that already exists in the set has no effect. Therefore, the output will remain {1, 2, 3}
.
What is the purpose of the break
statement in a loop?
Explanation:
When encountered inside a loop, the break
statement immediately terminates the loop's execution and continues with the code after the loop.
Which method is used to convert a string to uppercase in Python?
Explanation:
The upper()
method is used to create an uppercase copy of a string in Python. The other options are not valid methods.
What will the following Python code snippet print?
def greet(name):
print(f"Hello, {name}!")
greet("Alice")
Explanation:
The code defines a function greet
that takes a name
as input and prints a greeting. When called with "Alice", it substitutes "Alice" for name
in the print statement.
What is the output of the following Python code snippet?
print(10 / 3)
Explanation:
In Python, the '/' operator performs floating-point division, resulting in a floating-point number even if both operands are integers.
Which comparison operator is used to check if two values are not equal?
Explanation:
The '!=' operator checks for inequality between two values. If the values are not equal, it returns True; otherwise, it returns False.
Which loop is best suited for iterating over a sequence of elements like a list?
Explanation:
While both for
and while
loops can iterate over lists, for
loops are generally preferred for their conciseness and automatic iteration over each element.
What is the correct way to print 'Hello, World!' in Python?
Explanation:
Python uses the print()
function to display output. The text to be printed is enclosed in parentheses and single or double quotes.
What is the correct way to take integer input from the user in Python?
Explanation:
The 'input()' function returns a string, so we need to convert it to an integer using 'int()' to store it as a numerical value.
What will the following code snippet print?
x = 5
y = 2 * x
print(y)
Explanation:
The code assigns 5 to 'x', then calculates 'y' as 2 times 'x' (which is 10), and finally prints the value of 'y'.
What is the output of the following Python code?
my_list = [1, 2, 3, 4, 5]
print(my_list[1:3])
Explanation:
In Python slicing, the starting index is inclusive, and the ending index is exclusive. Therefore, my_list[1:3]
will return a new list containing elements from index 1 (the second element) up to, but not including, index 3.
What is the output of the following Python code snippet?
print(type(5.0))
Explanation:
The type()
function in Python returns the data type of a given value. In this case, 5.0
is a floating-point number, hence the output is <class 'float'>
.
What is a key limitation of lambda functions in Python compared to regular functions defined with def
?
Explanation:
Lambda functions are designed for concise, single-expression operations. While they can technically contain multiple expressions using tricks, they are best suited for simple logic that can be expressed in one line.
What will the following code print: print(10 > 5 and 'apple' == 'orange')
?
Explanation:
The expression uses the and
operator. While 10 > 5
is True, 'apple' == 'orange'
is False. For 'and' to return True, both conditions must be True.
What is the correct way to access the third element of a list named 'my_list' in Python?
Explanation:
In Python, list indices start from 0. So, to access the third element, you would use index 2.