Input and Output Management

 Input-Output Statement

                    The input and output statements are called input/output statement or IO statement is a portion of a program that instructs a computer how to read and process information. it pertains to gather information from an input device or sending information to an output device.

 simplepycoder.io_statement                    
One of the most important tasks in programming is to know how to get input from the user, and how to present them on the monitor after evaluating it.

Python Input

In python there are two input methods:
  • input()
  • raw_input()

input()

                The input function presents a prompt to the user. The parameter of the function is a string. The string is helpful to the user to enter something.

                  Syntax:

                               var_name = input("prompt string")

var_name        -  It is a variable that is used to store the user input.
prompt string  -  It is a command to the user.

simplepycoder.input

Here all inputs are stored as a string. In the above example first, we enter 12 that is an integer data type. but it is stored as a string in a. 
If we want to get an integer to evaluate an arithmetic operation, but the input stored as a string. because of its error will occur.To solve this problem we can use int(), float(), str() with input() method.
Examples

int():

                    If using int() method with input() method the input is stored as an integer. But if we enter a string instead of an integer, then an error message will occur.

        Example

simplepycoder.int

a similar result will occur while using float() and str().

Example

simplepycoder.float

Python Output

                    Output is a result of a program that is displayed on the monitor. We can display the output using the print() method. In this method, we can pass zero to more expressions separated by a comma. If we want to print a sentence, we should pass the sentence to the print method with quotes.

        Syntax:  

                    print( "Sentence" )

If we want to print a value of a variable, then pass the variable without any quotes.

        Syntax:

                    print( var_name )

We can print a sentence that has the value of a variable, then we use the following syntax:

        Syntax:

                       print("Sentence", var_name)

Example:

simplepycoder.print

In the second print() statement, we can notice that space was added between the string and the value of variable a. This is by default, but we can change it.

        Syntax:

                    print(*objects, sep = ' ' ,end = ' ' , file = sys,stdout,flush = False)

objects  -  It is a number of values to be printed.
sep        -  It represents the separator The separator is a word or symbol which is used between the values. The default separator is space.
end        -  It is a symbol or word which is print after all values are printed. It defaults into a new line.
file        -  The file is the object where the values are printed and its default value is sys.stdout(screen).

Example

simplepycoder.output

Output Formatting

                    Sometimes we would like to format our output to make it look attractive. This can be done by using the str.format() method. This method is visible to any string object.

Example 1

simplepycoder.format1

In this example, first, we create a placeholder using '{}' in the string which is printed on the monitor, and enter the value or variable in format() method which is print in the placeholder.

    
    Syntax:  
                    print(string.format(value))

Example 2

simplepycoder.format2

In this example, we print a number as a float number.
 

        Syntax:

                    print(....... {:.n f} .... . format(integer))

n   -   represent how many zeros printed after the floating-point.

simplepycoder.format3

 
In this example, we print multiple values or values of the variable in the created placeholder. The values which are pass into the format() method is considered as a tuple and it has an index. 

For example:

simplepycoder.format4

Here we use the index number inside the placeholder. The corresponding value of the index is printed in the placeholder.

We can also assign a variable with a value inside the format() method which is printed in the placeholder.

For example:

simplepycoder.format5

0/Post a Comment/Comments

Previous Post Next Post