1. What is Python?
Python is a high-level, interpreted programming language known for its simplicity and readability. It supports multiple programming paradigms, including procedural, object-oriented, and functional programming.
2. What are the key features of Python?
- Easy-to-read syntax
- Dynamic typing
- Interpreted language
- Extensive standard library
- Object-oriented programming support
- Cross-platform compatibility
3. What is the difference between Python 2 and Python 3?
Python 2 and Python 3 are two major versions of the Python programming language. Some key differences include:
- Print statement: In Python 2, print is a statement, while in Python 3, it is a function.
- Unicode support: Python 3 handles strings as Unicode by default, whereas Python 2 uses ASCII strings.
- Integer division: In Python 3, the division of two integers results in a float, whereas in Python 2, it truncates the result to an integer.
4. What are the different data types in Python?
Python supports various data types, including:
- Integers
- Floats
- Strings
- Lists
- Tuples
- Dictionaries
- Sets
- Booleans
5. What is a Python decorator?
A decorator in Python is a design pattern that allows you to modify or extend the behavior of functions or methods without changing their code. Decorators are implemented using the @decorator_name syntax.
6. What is a lambda function in Python?
A lambda function in Python is an anonymous function defined using the lambda keyword. It is used for creating small, one-line functions without a name.
7. What is a list comprehension in Python?
List comprehension is a concise way to create lists in Python by iterating over an iterable and applying an expression to each element. It provides a more readable and efficient way to create lists compared to traditional loops.
8. How can you handle exceptions in Python?
Exceptions in Python can be handled using try-except blocks. The code that may raise an exception is placed inside the try block, and the exception handling logic is written in the except block.
9. What is the difference between deep copy and shallow copy in Python?
- Shallow copy: A shallow copy creates a new object but does not copy the contents of nested objects. Changes made to nested objects in the copy affect the original object.
- Deep copy: A deep copy creates a new object and recursively copies the contents of nested objects. Changes made to nested objects in the copy do not affect the original object.
10. How can you open and read a file in Python?
You can open and read a file in Python using the built-in open() function. Here's an example:
with open('filename.txt', 'r') as file:
content = file.read()
print(content)Join for more: https://t.me/crackingthecodinginterview
ENJOY LEARNING ๐๐