writing_python_scripts/questions.rst
author Puneeth Chaganti <punchagan@fossee.in>
Wed, 17 Nov 2010 23:24:23 +0530
changeset 520 8249ae9d570a
parent 465 78d20cd87c7e
permissions -rw-r--r--
Minor edits.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
296
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
     1
Objective Questions
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
     2
-------------------
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
     3
335
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
     4
 1. Which of the following variables contains the locations to search for
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
     5
    python modules
296
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
     6
335
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
     7
   a. sys.pythonpath
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
     8
   #. sys.path
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
     9
   #. os.pythonpath
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
    10
   #. os.path
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
    11
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
    12
   Answer: sys.path
296
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    13
335
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
    14
 2. What is the type of ``sys.path``
296
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    15
335
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
    16
   a. list of strings
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
    17
   #. list of int
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
    18
   #. string
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
    19
   #. tuple of strings
296
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    20
335
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
    21
   Answer: list of strings
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
    22
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
    23
 3. The script ``utils.py`` is in one of locations of PYTHONPATH and contains
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
    24
    the following code:
296
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    25
335
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
    26
      def show(x):
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
    27
          print x
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
    28
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
    29
      show("Hello World")
296
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    30
335
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
    31
      if __name__ == "__main__":
296
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    32
335
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
    33
          show("Hello Test")
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
    34
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
    35
    How do you import the file.
296
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    36
335
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
    37
   a. import utils
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
    38
   #. import utils.py
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
    39
   #. import /home/user/utils
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
    40
   #. import /home/user/utils.py
296
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    41
335
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
    42
   Answer: import utils
296
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    43
335
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
    44
 4. The script ``utils.py`` is in one of locations of PYTHONPATH and contains
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
    45
    the following code:
296
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    46
335
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
    47
      def show(x):
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
    48
          print x
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
    49
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
    50
      show("Hello World")
296
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    51
335
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
    52
      if __name__ == "__main__":
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
    53
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
    54
          show("Hello Test")
296
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    55
335
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
    56
    How do you use the ``show`` function after doing ``import utils``
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
    57
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
    58
   a. utils.show("hey")
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
    59
   #. show("hey")
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
    60
   #. utils.py.show("hey")
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
    61
   #. utils.py.show.py("hey")
296
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    62
335
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
    63
   Answer: utils.show("hey")
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
    64
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
    65
 5. The script ``utils.py`` is in one of locations of PYTHONPATH and contains
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
    66
    the following code:
296
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    67
335
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
    68
      def show(x):
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
    69
          print x
296
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    70
335
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
    71
      show("Hello World")
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
    72
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
    73
      if __name__ == "__main__":
296
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    74
335
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
    75
          show("Hello Test")
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
    76
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
    77
    How do you use the ``show`` function after doing ``from utils import show``
296
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    78
335
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
    79
   a. utils.show("hey")
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
    80
   #. show("hey")
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
    81
   #. utils.py.show("hey")
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
    82
   #. utils.py.show.py("hey")
296
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    83
335
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
    84
   Answer: show("hey")
296
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    85
335
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
    86
 5. The script ``utils.py`` is in one of locations of PYTHONPATH and contains
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
    87
    the following code:
296
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    88
335
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
    89
      def show(x):
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
    90
          print x
296
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    91
335
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
    92
      show("Hello World")
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
    93
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
    94
      if __name__ == "__main__":
296
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    95
335
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
    96
          show("Hello Test")
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
    97
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
    98
    What is printed when you do ``import utils``
296
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    99
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   100
   Answer::
335
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
   101
      Hello World
296
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   102
335
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
   103
 6. The script ``utils.py`` is in one of locations of PYTHONPATH and contains
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
   104
    the following code:
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
   105
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
   106
      def show(x):
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
   107
          print x
296
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   108
335
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
   109
      show("Hello World")
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
   110
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
   111
      if __name__ == "__main__":
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
   112
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
   113
          show("Hello Test")
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
   114
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
   115
    What is printed when the script is executed.
296
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   116
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   117
   Answer::
335
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
   118
      Hello World
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
   119
      Hello Test
296
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   120
465
78d20cd87c7e Added questions to writing python scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents: 335
diff changeset
   121
 7. Say, we wish to use the ``pi`` value from the ``math`` module in
78d20cd87c7e Added questions to writing python scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents: 335
diff changeset
   122
    the standard library. How do we import it?
78d20cd87c7e Added questions to writing python scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents: 335
diff changeset
   123
78d20cd87c7e Added questions to writing python scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents: 335
diff changeset
   124
    Answer: from math import pi  OR import math.pi OR 
78d20cd87c7e Added questions to writing python scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents: 335
diff changeset
   125
            import math.pi as pi
78d20cd87c7e Added questions to writing python scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents: 335
diff changeset
   126
78d20cd87c7e Added questions to writing python scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents: 335
diff changeset
   127
            
78d20cd87c7e Added questions to writing python scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents: 335
diff changeset
   128
  8. A module should contain only functions, True or False?
78d20cd87c7e Added questions to writing python scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents: 335
diff changeset
   129
78d20cd87c7e Added questions to writing python scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents: 335
diff changeset
   130
     Answer: False.
78d20cd87c7e Added questions to writing python scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents: 335
diff changeset
   131
  
78d20cd87c7e Added questions to writing python scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents: 335
diff changeset
   132
78d20cd87c7e Added questions to writing python scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents: 335
diff changeset
   133
Larger Questions
78d20cd87c7e Added questions to writing python scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents: 335
diff changeset
   134
----------------
78d20cd87c7e Added questions to writing python scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents: 335
diff changeset
   135
78d20cd87c7e Added questions to writing python scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents: 335
diff changeset
   136
1. Look at the python documentation (from the web) and learn how to
78d20cd87c7e Added questions to writing python scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents: 335
diff changeset
   137
   make a folder containing some python files into a module.
78d20cd87c7e Added questions to writing python scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents: 335
diff changeset
   138
78d20cd87c7e Added questions to writing python scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents: 335
diff changeset
   139
   Answer: Add a file named __init__.py
78d20cd87c7e Added questions to writing python scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents: 335
diff changeset
   140
78d20cd87c7e Added questions to writing python scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents: 335
diff changeset
   141
2. We know that ``sys.path`` has the paths in which Python checks for
78d20cd87c7e Added questions to writing python scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents: 335
diff changeset
   142
   a module, when it is imported. Suppose you have a package ``utils``
78d20cd87c7e Added questions to writing python scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents: 335
diff changeset
   143
   in some location on your disk, which is not on the python path, how
78d20cd87c7e Added questions to writing python scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents: 335
diff changeset
   144
   will you import it?
78d20cd87c7e Added questions to writing python scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents: 335
diff changeset
   145
78d20cd87c7e Added questions to writing python scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents: 335
diff changeset
   146
   Answer: Append that path to the ``sys.path`` variable.