Learn Python – How to take input in Python?- Basic and advance

Taking input is a way of have interaction with users, or get records to grant some result. Python affords two built-in methods to study the information from the keyboard. These strategies are given below.

input(prompt)

raw_input(prompt)

input()

The input characteristic is used in all modern day model of the Python. It takes the input from the user and then evaluates the expression. The Python interpreter robotically identifies the whether a user enter a string, a number, or a list. Let’s recognize the following example.

Example –

# Python program showing  
# a use of input()  
  
name = input("Enter your name: ")  
print(name)  

Output:

Enter your name: Devansh
Devansh

The Python interpreter will not execute further traces until the person enters the input.

Let’s understand another example.

Example – 2

# Python program showing  
# a use of input()  
name = input("Enter your name: ")  # String Input  
age = int(input("Enter your age: ")) # Integer Input  
marks = float(input("Enter your marks: ")) # Float Input  
print("The name is:", name)  
print("The age is:", age)  
print("The marks is:", marks)  

Output:

Enter your name: Johnson
Enter your age: 21
Enter your marks: 89
The name is: Johnson
The age is 21
The marks is: 89.0

Explanation:

By default, the input() feature takes input as a string so if we need to enter the integer or float type input then the input() characteristic ought to be type casted.

age = int(input("Enter your age: ")) # Integer Input  
marks = float(input("Enter your marks: ")) # Float Input  

We can see in the above code the place we type casted the user enter into int and float.

How input() function works?

The flow of the program has stopped until the user enters the input.

The text statement which also knows as prompt is optional to write in input() function. This prompt will display the message on the console.

The input() function automatically converts the user input into string. We need to explicitly convert the input using the type casting.

raw_input() – The raw_input function is used in Python’s older version like Python 2.x. It takes the input from the keyboard and return as a string. The Python 2.x doesn’t use much in the industry. Let’s understand the following example.

Example –

# Python program showing  
# a use of raw_input()  
  
name = raw_input("Enter your name : ")  
print name  

Output:

Enter your name: Peter
Peter

How to check Python version?

To test the Python version, open command line (Windows), shell (Mac), or terminal (Linux/Ubuntu) and run python -version. It will show the corresponding Python version.

Check Python version in the running script

We can test the Python version in our walking script. Consider the following approaches to understand Python version in all working systems.

Commands Operating System/Environment Output
Python –version or
Python -v or
Python -vv
Window/Mac/Linux Python 3.8.1
import sys
sys.version
sys.version_info
Python Script 3.8.3 (default, May 13 2020, 15:29:51) [MSC v.1915 64 bit (AMD64)]
Import platform
platform.python_version()
Python Script ‘3.8.1’

Let’s have a look at following image.