Python Input Output (I/O) and Import

    In this tutorial, we are going to discuss two main inbuilt functions in Python, namely print() and input(), that you often need to use in your code to perform I/O (Input/Output) operations. Also, we will discuss what the import keyword is and why we use it. So, without further ado, let's get started!

    Python print(), input(), and import

    In Python, you will get several inbuilt functions capable of performing specific tasks. In our previous Python tutorials, we have used the print() function a lot, and now, you might want to know how it work. Well, in the upcoming sections, we will explain the print() function which facilitates output operations in Python. Also, we will discuss the input() function which facilitates input operations.

    Python print() Function

    In Python, if you want to show something on-screen as an output, you can use the print() function. Usually, the print() function allows you to show the output data of variables and strings .

    Let’s understand the print() function with an example:

    print("to print a normal string you can use quotes, it could be single or double")
    x=10
    # to print a variable just put it inside the print function
    #inside the print function if you do use the quotes it treats as a variable
    print("x")               #here x is a string
    print(x)                 #here x is a variable containing a value 10
    #use comma, to separate two different data types while using the print option
    integer = 10
    string = "Hello world"
    print(string, integer)

    Output

    x
    10
    Hello world 10

    If you write two print statements, you will get two outputs in two lines because the print() function has an additional keyword inside it with default value ' \n' , which is used to print a new line. However, if you want, you can alter the value of the end. e.g.

    print("Hello" , end="#" )
    print("world")
    print("hello", end="\t")
    print("world")

    Output

    Hello#world
    
    hello   world

    String Formatting:

    There is a special function called format() which is used with a string. It gives you more flexibility over the print() function. The format() function is used with a string, and you can insert variables in specific places of that particular string. We use {} curly brackets to hold the places of variables or strings that pass in the format() function. Let’s try to understand the format() function with an example:

    s= "Steve"
    age=30
    print("hi, there my name is {} and I am {} years old ". format(s,age))

    #Output

    hi, there my name is Steve and I am 30 years old.

    The {} is also known as placeholders for the format() function, and the placeholders should correspond to the variables passed in the format() function. You can also use indexing in the placeholder to call the corresponding values in the format() function. e.g.

    print("i love {1} and {0}".format("mangoes","apples"))
    print("i love {0} and {0}".format("mangoes","apples"))

    #Output

    i love apples and mangoes
    i love mangoes and mangoes

    Python input() Function

    In the above examples, we assign the values to the variable before we ran the program. However, what if we want to get a value from the user rather than pre-defining it? Well, we can use the input() function that allows a user to input the values in the program. Whenever you input a value in the program with the help of the input() function, the value is treated as a string. Remember that if you want to enter an integer or float, you need to use type casting .

    input() function syntax

    variable = input([prompt])

    Here whatever value we enter will store in the variable . The prompt is a string that is used as a message that displays on the screen

    Let’s understand the input() function with an example:

    input_1=input("Whatever you enter here treated as a string: ")
    integer = int(input("Enter an integer: "))
    float_1= float(input("Enter a float: "))
    print(input_1)
    print(integer)
    print(float_1)

    Output

    Whatever you enter here treated as a string: 30
    Enter an integer: 40
    Enter a float: 50
    30
    40
    50.0

    Import in Python:

    Import is a keyword that is used to import the definitions or functions from a module. Also, a module is a Python file that contains prewritten code or functions. The import keyword is used along with another keyword from which is used to import the module. Suppose you want a program that accepts an integer and give the square root of the integer. For this, you can make a program or you can use a predefined function sqrt() , which is a part of module math . So, to use the sqrt() function, you must import it on your current file using the import keyword.

    Let’s understand it with the following example:

    from math import sqrt
    var = int(input("Enter a Number: "))
    sqt = sqrt(var)
    print(sqt)

    Output

    Enter a Number: 10
    3.1622776601683795

    Important Points to Remember

    • To print two data types using the print() function, use a comma to separate them.
    • Use Type conversion to convert the data type of the value you get from the input() function.
    • The from keyword is used to import a module.
    • The import keyword imports the definitions or functions from a module.
    • There are many modules in Python.
    • You can also install third-party modules or libraries using the pip install command .

    Conclusion

    In Python, we can use print() and input() functions to perform input/output operations. As a Python developer, you need to be well aware of both functions because you may need to use them multiple times within a Python program.

    Also, you need to understand the import keyword, which allows you to import and use the definitions and functions available within a module.