Friday, 22 September 2017

5)Python Input, Output and Import

Python Input, Output and Import

1)Python Output Using print() function

Using print() function to output data to the standard output screen

Syntax of the print() function:
print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
Note:
By default , separator is space character and end of line is new line character '\n'




Output formatting:

Output formatting in python can be done using str.format() method, 

Note:

This method is accessible to any string object


curly braces {} are used as placeholders.




Also we can specify order in which it is printed by using numbers (tuple index).



We can even use keyword arguments to format the string using % operator.



2)Python Input:
input() function used to read the input data from the user dynamically.

Syntax for input():
input([prompt])

Note:

prompt is the string to display on the screen. It is optional.



Highlights:

  • Always the entered data is in the form of string,
  • if they are numbers, we need to convert using either int() or float() function

Note:
As the output in the form of string, if you want to perform to evaluate the expressions, can be done through eval() function


eval() ==> to perform evaluation of expressions of string



3)Python Import:

  • import keyword is used to import the specific modules in python.
  • import some specific attributes and functions only, using the from keyword.




Previous page

No comments:

Post a Comment

Fundamentals of Python programming

Fundamentals of Python programming: Following below are the fundamental constructs of Python programming: Python Data types Python...