Most targeted up-to-date Python interview questions and answers list
- How do you print "Hello, World!" in Python?
- How do you declare and initialize a variable in Python?
- How do you define a function in Python?
- How do you create a class in Python?
- How do you iterate through a list in Python?
- How do you handle exceptions in Python?
- How do you read from a file in Python?
- How do you write to a file in Python?
- How do you work with dictionaries in Python?
- How do you sort a list in Python?
- How do you work with modules in Python?
- How do you handle JSON data in Python?
Answer:
print("Hello, World!")
Answer:
name = "John Doe"
age = 25
Answer:
def greet():
print("Hello!")
Answer:
class MyClass:
def __init__(self, name):
self.name = name
def greet(self):
print(f"Hello, {self.name}!")
Answer:
my_list = [1, 2, 3, 4, 5]
for item in my_list:
print(item)
Answer:
try:
# Code that may raise an exception
result = 10 / 0
except ZeroDivisionError:
print("Cannot divide by zero!")
Answer:
with open("file.txt", "r") as file:
content = file.read()
print(content)
Answer:
with open("file.txt", "w") as file:
file.write("Hello, World!")
Answer:
my_dict = {"name": "John", "age": 25}
# Accessing values
print(my_dict["name"])
# Iterating through key-value pairs
for key, value in my_dict.items():
print(f"{key}: {value}")
Answer:
my_list = [3, 1, 4, 2, 5]
sorted_list = sorted(my_list)
print(sorted_list)
Answer:
# Importing a module
import math
# Using functions from the module
square_root = math.sqrt(25)
print(square_root)
Answer:
import json
# Converting JSON to Python object
json_data = '{"name": "John", "age": 25}'
python_data = json.loads(json_data)
print(python_data)
# Converting Python object to JSON
python_data = {"name": "John", "age": 25}
json_data = json.dumps(python_data)
print(json_data)
A comprehensive list of questions and answers
We welcome contributions from our users to help make this resource as comprehensive and useful as possible. If you have been recently interviewed and encountered a question that is not currently covered on our website, feel free to suggest it as a new question. Your contributions will be added to our platform, and we will make sure to credit you for your contributions. We appreciate your help in making our platform a valuable tool for all job seekers.
MIT License