Here are some basic Python interview questions for beginners, along with their brief explanations. These questions cover key concepts in Python programming and will help you prepare for interviews:
1. What is Python?
- Answer: Python is a high-level, interpreted, general-purpose programming language. It is known for its readability, simplicity, and versatility. It supports multiple programming paradigms, including procedural, object-oriented, and functional programming.
2. What are the key features of Python?
- Answer:
- Easy to Learn and Use: Simple syntax that is easy to read and understand.
- Interpreted Language: Code is executed line by line.
- Dynamically Typed: Variable types are inferred at runtime, no need to declare them explicitly.
- Extensive Standard Library: Provides built-in modules and functions to perform various tasks.
- Cross-platform: Python runs on multiple operating systems without modification.
3. What is a variable in Python?
- Answer: A variable in Python is a name that refers to a memory location where data is stored. Python variables do not require an explicit declaration to reserve memory space. The variable type is determined automatically based on the assigned value.
4. What are the different data types in Python?
- Answer:
- Numeric Types:
int
,float
,complex
- Sequence Types:
list
,tuple
,range
- Text Type:
str
- Mapping Type:
dict
- Set Types:
set
,frozenset
- Boolean Type:
bool
- Binary Types:
bytes
,bytearray
,memoryview
- Numeric Types:
5. What is a list in Python?
- Answer: A list is a mutable, ordered collection of items in Python. Lists can contain elements of different data types, including other lists.
Example:
my_list = [1, 2, 3, "apple"]
6. What is the difference between a list and a tuple?
- Answer:
- List: Mutable (can be changed), defined with square brackets
[]
. - Tuple: Immutable (cannot be changed), defined with parentheses
()
.
- List: Mutable (can be changed), defined with square brackets
Example:
my_list = [1, 2, 3]
my_tuple = (1, 2, 3)
7. What is a dictionary in Python?
- Answer: A dictionary is an unordered collection of key-value pairs. Keys are unique, and values can be any data type.
Example:
my_dict = {"name": "John", "age": 25}
8. What is a function in Python?
- Answer: A function is a block of reusable code that performs a specific task. Functions are defined using the
def
keyword.
Example:
def greet(name):
return f"Hello, {name}!"
9. What is the difference between break
, continue
, and pass
?
- Answer:
break
: Terminates the current loop and moves control to the next statement.continue
: Skips the current iteration of the loop and moves to the next iteration.pass
: A placeholder used when no action is required in a loop or conditional statement (i.e., a "no-op").
10. What are loops in Python?
- Answer: Loops are used to execute a block of code repeatedly. There are two main types:
for
loop: Iterates over a sequence (e.g., a list or range).while
loop: Repeats as long as a condition is true.
Example:
for i in range(5):
print(i)
11. What are conditional statements in Python?
- Answer: Conditional statements allow you to execute different blocks of code based on conditions.
if
: Executes a block of code if the condition is true.elif
: Checks additional conditions if the previousif
condition is false.else
: Executes if none of the above conditions are true.
Example:
if age > 18:
print("Adult")
else:
print("Minor")
12. What is a class in Python?
- Answer: A class is a blueprint for creating objects (instances). It defines attributes (variables) and methods (functions) that objects of the class will have.
Example:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def greet(self):
return f"Hello, my name is {self.name}!"
13. What is inheritance in Python?
- Answer: Inheritance allows one class (child class) to inherit attributes and methods from another class (parent class), promoting code reuse.
Example:
class Animal:
def speak(self):
return "Animal speaking"
class Dog(Animal):
def speak(self):
return "Bark"
14. What are Python’s built-in data structures?
- Answer: Python provides several built-in data structures such as:
- List: An ordered collection of items.
- Tuple: An immutable ordered collection.
- Dictionary: An unordered collection of key-value pairs.
- Set: An unordered collection of unique items.
15. What is the difference between is
and ==
in Python?
- Answer:
==
: Checks if the values of two variables are equal.is
: Checks if two variables refer to the same object in memory.
16. What is a lambda function in Python?
- Answer: A lambda function is an anonymous function defined using the
lambda
keyword. It can take any number of arguments but can only have one expression.
Example:
add = lambda x, y: x + y
print(add(3, 4)) # Output: 7
17. What is exception handling in Python?
- Answer: Exception handling allows you to handle errors or exceptions gracefully using the
try
,except
,else
, andfinally
blocks.
Example:
try:
num = int(input("Enter a number: "))
except ValueError:
print("Invalid input!")
18. What are modules in Python?
- Answer: A module is a file containing Python definitions and statements. You can import and use the functions, classes, and variables defined in a module.
Example:
import math
print(math.sqrt(16)) # Output: 4.0
19. What is the difference between del
and remove()
in Python?
- Answer:
del
: Deletes a variable or an item from a list by its index.remove()
: Removes the first occurrence of a specified value from a list.
20. What are Python decorators?
- Answer: Decorators are functions that modify or enhance the behavior of other functions or methods. They are commonly used for logging, access control, caching, etc.
Example:
def decorator(func):
def wrapper():
print("Before function call")
func()
print("After function call")
return wrapper
@decorator
def say_hello():
print("Hello!")
say_hello()