×

How to Take Input in Python

How To Take Input In Python

Python has various in-built functions that help in code redundancy. Let's discuss the usage of some functions-

  1. ascii() – it returns a readable representation of objects.
  2. format() – it inserts the specified value in the placeholder.
  3. globals() – it returns a dictionary of variables present in the global namespace.
  4. help() – it displays the documentation of modules.
  5. id() – it tells about the data type of variable.
  6. map() – it returns a map object of results after applying the function.
  7. filter() – it returns the filtered objects after testing whether items are acceptable or not.
  8. pow() – this function returns of value x raised to the power of y.
  9. sorted() – this function returns a sorted list of objects.
  10. input() – this function asks the user to input the value.

Here we will talk about the ways by which we can take input in python.

The two approaches we can use are-

  1. Using a prompt in the input function.
  2. Using the input function input().

In this program, we ask the user to input the age and then we will display it.

 a=input(“Enter age: ”)   
 print(“The age is {}”.format(a)) 

Once you have written the code, go to ‘File’ and click on ‘Save As’. Save your file as ‘input.py’.

How To Take Input In Python

When we give input in the output window, the input function takes that as a string (it doesn't matter whether you give an integer or float value). If we want to use an integer value for computation in our code, we must typecast it. Here we can check the data type of our variable (in python we refer to them as objects) using print(type(a)).

How To Take Input In Python

On executing our program, we can see that it belongs to the class 'string'.

How To Take Input In Python

To obtain our value in the integer type, we will typecast it as-

a=int(input(“Enter age: ”))

In this program, we have used placeholders { } in the print function. It holds the values that we want to display. The sequence of the same can be provided in the format function.

How To Take Input In Python

Now on running our program, we can see this time the class type is ‘integer’.

How To Take Input In Python

In the next approach, we will directly call the input function and ask the user to provide the value.

How To Take Input In Python

On executing this program, we will not get a message on the output window, Instead, we will see the blinking cursor on the output window so here we’ll input the value.

Here also we can observe the type is a string.

How To Take Input In Python

We can again typecast it and get our value in the integer type.

Input_age=int(input())

How To Take Input In Python

After typecasting we can observe that we can get the value in the 'integer' type.

How To Take Input In Python

Let’s have a look at few more examples where input() is used.

 p=int(input("Enter the principal: "))
 r=int(input("Enter the rate: "))
 t=int(input("Enter the time: "))
 si=p*r*t/100
 print("The value for simple interest is {}".format(si)) 
  • Python program to calculate the volume of a cylinder.
 pi=3.14
 r=int(input("Enter the value of radius: "))
 h=int(input("Enter the value of height: "))
 vol=pi*pow(r,2)*h
 print("Volume of cylinder is {}".format(vol)) 
  • Python program to perform addition, subtraction, division, and multiplication.
 num1=float(input("Enter number1: "))
 num2=float(input("Enter number2: "))
 add=num1+num2
 subt=num1-num2
 mult=num1*num2
 div=num1/num2
 print("Addition of two float numbers is {}".format(add))
 print("Subtraction of two float numbers is {}".format(subt))
 print("Multiplication of two float numbers is {}".format(mult))
 print("Division of two float numbers is {}".format(div)) 
  • Python program to calculate the area of a circle.
 pi=3.14
 r=int(input("Enter the value of radius: "))
 area=pi*r*r
 print("Area of circle is {}".format(area)) 
  • Python program to find the greatest of three numbers.
 a=int(input("Enter the value of 1st number: "))
 b=int(input("Enter the value of 2nd number: "))
 c=int(input("Enter the value of 3rd number: "))
 if(a>b and a>c):
     print("{} is greatest".format(a))
 elif(b>a and b>c):
     print("{} is greatest".format(b))
 else:
     print("{} is greatest".format(c)) 
  • Python program to understand type conversion.
 x=input("Enter the first number: ")
 y=input("Enter the second number: ")
 print(x+y)                #result is addition of two strings
 x=int(input("Enter the first number: "))
 y=int(input("Enter the second number: "))
 print(x+y)                 #result is addition of two numbers 
  • Python program to calculate distance between two points.
 x1=int(input("Enter the x-coordinate of first point: "))
 y1=int(input("Enter the y-coordinate of first point: "))
 x2=int(input("Enter the x-coordinate of second point: "))
 y2=int(input("Enter the x-coordinate of second point: "))
 dist=((x2-x1)**2+(y2-y1)**2)**0.5
 print("The calculated distance is {}".format(dist)) 

Related Topics

Augmented reality in python

In this article, we shall learn about augmented reality in python. Augmented reality is a technology that captures the world around you and adds virtual content or objects on top of...

3 minutes read.

Python Examples

In this tutorial, we will see some examples related to python. This will include some basic example questions and their code in Python. Write a program to print “Hello Python”. print(‘Hello Python’) Output Hello...

5 minutes read.

Python Dictionary copy() method

Python Dictionary copy() method The dictionary.copy () method in Python returns a copy of the specified dictionary. Syntax dictionary.copy () Parameter NA Return None Example 1 # Python program explaining # the dictionary.copy() method # initialising the dictionary ...

1 minute read.

GUI Calendar using Tkinter in Python

GUI: A graphical interface (GUI) is a user interface that lets users interact with electronic devices like computers and smartphones by using menus, icons, and other visual cues (graphics). In contrast...

3 minutes read.

Python Set add() Method

Python Set add() Method The set.add() method adds the specified element to a set. If the element is already present in the set, it doesn't add it. Syntax set.add(element) Parameter element- This parameter represents the element that...

1 minute read.

Latest Project Ideas using Python 2024

Python is one of the well-known languages for programming in the contemporary domain of computer science. The demand to adopt Python for IT purposes is steadily increasing because of its...

7 minutes read.

Defaultdict in Python

In this tutorial, we will study What is defaultdict in Python We will understand it with the aid of certain examples. Before this, we will have a look at dictionaries...

3 minutes read.

Python Simple Interest

Python is an Object-Oriented high-level language. Python has an English-like syntax, which is very easy to read and write codes. Python is an interpreted language which means that it uses...

3 minutes read.

Length of Tuple in Python

What is Tuple? Python is a data structure in a python programming language; it is the collection of the objects in a sequence. The tuples are immutable; that is, we cannot...

3 minutes read.

Standard Scaler in SKLearn

The sci kit learns in python is a library thatch is used in machine learning which is used to work on data modeling.It is only focused on the data modeling,...

4 minutes read.

Python Uses

Python has advanced in recent years to rank among the programming languages that are most often used globally. It is utilized in everything, including machine learning, software testing, and website...

4 minutes read.

Accuracy_score Function in Sklearn

A crucial stage in data science is measuring our model's performance using the appropriate metric. In this article, we will examine two methods for calculating the accuracy of your predictions:...

13 minutes read.

Exit program in python

Exit program in python The quit(), exit(), and sys.exit() functions have almost the same functionality as they raise the SystemExit exception by which the Python interpreter exit and no stack traceback...

2 minutes read.

Spotify API in Python

The application programming interface is referred to as API. In essence, an API serves as a layer of communication or, as the name suggests, an interface that enables systems to...

3 minutes read.

Python e-book free download

Python is a booming language these days. It has many applications for making code easier; it is also an open-source language. There are many sources to learn python. In this...

3 minutes read.

Python Dictionary popitem() method

Python Dictionary popitem() method The dictionary.popitem() method in Python removes the item that was last inserted into the dictionary. Syntax dictionary.popitem() Parameter NA Return This method returns an arbitrary element (key, value) pair from the given dictionary...

1 minute read.

Applications of Python

Top 10 Applications of Python in 2020- 2021 Python language is famous for its general-purpose nature, which enables developers to use it in every field of development. Python can be found...

6 minutes read.

Anonymous/Lambda Function in Python

Lambda keyword is used to declare an Anonymous function, i.e. a function that does not have any name. It is also called Anonymous functions. In python, normal functions are defined...

3 minutes read.

Poolmanager in Python

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

4 minutes read.

Python Os sep

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.