Test your Python knowledge with 15 easy and essential Python interview questions. Perfect for beginners preparing for Python interviews and those wanting to strengthen their Python programming basics.
Which of the following is an immutable data type in Python?
Explanation: Tuples in Python are immutable, meaning once created, their elements cannot be changed. Lists and dictionaries are mutable, so their contents can be modified. Sets are mutable as well, which allows elements to be added or removed.
What is the correct way to assign the value 5 to a variable named x in Python?
Explanation: In Python, you assign a value to a variable using the equals sign, as in x = 5. The u003C- operator is not used in Python, int x = 5 is Java or C-style syntax, and x: 5 is invalid in this context.
Why is indentation important in Python?
Explanation: Indentation in Python is used to define code blocks, such as those in functions or loops. While indentation can improve readability, its primary role in Python is syntactical. Indentation does not affect code execution speed or function as comments.
What will be the output of 'Hello' + ' ' + 'World' in Python?
Explanation: Concatenating 'Hello', a space character, and 'World' results in 'Hello World'. 'HelloWorld' lacks the space, 'Hello+World' adds a plus sign, and 'Hello, World' introduces a comma not present in the original strings.
Which symbol is used to write a single-line comment in Python?
Explanation: The hash symbol (#) is used for single-line comments in Python. Double slashes (//) are used in languages like JavaScript or Java, /* and u003C!-- are not used for this purpose in Python.
Which statement is used to iterate over a sequence in Python?
Explanation: The 'for' statement is used to loop over sequences such as lists or strings in Python. Python does not have a 'loop' or 'repeat' keyword, and 'switch' is used in other languages for selection, not iteration.
How do you define a function named hello in Python?
Explanation: Functions in Python are defined using the 'def' keyword, followed by the function name and parentheses. The other options use incorrect keywords or structure not recognized in Python.
What does list(range(3)) return in Python?
Explanation: list(range(3)) gives a list of numbers starting from 0 up to, but not including, 3: [0, 1, 2]. The [1, 2, 3] option starts from 1 and is incorrect, [0, 1, 2, 3] goes one too far, and [1, 2] is too short.
Which function converts a string to an integer in Python?
Explanation: The int() function converts a string to an integer if the string contains numeric characters. float() converts to a floating-point number, str() makes the input a string, and input() is used for user input.
Which of the following uses list comprehension to create a list of squares of numbers from 0 to 4?
Explanation: [x*x for x in range(5)] correctly computes the square of each number using list comprehension in Python. The first uses '^' which performs bitwise XOR, not exponentiation. The third is syntactically incorrect, and the fourth uses an undefined square function and an invalid range.
Which method would you use to get all keys from a Python dictionary named mydict?
Explanation: mydict.keys() returns a view object of all keys in the dictionary. mydict.getKeys() and mydict.allKeys() are not valid methods, while mydict.items() returns both keys and values.
What does the 'None' keyword represent in Python?
Explanation: 'None' is Python’s way of representing the absence of a value, similar to 'null' in other languages. It is not the same as zero or an empty string, and it does not represent False, even though it's considered False in a Boolean context.
Which keywords are used for error handling in Python?
Explanation: try and except blocks are used in Python to handle errors and exceptions. begin and rescue are used in Ruby, do and catch are from other languages, and switch and case are used for selection, not exception handling.
What is the output of bool([]) in Python?
Explanation: An empty list evaluates to False when passed to the bool() function in Python. Non-empty containers evaluate to True, so True is incorrect. Error and None are not produced here.
Which function returns the number of elements in a list named items?
Explanation: The len() function is used in Python to retrieve the number of elements in a list or other container. count(items) is not correct (though count is a list method for counting occurrences of a specific element), and size(items) and length(items) are not built-in Python functions.
How can you specify a default parameter value in a Python function?
Explanation: Default parameter values in Python functions are set by providing parameter=value in the function definition. Assigning value elsewhere or using 'default =' syntax is not correct, and simply omitting the parameter does not set a default.