1. What is Python?
Answer: Python is a high-level, interpreted programming language known for its readability and
simplicity. It supports multiple programming paradigms, including procedural, object-oriented,
and functional programming.
2. What are the key features of Python?
Answer: Some key features include:
Easy to read and write syntax
Extensive standard library
Dynamic typing
Interpreted language
Support for multiple programming paradigms
Strong community support
3. What is PEP 8?
Answer: PEP 8 is the Python Enhancement Proposal that provides guidelines and best practices on
how to write Python code. It covers topics like naming conventions, code layout, and
documentation.
4. How do you create a list in Python?
Answer: You can create a list by enclosing elements in square brackets. For example: my_list = [1,
2, 3, 4].
5. What is the difference between a list and a tuple?
Answer: The main difference is that lists are mutable (can be changed) while tuples are immutable
(cannot be changed). Lists use square brackets [], while tuples use parentheses ().
6. How do you handle exceptions in Python?
Answer: You can handle exceptions using the try and except blocks. For example:
python
RunCopy code
1try:
2 # code that may raise an exception
3except SomeException:
4 # code to handle the exception
7. What are functions in Python?
Answer: Functions are reusable blocks of code that perform a specific task. They are defined using
the def keyword. For example:
python
RunCopy code
1def my_function():
2 print("Hello, World!")
8. What is a dictionary in Python?
Answer: A dictionary is a collection of key-value pairs. It is unordered and mutable. You can create
a dictionary using curly braces {}. For example: my_dict = {'name': 'Alice', 'age': 25}.
9. What is the purpose of the self keyword in Python?
Answer: The self keyword is used in instance methods to refer to the instance of the class. It
allows access to the attributes and methods of the class in Python.
10. How do you read and write files in Python?
Answer: You can use the built-in open() function to read and write files. For example:
python
RunCopy code
1# Writing to a file
2with open('file.txt', 'w') as f:
3 f.write('Hello, World!')
5# Reading from a file
6with open('file.txt', 'r') as f:
7 content = f.read()
11. What are list comprehensions?
Answer: List comprehensions provide a concise way to create lists. They consist of brackets
containing an expression followed by a for clause. For example:
python
RunCopy code
1squares = [x**2 for x in range(10)]
12. What is the difference between == and is in Python?
Answer: == checks for value equality (whether the values of two objects are the same),
while is checks for identity (whether two references point to the same object in memory).