Question: What are the built-in data types in Python?
|
Answer: Python supports following built-in data types:
integers, floats, strings, lists, tuples, dictionaries, sets, booleans, etc.
|
Question: How do you define a class in Python?
|
Answer: One can define a class in Python using the class keyword followed by the class name and a colon.
Inside the class, you can define attributes and methods. For example:
class MyClass:
def __init__(self, name):
self.name = name
def greet(self):
print("Hello, " + self.name + "!")
|
Question: What is the difference between 'append()' and 'extend()' methods for lists in Python?
|
Answer: The 'append()' method adds a single element to the end of a list.
The 'extend()' method adds all the elements of another list to the end of the current list.
|
Question: What is a 'generator' in Python?
|
Answer: A 'generator' in Python is a special type of iterator that generates values lazily
as they are needed.
'Generators' are created using functions and the yield keyword instead of return.
|
Question: How do you open and read from a file in Python?
|
Answer: You can open and read from a file in Python using the 'open()' function. For example:
with open('file.txt', 'r') as f:
contents = f.read()
|
Question: What are lambda functions in Python?
|
Answer: The Lambda functions, also known as anonymous functions, are small, single-expression functions
defined using the lambda keyword. They are often used for short, simple operations. For example:
square = lambda x: x ** 2
|
Question: What is the purpose of the '__init__()' method in Python classes?
|
Answer: The '__init__()' method is a special method in Python classes used to initialize
new instances of the class. It is called automatically when a new object is created.
|