Python fpdf Library

 from fpdf import FPDF


# Create a PDF

pdf = FPDF()

pdf.add_page()

pdf.set_auto_page_break(auto=True, margin=15)


# Set font

pdf.set_font("Arial", size=12)


# Title

pdf.set_font("Arial", 'B', 16)

pdf.cell(200, 10, txt="Basic Python Interview Questions for Freshers", ln=True, align='C')

pdf.ln(10)


# Reset font

pdf.set_font("Arial", size=12)


# Content

qa_pairs = [

    ("1. What is Python?",

     "Python is a high-level, interpreted programming language known for its simplicity and readability. It supports multiple programming paradigms, such as procedural, object-oriented, and functional programming."),

    

    ("2. What are the key features of Python?",

     "- Easy to read and write\n- Interpreted language\n- Dynamically typed\n- Extensive standard library\n- Object-oriented\n- Cross-platform"),

    

    ("3. What is the difference between a list and a tuple?",

     "List:\n  - Mutable (can be changed)\n  - Uses []\n  - Slower\nTuple:\n  - Immutable (cannot be changed)\n  - Uses ()\n  - Faster"),

    

    ("4. What are Python data types?",

     "- Numeric: int, float, complex\n- Sequence: list, tuple, range\n- Text: str\n- Set: set, frozenset\n- Mapping: dict\n- Boolean: bool"),

    

    ("5. What is a dictionary in Python?",

     "A dictionary is an unordered collection of key-value pairs.\nExample:\n  my_dict = {\"name\": \"John\", \"age\": 25}"),

    

    ("6. What is the difference between 'is' and '==' in Python?",

     "- '==' compares values\n- 'is' compares object identity"),

    

    ("7. What are functions in Python?",

     "Functions are blocks of reusable code that perform a specific task.\nExample:\n  def greet():\n    print(\"Hello\")"),

    

    ("8. What is a lambda function?",

     "An anonymous function defined using the 'lambda' keyword.\nExample:\n  add = lambda x, y: x + y"),

    

    ("9. What are *args and **kwargs?",

     "- *args: Non-keyword variable-length arguments (tuple)\n- **kwargs: Keyword variable-length arguments (dictionary)\nExample:\n  def func(*args, **kwargs):\n    print(args)\n    print(kwargs)"),

    

    ("10. What is indentation in Python?",

     "Indentation defines blocks of code. It is mandatory in Python and replaces the use of {} like in other languages."),

    

    ("11. What is a module in Python?",

     "A file containing Python code (functions, variables) that can be imported into another file.\nExample:\n  import math"),

    

    ("12. What is the use of 'self' in Python classes?",

     "'self' refers to the instance of the class and is used to access variables and methods within the class."),

    

    ("13. What are loops in Python?",

     "- for loop: used to iterate over a sequence\n- while loop: executes as long as a condition is true"),

    

    ("14. What are Python conditional statements?",

     "- if\n- elif\n- else\nExample:\n  if x > 0:\n    print(\"Positive\")"),

    

    ("15. How do you handle exceptions in Python?",

     "Using try, except, finally blocks.\nExample:\n  try:\n    x = 10 / 0\n  except ZeroDivisionError:\n    print(\"Cannot divide by zero\")")

]


# Add content to PDF

for question, answer in qa_pairs:

    pdf.set_font("Arial", 'B', 12)

    pdf.multi_cell(0, 10, question)

    pdf.set_font("Arial", size=12)

    pdf.multi_cell(0, 10, answer)

    pdf.ln(2)


# Output the PDF

pdf.output("Basic_Python_Interview_Questions.pdf")


Comments

Popular Posts