Python Unit 3: Interaction with SQLite
40 Questions
0 Views

Python Unit 3: Interaction with SQLite

Created by
@ConciseMaracas

Questions and Answers

What is the primary purpose of the from...import statement in Python?

  • To redefine existing functions in the current module.
  • To import an entire module into the current namespace.
  • To assign values to multiple variables at once.
  • To import specific names from a module into the current namespace. (correct)
  • What is the purpose of the global statement in Python?

  • To declare a variable within the local scope
  • To limit the accessibility of a variable
  • To automatically convert local variables to global variables
  • To indicate that a variable should be treated as a global variable (correct)
  • What does the from modname import * statement do?

  • Ensures that only the module's constants are imported.
  • Imports only the specified function from the module.
  • Imports all functions and variables from the given module into the current namespace. (correct)
  • Replaces existing functions in the current namespace.
  • What will be printed when the following code is executed?

    def test_func(): global x x = 50

    x = 25

    test_func() print(x)

    <p>50</p> Signup and view all the answers

    Where does the Python interpreter first look for a module when importing?

    <p>In the current directory.</p> Signup and view all the answers

    What function can be used to access the names defined in the global namespace?

    <p>globals()</p> Signup and view all the answers

    What is stored in the sys.path variable in Python?

    <p>A list of directories that Python checks for modules.</p> Signup and view all the answers

    What will the dir() function return when called on a module?

    <p>A sorted list of names defined by the module</p> Signup and view all the answers

    In Python, what does the PYTHONPATH variable consist of?

    <p>A list of directories to search for modules.</p> Signup and view all the answers

    When you assign a value to a variable in Python, what is the relationship between the variable and the object?

    <p>The variable becomes a reference to the object stored in memory.</p> Signup and view all the answers

    In the context of Python functions, what does the locals() function return when called?

    <p>All names accessible locally from that function</p> Signup and view all the answers

    Which of the following statements about id() function is true?

    <p>It returns the memory address of the object the variable refers to.</p> Signup and view all the answers

    What will happen if the reload() function is called on an already imported module?

    <p>The previously imported module will be reloaded</p> Signup and view all the answers

    What is recommended regarding the use of the from module import * statement?

    <p>It should be used sparingly to avoid namespace conflicts.</p> Signup and view all the answers

    What would be the output of the following code?

    x = 5

    def test(): global x x += 5 return x

    print(test())

    <p>10</p> Signup and view all the answers

    If a function does not use the global statement, what scope do any variables assigned a value within that function have?

    <p>Local scope</p> Signup and view all the answers

    What is the purpose of the reload() function in Python?

    <p>To reimport a previously imported module</p> Signup and view all the answers

    What should be included in the folder to define a Python package?

    <p>An empty init.py file</p> Signup and view all the answers

    Which of the following statements about Python packages is true?

    <p>A package can contain multiple modules</p> Signup and view all the answers

    How does a module differ from a package in Python?

    <p>A module is a file while a package is a folder</p> Signup and view all the answers

    What will happen if you change p1.py and do not use the reload() function?

    <p>The output will remain the same as before the changes</p> Signup and view all the answers

    What is the first step to create a Python package named python_package?

    <p>Create a folder named C:\MyApp</p> Signup and view all the answers

    Which command can be used to create modules within a package using the command line?

    <p>cmd</p> Signup and view all the answers

    What does the init.py file signify in a package structure?

    <p>It marks the folder as a package</p> Signup and view all the answers

    What method is used to close the cursor after completing operations in SQLite?

    <p>cursor.close()</p> Signup and view all the answers

    What is the purpose of the conn.commit() method in the INSERT operation?

    <p>It saves the changes made to the database.</p> Signup and view all the answers

    Which command is used to create a new table in SQLite?

    <p>CREATE TABLE</p> Signup and view all the answers

    In the INSERT operation example, which ID is associated with the name 'AMIT'?

    <p>1</p> Signup and view all the answers

    What happens when the execute method is called with an INSERT statement?

    <p>A new record is added to the database.</p> Signup and view all the answers

    Which of the following statements is true regarding closing the connection in SQLite?

    <p>You can close the connection without closing the cursor.</p> Signup and view all the answers

    What would you use to retrieve all records from the developer table?

    <p>SELECT * FROM DEVELOPER</p> Signup and view all the answers

    Which Python module is used to interact with SQLite databases?

    <p>sqlite3</p> Signup and view all the answers

    What does the id() function return for a variable in Python?

    <p>The memory address of the variable</p> Signup and view all the answers

    How does Python resolve variable names when accessed within a function?

    <p>It looks in local first, then global, and finally built-in namespaces.</p> Signup and view all the answers

    Which variable is accessible and mutable within the inner_function() according to the given example?

    <p>c</p> Signup and view all the answers

    What will happen if you attempt to assign a value to the variable b within inner_function()?

    <p>A new local variable <code>b</code> will be created.</p> Signup and view all the answers

    What is the output of the outer_function() given the initial code example?

    <p>a = 30, a = 20, a = 10</p> Signup and view all the answers

    What happens when a local variable has the same name as a global variable?

    <p>The global variable is ignored.</p> Signup and view all the answers

    In the provided examples, which variable is considered nonlocal when accessed from inner_function()?

    <p>b</p> Signup and view all the answers

    Which of the following describes a class method's scope in Python?

    <p>It follows the same scoping rules as ordinary functions.</p> Signup and view all the answers

    Study Notes

    Module Importing in Python

    • Specific functions can be imported from a module using from module_name import function_name. For example, from math import pi imports the constant pi from the math module.
    • Multiple functions can be imported simultaneously, e.g., from math import sqrt, factorial.
    • Use from module_name import * to import all names from a module. This method should be used sparingly to avoid namespace pollution.

    Locating Modules

    • Python searches for modules in this order:
      • Current directory
      • Directories in the PYTHONPATH environment variable
      • Default system paths (e.g., /usr/local/lib/python on UNIX)
    • The search path is stored in sys.path.

    Environment Variables

    • PYTHONPATH lists directories that Python searches for modules. Example for Windows: set PYTHONPATH = c:\python20\lib;, and for UNIX: set PYTHONPATH = /usr/local/lib/python.

    Namespaces and Scoping

    • Names are identifiers for objects in Python, enabling access to the objects. For instance, a=2 assigns the object 2 to the name a.
    • Local namespaces exist for functions, while global namespaces exist for the entire program.
    • A reference is checked in the local, global, and built-in namespaces in that order.
    • Nested functions create their own local scopes.

    Variable Scope in Functions

    • When inside a nested function, local variables can be accessed, while non-local variables can only be read.
    • Global variables can be accessed by declaring them with the global statement.

    Viewing Current Namespaces

    • Use dir() to get a sorted list of names defined in a module, and locals() or globals() to view current local and global variables, respectively.

    Reloading Modules

    • The reload() function allows re-executing previously imported modules, useful when changes were made to the module code without leaving the interpreter.
    • Syntax: reload(module_name).

    Python Packages

    • Packages in Python allow organization of multiple modules in directories, with a folder structure that includes an __init__.py file indicating a package.
    • Example structure includes a main folder with subfolders that contain modules necessary for a project.

    SQLite Operations in Python

    • Database connections are managed through the sqlite3 module.
    • Create a connection and table using:
      import sqlite3
      con = sqlite3.connect('db1.db')
      con.execute("CREATE TABLE developer(ID INT PRIMARY KEY, NAME TEXT NOT NULL, AGE INT, SALARY REAL)")
      
    • Close connections with con.close().

    Database Insert and Query Operations

    • Insert data using the INSERT SQL command:
      conn.execute("INSERT INTO DEVELOPER(ID, NAME, AGE, SALARY) VALUES(1, 'AMIT', 32, 25000.00)")
      
    • To retrieve data, use the SELECT SQL command, iterating through results for display.

    Update Operations in SQLite

    • Utilize the UPDATE command to modify existing records in a table, followed by SELECT to fetch updated entries.

    Studying That Suits You

    Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

    Quiz Team

    Description

    This quiz covers the key concepts of Python's interaction with SQLite, including importing specific functions from modules such as math. Test your understanding of syntax and functionality in Python with practical examples.

    More Quizzes Like This

    Python Interaction with SQLite Module
    16 questions
    Python Classes Overview
    8 questions
    Python Module Flashcards - Edube Module 1
    11 questions
    Use Quizgecko on...
    Browser
    Browser