Python materials
Python materials
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.
Dynamic typing
Interpreted language
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.
Answer: You can create a list by enclosing elements in square brackets. For example: my_list = [1,
2, 3, 4].
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 ().
Answer: You can handle exceptions using the try and except blocks. For example:
python
RunCopy code
1try:
3except SomeException:
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!")
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}.
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.
Answer: You can use the built-in open() function to read and write files. For example:
python
RunCopy code
1# Writing to a file
3 f.write('Hello, World!')
7 content = f.read()
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
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).