Take input in Python | Print input in Python

The print() function used to print in python:

In Python, we can print anything that we want using the print() function. The print function by default inserts a new line after every function call

a=2
b="Hello World"
c=8.545
print(a)
print(b)
print(c)
#This is a comment.

The ‘#’ is used to create comments in Python. Anything written after it is not executed. Comments are usually written so that the person who views the code can understand what a particular code snippet does.

Like, if we want to say that this portion prints a person’s name, then we can add a comment to it stating that the purpose of this code snippet is to print a person’s name.

Now, the output of the above code is-

2
Hello World
8.545

We can also write the three print functions as follows –

print(a,b,c)

And this will give us the same output as above.

Now, if we want to print the variable ‘a’  but don’t want a new line to be printed after it, we can do it in the following manner –

print(a,end=' ')

The input() function used to take input in python:

Now, let’s take a look at how to take input in python.

Suppose the user gives an input and we want to print it. We use the input() function for this. We save the input in a variable, say var_1, and then print it. Let the input be 345.

So,

var_1=input()
print(var_1)

And then, 345 will be printed.

Note: The input() function always returns a string, so if we want to store it as an integer or a float value in our variable, we need to do the casting.

So, if we want to save 345 as an integer, we will write int(input()) instead of just input().

The same is true when we want to store the value as a float variable. We will write float(input()) in place of input().

Saving and Running a Python file:

Let’s create a python file in sublime text and save it. Say, we write the following code –

print("My first Python program!")

and save it with a name Python_program.py . Python files have the extension ‘.py’ .

Now, if we want to run it, we will go to the Anaconda Terminal, and then go to the required address (using the commands told in the previous tutorials), and when we reach the folder which contains our python file, we will type – python ‘name of file’ and press enter.

So, in our case, we will type python Python_program.py, and hit enter. Voila, our program gets executed!

That’s it folks! In the next few tutorials, we will go through the basics of variables.

Take input in Python | Print input in Python