parsing_data/questions.rst
changeset 238 c507e9c413c6
equal deleted inserted replaced
237:6c203780bfbe 238:c507e9c413c6
       
     1 Objective Questions
       
     2 -------------------
       
     3 
       
     4  1. How do you split the string "Guido;Rossum;Python" to get the words
       
     5 
       
     6    Answer: line.split(';')
       
     7 
       
     8  2. line.split() and line.split(' ') are same
       
     9 
       
    10    a. True
       
    11    #. False
       
    12 
       
    13    Answer: False
       
    14 
       
    15  3. What is the output of the following code::
       
    16 
       
    17       line = "Hello;;;World;;"
       
    18       sub_strs = line.split()
       
    19       print len(sub_strs)
       
    20 
       
    21     Answer: 5
       
    22 
       
    23  4. What is the output of "      Hello    World    ".strip()
       
    24 
       
    25    a. "Hello World"
       
    26    #. "Hello     World"
       
    27    #. "      Hello World"
       
    28    #. "Hello World     "
       
    29    
       
    30    Answer: "Hello    World"
       
    31 
       
    32  5. What does "It is a cold night".strip("It") produce
       
    33     Hint: Read the documentation of strip
       
    34 
       
    35    a. "is a cold night"
       
    36    #. " is a cold nigh" 
       
    37    #. "It is a cold nigh"
       
    38    #. "is a cold nigh"
       
    39 
       
    40    Answer: " is a cold nigh"
       
    41 
       
    42  6. What does int("20") produce
       
    43 
       
    44    a. "20"
       
    45    #. 20.0
       
    46    #. 20
       
    47    #. Error
       
    48 
       
    49    Answer: 20
       
    50 
       
    51  7. What does int("20.0") produce
       
    52 
       
    53    a. 20
       
    54    #. 20.0
       
    55    #. Error
       
    56    #. "20"
       
    57 
       
    58    Answer: Error
       
    59 
       
    60  8. What is the value of float(3/2)
       
    61 
       
    62    a. 1.0
       
    63    #. 1.5
       
    64    #. 1
       
    65    #. Error
       
    66 
       
    67    Answer: 1.0
       
    68 
       
    69  9. what doess float("3/2") produce
       
    70 
       
    71    a. 1.0
       
    72    #. 1.5
       
    73    #. 1
       
    74    #. Error
       
    75 
       
    76    Answer: Error
       
    77    
       
    78  10. See if there is a function available in pylab to calculate the mean
       
    79      Hint: Use tab completion
       
    80 
       
    81 Larger Questions
       
    82 ================
       
    83 
       
    84  1. The file ``pos.txt`` contains two columns of data. The first and second
       
    85     columns are the x and y co-ordiantes of a particle in motion, respectively.
       
    86     Plot the trajectory of the particle.
       
    87 
       
    88    Answer::
       
    89 
       
    90      x_values = []
       
    91      y_values = []
       
    92 
       
    93      for line in open("/home/fossee/pos.txt");
       
    94          x_str, y_str = line.split()
       
    95          x = int(x_str)
       
    96          y = int(y_str)
       
    97 
       
    98          x_values.append(x)
       
    99          y_values.append(y)
       
   100 
       
   101          plot(x, y, 'b.')
       
   102