Python Input Output (I/O) and Import

By | April 11, 2019

In this tutorial, we are going to discuss two main inbuilt functions in python that you often use in your code and we will also discuss a keyword import and why we use it.

Python Output print(), Input input(), and import

Table of content:

  • Python Output
  • String formatting
  • Python input
  • Import in python

In python, you will get a number of inbuilt functions which are used to perform specific tasks. In our previous examples, we have used print() function a lot, and you might be wondering that how does it work. In this article, we will cover two main functions print() and input().

Python print() function:

In python, if you want to show something on screen as an output, there we use the print() function. Using the print() function you can show the output data on your standard screen.

Normally print() function is used to show the output data of variables and strings.

Let’s understand it 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 would get their two outputs in two lines because the print() function has an additional keyword end inside it which has default value ‘\n’ which is used to print a new line. if you want you can alter the value of 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, that gives you more flexibility over the print() function. 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 string that pass in the format() function.

let’s understand it 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 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

Input in python using input() function

in the previous examples, we were assigning the values to the variable before we run the programme but what if we want value from the user rather than pre-defining it. Here we use the input() function which allows the user to input the values in the programme.

Here are something you must know before you use an input() function. Whenever you input a value in the programme with the help of input() function the value is treated as a string and what if you want to enter an integer or float for that you need to use typecasting that we have learned on the previous tutorial.

#input function syntax

variable = input([prompt])

here whatever value we enter it will store in the variable.

The prompt is a string which is used as a message that displays on the screen

Let’s understand it 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 which is used to import the definitions of the module (modules are the python files which contain the prewritten code or function) in the current file. Import keyword is used along with another keyword from which is used to import the module.

Suppose you want a programme which 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 need to import it on your current file by using the import keyword.

Let’s understand it with an example

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

#Output

Enter a Number: 10
3.1622776601683795

Points to remember:

  • To print two different data types using print function use comma to separate them
  • Use Type conversion to convert the data type of value which you are using from the input function.
  • from keyword is used to import the module
  • import keyword is used to import the definitions or function of the module.
  • There are many modules in the python
  • You can also install the third-party modules or libraries using the pip install command
  • Always write from and import keyword in the beginning lines of the code.

Python Tutorials


Introduction

Flow Control

Functions

Datatypes

File Handling

Object & Class

Additional Tutorials

 

Leave a Reply

Your email address will not be published. Required fields are marked *