×

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

Python Generator

Python Generator: A Function is said to be a Python Generator that produces or generates a sequence of results. A Python Generator maintains its native state to work so that...

6 minutes read.

Matrix List Comprehension in Python

Introduction One of Python's most beautiful features is list comprehension. Iterating over an iterable object to create lists is a clever and succinct method. Nested List or matrix list Comprehensions, which...

6 minutes read.

Python Algorithms

A quote goes, "A goal without a plan is just a wish." To achieve anything in life, we can't simply go for it without making a plan and sticking to...

4 minutes read.

Python NewLine

In python, a new line character denoted by "\n" is known as an escape character or escape sequence, and it will force the cursor to change its position to the next...

6 minutes read.

Python sklearn train_test_split

Sklearn: One of the most beneficial open-source libraries for learning algorithms in Python is, without a doubt, Sklearn or Scikit-Learn. The most effective tools for statistical modeling and machine learning are all included in...

6 minutes read.

Reverse a sentence In Python

Introduction The built-in reverse() function is not supported by the Python string library. As we know, a Python string is defined as a set of Unicode characters and Python provides various...

4 minutes read.

Flutter Python

The flutter is a frame work available in the python programming language, to create web applications, mobile apps. The flutter is generally used for the development of the backend of...

3 minutes read.

What is Sleeping Time in Python

Did you ever postpone a Python program's execution? Usually, you want your code to execute as quickly as possible. But there are times when it is in your best interests...

3 minutes read.

Hog Descriptor Opencv Python

Python programming language: 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...

3 minutes read.

Python Coroutine

Introduction In Python, Coroutines are defined as the special type of function that freely allows control to its caller without losing the state. Coroutines and generates are similar but coroutines consist...

3 minutes read.

Python Image Processing

What is an Image? Images are the pictures that will define the world, and it has their own story, and consists of information about them and these are useful in many...

9 minutes read.

Best resources to learn Numpy and Pandas in python

In this tutorial, we will discuss the different resources where we can learn about NumPy and Pandas libraries of Python in a more efficient way. NumPy NumPy, a Python library used for...

4 minutes read.

Base Case in Recursive function python

In this article, we will learn about Python's base case in a recursive function. Before learning this, let’s first understand what recursion is in Python and the use of recursion in...

6 minutes read.

Python System Requirements

Introduction As we know, Python is a popular programming language usually used to write scripts for operating systems. It’s handy adequate for utilizing in web development and application design. In this article,...

2 minutes read.

Python String capitalize() method

Python String capitalize() method The string.capitalize() method in Python returns a copy of the string with only its first character capitalized. Syntax string.capitalize() Parameter NA Return This function returns a string where the first character is upper...

1 minute read.

Python String count() method

Python String count() method The string.count() method returns the number of times a specified value appears in the string. Syntax string.count(sub[, start[, end]]) Parameter sub: This parameter represents a substring to be searched. start: This parameter...

1 minute read.

Spell Corrector GUI 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.

Linear Regression using Sklearn with Example

This article will examine Python's global, local and non-local variables and show you how to use them to write code without problems. Let's quickly review what a variable in Python is...

8 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.

Palindrome In Python

What is Palindrome? A Palindrome can be defined as the number or a string that resides unchanged when it is reversed. Example: 14341 Output: Yes, this is a Palindrome number Example: RACECAR Output: Yes, this...

2 minutes read.