p

python tutorial - Python interview questions and answers for testers - learn python - python programming



python interview questions :131

How to use python to format strings with delimiter into columns?

>>> pairs = map(str.split, text.splitlines())
>>> max_len = max(len(pair[0]) for pair in pairs)
>>> info = '\n'.join('{key:<{indent}}{val}'.format(key=k,
                                                   indent=max_len + 2,
                                                   val=v) for k, v in pairs)
>>> print info
abc:    234234
aadfa:  235345
bcsd:   992
click below button to copy the code. By Python tutorial team
python delimiter

Learn python - python tutorial - python delimiter - python examples - python programs

>>> import math
>>> info = ["abc: 234234", "aadfa: 235345", "bcsd: 992"]
>>> info = [item.split() for item in info]
>>> maxlen = max([len(item[0]) for item in info])
>>> maxlen = math.ceil(maxlen/8.0)*8
>>> info = [item[0]+" "*(maxlen-len(item[0]))+item[1] for item in info]
click below button to copy the code. By Python tutorial team
  • You can control how the final length is made.

python interview questions :132

How can I get the CPU temperature in Python?

  • There's no standard Python library for this, but on various platforms you may be able to use a Python bridge to a platform API to access this information.
  • For example on Windows this is available through the Windows Management Instrumentation (WMI) APIs, which are available to Python through the PyWin32 library. There is even a Python WMI library which wraps PyWin32 to provide a more convenient interface.
  • To get the temperature you'll need to use one of these libraries to access the root/WMI namespace and the MSAcpi_ThermalZone Temperature class. This gives the temperature in tenths of a Kelvin, so you'll need to convert to Celsius by deducting 2732 and dividing by 10.
python cpu temperature

Learn python - python tutorial - python cpu temperature - python examples - python programs

python interview questions :133

What is a Python module?

  • A module is a Python script that generally contains import statements, functions, classes and variable definitions, and Python runnable code and it “lives” file with a ‘.py’ extension.
  • zip files and DLL files can also be modules.Inside the module, you can refer to the module name as a string that is stored in the global variable name .

python interview questions :134

Name the File-related modules in Python?

  • Python provides libraries / modules with functions that enable you to manipulate text files and binary files on file system. Using them you can create files, update their contents, copy, and delete files.
python module

Learn python - python tutorial - python module - python examples - python programs

  • The libraries are : os, os.path, and shutil.
  • Here, os and os.path - modules include functions for accessing the filesystem shutil - module enables you to copy and delete the files.

python interview questions :135

Explain the use of with statement?

  • In python generally “with” statement is used to open a file, process the data present in the file, and also to close the file without calling a close() method. “with” statement makes the exception handling simpler by providing cleanup activities.
  • General form of with:with open(“file name”, “mode”) as file-var:processing statements

Note: no need to close the file by calling close() upon file-var.close()

python interview questions :136

Explain all the file processing modes supported by Python ?

  • Python allows you to open files in one of the three modes. They are: read-only mode, write-only mode, read-write mode, and append mode by specifying the flags “r”, “w”, “rw”, “a” respectively.
  • A text file can be opened in any one of the above said modes by specifying the option “t” along with “r”, “w”, “rw”, and “a”, so that the preceding modes become “rt”, “wt”, “rwt”, and “at”.A binary file can be opened in any one of the above said modes by specifying the option “b” along with “r”, “w”, “rw”, and “a” so that the preceding modes become “rb”, “wb”, “rwb”, “ab”.

python interview questions :137

Explain how to redirect the output of a python script from standout(ie., monitor) on to a file ?

  • Open an output file in “write” mode and the print the contents in to that file, using sys.stdout attribute.
import sys
filename = “outputfile” sys.stdout = open() print “testing”
click below button to copy the code. By Python tutorial team
  • you can create a python script say .py file with the contents, say print “testing” and then redirect it to the output file while executing it at the command prompt.
print “Testing”
execution: python redirect_output.py > outputfile.
click below button to copy the code. By Python tutorial team

python interview questions :138

Explain the shortest way to open a text file and display its contents.?

  • The shortest way to open a text file is by using “with” command as follows:
with open("file-name", "r") as fp:
fileData = fp.read()
#to print the contents of the file print(fileData)
click below button to copy the code. By Python tutorial team

python interview questions :139

How do you create a dictionary which can preserve the order of pairs?

  • We know that regular Python dictionaries iterate over <key, value> pairs in an arbitrary order, hence they do not preserve the insertion order of <key, value> pairs.
  • Python 2.7. introduced a new “OrderDict” class in the “collections” module and it provides the same interface like the general dictionaries but it traverse through keys and values in an ordered manner depending on when a key was first inserted.
from collections import OrderedDict
d = OrderDict([('Company-id':1),('Company-Name':'Intellipaat')])
d.items() # displays the output as: [('Company-id':1),('Company-Name':'Intellipaat')]
click below button to copy the code. By Python tutorial team

python interview questions :140

When does a dictionary is used instead of a list?

  • Dictionaries - are best suited when the data is labelled, i.e., the data is a record with field names.
  • lists - are better option to store collections of un-labelled items say all the files and sub directories in a folder.
  • Generally Search operation on dictionary object is faster thansearching a list object.

Related Searches to Python interview questions and answers for testers