Exercise: Data Types

Questions for: Data Types

What does the min() function in Python do?
A:
Returns the minimum value in a list or tuple
B:
Rounds a floating-point number down to the nearest integer
C:
Converts a string to lowercase
D:
Checks if a variable is of a certain type
Answer: A

The min() function is used to find the smallest value in a list or tuple.

For example, if you have the following list:

list = [1, 2, 3, 4, 5]

You can use the min() function to find the smallest element in the list, which would be 1.

The min() function can be useful for a variety of tasks, such as finding the smallest value in a dataset, or finding the smallest number of items in a stock.

What is the output of the following code snippet?
my_tuple = (1, 2, 3, 4, 5)
print(my_tuple[-2:])
A:
(4, 5)
B:
(2, 3, 4, 5)
C:
(3, 4, 5)
D:
(2, 3)
Answer: A

my_tuple[-2:] slices the tuple my_tuple starting from the second-to-last element (index -2) to the end of the tuple.

So, it selects elements at indices -2 and -1, which are 4 and 5 respectively, resulting in the tuple (4, 5).

What will be the output of the following code snippet?
my_string = "Python"
print(my_string[1:5:2])
A:
yhn
B:
yh
C:
yhon
D:
yto
Answer: B

The slicing syntax [start:stop:step] is used, selecting characters from index 1 to 5 with a step of 2.

my_string[1:5:2] slices the string my_string starting from index 1 (inclusive) up to index 5 (exclusive), with a step size of 2.

So, it selects characters at indices 1 and 3 (1+step-size) (skipping index 5), which are 'y' and 'h' respectively.

Which of the following statements is true about tuples?
A:
Tuples are mutable
B:
Tuples can not have duplicate elements
C:
Elements in a tuple can be accessed using indices
D:
Tuples are created using square brackets
Answer: C
Tuples in Python are ordered and immutable. Elements can be accessed using indices.
What will be the output of the following code snippet?
x = 10
y = "5"
result = x + int(y)
print(result)
A:
15
B:
105
C:
"10" + "5"
D:
Error
Answer: A
The int(y) converts the string '5' to an integer, and then the addition results in 10 + 5 = 15.
Ad Slot (Above Pagination)
Quiz