Learn to use and interpret input from the user - Python

Learn to use and interpret input from the user - Python

What is an interpreter in Python?

An interpreter is a program that peruses and executes code. This includes source code, pre-compiled code, and scripts.

How do I run Python interpreter?

Run the Python command-line interpreter, under your OS of choice,

  1. Open Command line: Start menu -> Run and type cmd.
  2. Type: C:\python27\python.exe.
  3. Note: This is the default path for Python 2.7. If you are using a computer where Python is not installed in this path, change the path accordingly.

blog1.PNG

What is user input in python?

Python user input from the keyboard can be read using the input() built-in function. The input from the user is read as a string and can be assigned to a variable. After entering the value from the keyboard, we have to press the “Enter” button. Then the input() function reads the value entered by the user.

Example program that shows interpreter input from user.

interpreter.py blog1.2.PNG blog1.1.PNG In above example,

  • main() Function, here we creating a 2 empty lists with separate variables called num_tokens = [], str_tokens = []
  • Next created a variable which stores user input and user input is both num and string separated by a delimiter | as shown in above picture.
  • split is a python inbuilt method which helps in splitting user_data into lists so here we separate the user_data whenever we find a delimiter | .
  • Now we are separating numeric data and string data with the help of the python inbuilt method isnumeric().
  • isnumeric() helps us to know whether the user_data is numeric or not so it just say ture or false.
  • strip() method is python inbuilt function, we are using here to remove the unwanted spaces in user_data.
  • Next if we found it is numeric then we are appending it to num_tokes list if not numeric the we are appending it to str_token list.
  • append() is a python inbuilt method which adds an item into end of the list.
  • Finally we are printing length of str_tokens and length of num_tokens and calling main() function in the End.

Sample output: Insert Delimited Data: hfkh | jfyjfj| 4376 | 78 | jfb | 45834686 String Tokens: 3 , Numeric Tokens: 3

#interpreter.py is a script which takes input
#of arbitrary length, separated by a delimiter
#and identifies the number and type of tokens

#Delimiter |

def main():
    num_tokens = []
    str_tokens = []
    user_data = input("Insert Delimited Data: ")
    split_data = user_data.split(sep="|")
    for i in split_data:
        if i.strip().isnumeric():
            num_tokens.append(i)
        else:
            str_tokens.append(i)

    print("String Tokens: {}\nNumeric Tokens: {}".format(len(str_tokens), len(num_tokens)))
    return

main(

Summary:

In Python we can have both static and dynamic inputs that can be performed by python inbuilt function called input() and This function prompts the user for an input from the keyboard. user can give input in program itself that is static and if program asks input for user when program runs is called dynamic.