Posts

Python Basic: Exercise-2 with Solution

  Write a Python program to find out what version of Python you are using. A string containing the version number of the Python interpreter plus additional information on the build number and compiler used. This string is displayed when the interactive interpreter is started. Version info: A tuple containing the five components of the version number: major, minor, micro, releaselevel, and serial. All values except releaselevel are integers; the release level is 'alpha', 'beta', 'candidate', or 'final'. The version_info value corresponding to the Python version 2.0 is (2, 0, 0, 'final', 0). The components can also be accessed by name, so sys.version_info[0] is equivalent to sys.version_info.major and so on. Note : 'sys' module provides access to some variables used or maintained by the interpreter and to functions that interact strongly with the interpreter. Sample Solution: Python Code: import sys print ( "Python version" ) pr...

Python Basic: Exercise-1 with Solution

Image
  Write a Python program to print the following string in a specific format (see the output). Sample String:  "Twinkle, twinkle, little star, How I wonder what you are! Up above the world so high, Like a diamond in the sky. Twinkle, twinkle, little star, How I wonder what you are!" The print statement (Python 2.6) has been replaced with a print() function (Python 2.6), with keyword arguments to replace most of the special syntax of the old print statement . The print() function doesn’t support the “softspace” feature of the old print statement. For example, in Python 2.x, print "A\n", "B" would write "A\nB\n"; but in Python 3.0, print("A\n", "B") writes "A\n B\n". Initially, you’ll be finding yourself typing the old print x a lot in interactive mode. Time to retrain your fingers to type print(x) instead! When using the 2to3 source-to-source conversion tool, all print...