Cards (43)

  • What is a module in Python?
    A self-contained file containing Python code.
  • What is the purpose of modules in Python?
    To organize and reuse code effectively.
  • How do modules promote code modularity?
    By encapsulating functions, classes, and variables.
  • What are the key benefits of using modules in Python?
    • Code Organization: Logical grouping and improved readability.
    • Code Reusability: Shared functionality and standardization.
    • Collaboration: Facilitates teamwork on independent modules.
  • What is an example of a module that could be created for a web application?
    A utils.py module for common utility functions.
  • How would you define a function to send an email in a module?
    def send_email(recipient, message):
  • What is the purpose of the import statement in Python?
    To import a module into your Python script.
  • What is the basic syntax for importing a module?
    import module_name
  • How would you import the math module in Python?
    import math
  • How do you access a function from an imported module?
    Using dot notation.
  • How would you calculate the square root of 16 using the math module?
    result = math.sqrt(16)
  • What is the syntax to import specific functions from a module?
    from module_name import function_name, variable_name
  • How would you import only the pi constant from the math module?
    from math import pi
  • How do you calculate the area of a circle using the imported pi constant?
    area = pi * radius**2
  • What are some additional notes regarding module imports in Python?
    • Multiple Imports: Import multiple modules in one statement.
    • Renaming Imports: Use 'as' to rename imports.
    • Package Imports: Use dot notation for packages.
  • What does the math module provide?
    A variety of mathematical functions.
  • What does the math.sqrt(x) function do?
    Calculates the square root of a number x.
  • How would you calculate the hypotenuse of a right triangle with sides 3 and 4 using the math module?
    hypotenuse = math.sqrt(3**2 + 4**2)
  • What does the random module do?
    Generates pseudo-random numbers.
  • What does random.randint(a, b) do?
    Generates a random integer between a and b.
  • How would you simulate a dice roll using the random module?
    dice_roll = random.randint(1, 6)
  • What does random.random() return?
    A random floating-point number between 0.0 and 1.0.
  • What does random.choice(sequence) do?
    Randomly selects an element from a sequence.
  • What does random.shuffle(sequence) do?
    Shuffles the elements in a sequence in-place.
  • What does the datetime module allow you to do?
    Work with dates and times.
  • What does datetime.now() return?
    The current date and time as a datetime object.
  • How would you calculate the time difference between two events using the datetime module?
    time_difference = end_time - start_time
  • How do you create a specific date object using the datetime module?
    datetime.date(year, month, day)
  • How do you create a specific time object using the datetime module?
    datetime.time(hour, minute, second, microsecond)
  • What is the structure of a Python module?
    A .py file containing Python code.
  • How do you import a module from the same directory?
    Use the import statement.
  • How do you import a module from a different directory?
    Add the directory to Python's module search path.
  • What is a package in Python?
    A directory containing one or more modules and an __init__.py file.
  • What is the purpose of the __init__.py file in a package?
    It declares the directory as a package and can contain initialization code.
  • How does Python treat directories containing an __init__.py file?
    As packages, allowing module imports using dot notation.
  • What can the __init__.py file contain?
    Initialization code that runs when the package is imported.
  • How can you control imports in a package using __init__.py?
    By selectively importing modules in the __init__.py file.
  • How would you import a specific function from a module in a package?
    from .module_name import function_name
  • What is the structure of a package in Python?
    A directory with an __init__.py file and one or more modules.
  • What does the Person class in the example do?
    Calculates age based on the current year and birth year.