Accepting input from Console

Accepting input from Console

input() Function

Syntax for input() is:

If prompt is present, it is displayed on monitor, after which the user can provide data from keyboard. Input takes whatever is typed from the keyboard and evaluates it. As the input provided is evaluated, it expects valid python expression. If the input provided is not correct then either syntax error or exception is raised by python.


x= input ("enter data:")
Enter data: 2+1/2.0
Will supply 2.5 to x

input ( ), is not so popular with python programmers as:

i) Exceptions are raised for non-well formed expressions.

ii) Sometimes well formed expression can wreak havoc.

Output is what program produces. In algorithm, it was represented by print. For output in Python we use print. We have already seen its usage in previous examples. Let's learn more about it.

Example 1:input( ) with prompt string

>>> city=input (“Enter Your City: ”)
Enter Your City: Madurai
>>> print (“I am from “, city)
I am from Madurai

Example 2:input( ) without prompt string

>>> city=input()
>>> print (I am from", city)
I am from Rajarajan

Example Program:

Python program to Input 2 Number and print its sum

x = int (input(“Enter Number 1: ”))
y = int (input(“Enter Number 2: ”))
print (“The sum = ”, x+y)


Enter Number 1: 34
Enter Number 2: 56
The sum = 90

Example 4: Alternate method for the above program

x,y=int (input("Enter Number 1 :")),int(input("Enter Number 2:"))
print ("X = ",x," Y = ",y)


Enter Number 1 :30
Enter Number 2:50
X = 30 Y = 50

Qus. When was Python released?

Ans- Python was released in February 1991.

Qus. Who developed Python?

Ans- Python was developed by Guido Van Rossum.

Qus. Which two languages contributed to Python as a Programming Language?

Ans- ABC language and Modula 3.

Qus. Is Python an Object Oriented Language?

Ans- Yes, Python is an object oriented language.

Qus. Python got its name from which show?

Ans- Python got its name from famous BBC comedy show “Monty Python’s Flying Circus”.

Qus. "Python is an interpreted language". What does it mean to you?

Ans- It means that the Python installation interprets and executes the code line by line at a time.

Qus. What does a cross platform language mean?

Ans- it means a language can run equally on variety of platforms-Windows, Linux/UNIX, Macintosh, Supercomputers, Smart phones etc.

Qus. Python is a Free and Open Source language. What do you understand by this feature?

Ans- It means Python is freely available without any cost. Its source code is also available. One can modify, improve/extend an open source software.

Qus. In how many ways, can you work in Python?

Ans-In two ways we can work in Python-

(i) Interactive mode

(ii) Script Mode

Qus. What is the difference between Interactive mode and Script Mode in Python?

Ans- In interactive mode, one command can run at a time and commands are not saved. Whereas in Script mode, we can save all the commands in the form of a program file and can see output of all lines together.

Qus. What are the advantages of Python?

Ans- Advantages-

i. Easy to use OO Language.

ii. Expressive Language.

iii. Interpreted Language.

iv. It is complete.

v. Cross-Platform Language.

vi. Free and Open Source.

vii. Variety of Usage/ Applications

Qus. What are the limitations of Python?

Ans- limitations-

i. Not the fastest language.

ii. Lesser Libraries than C, Java, Perl.

iii. Not strong on Type Binding.

iv. Not easily convertible.

Qus. Which of the following are not valid strings in Python?

(a)‖Hello‖ (b) ‗Hello‘ (c)‖Hello‘ (d) ‗Hello‖ (e) {Hello}

Ans- String (c) , (d) and (e ) are not valid strings.

Qus. Write Instructions in python to get the following result: (Do it in both interactive mode and script mode)

I am a student of KV Barabanki

I live in Barabanki

And I love Barabanki.

Barabanki is 20 KM away from Lucknow

This Place is famous for Dewa Sharif

Ans- In Interactive Mode-

>>> print("I am a student of KV Barabanki \nI live in Barabanki \nAnd I love Barabanki\n\n Barabanki is 20 KM away from Lucknow\n This Place is famous for Dewa Sharif")

In Script Mode-

print("I am a student of KV Barabanki \n I live in Barabanki \nAnd I love Barabanki\n\n Barabanki is 20 KM away from Lucknow\n This Place is famous for Dewa Sharif")

(save this code to a file and then run the file)

Qus. What is None literal in Python?

Ans: Python has one special literal, which is None. The None literal is used to indicate absence of value. It is also used to indicate the end of lists in Python. It means ―There is nothing here‖.

Qus. What is the difference between a keyword and an identifier?

Ans: Difference between Keyword and Identifier: Every language has keywords and identifiers, which are only understood by its compiler. Keywords are predefined reserved words, which possess special meaning. An identifier is a unique name given to a particular variable, function or label of class in the program.

Qus. What are literals in Python? How many types of Literals allowed in Python?

Ans: Literals: Python comes with some built-in objects. Some are used so often that Python has a quick way to make these objects, called literals.

The literals include the string, Unicode string, integer, float, long, list, tuple and dictionary types.

Qus. How many types of sequences are supported in Python?

Ans: Three Types of Sequences are supported in python:

(i) String

(ii) List

(iii) Tuple

Qus. What factors guide the choice of identifiers in program?


(i) An identifier must start with a letter or underscore followed by any number of digits and/or letters.

(ii) No reserved word or standard identifier should be used.

(iii) No special character (Other than underscore) should be included in the identifier.

Qus. What is the difference between an expression and a statement in Python?

Ans: A statement is an instruction that the Python interpreter can execute. We have only seen the assignment statement so far. Some other kinds of statements that we‘ll see shortly are while statements, forstatements, if statements, and import statements. (There are other kinds too!) An expression is a combination of values, variables, operators, and calls to functions. Expressions need to be evaluated. If you ask Python to print an expression, the interpreter evaluates the expression and displays the result.

Qus. What are tokens in Python? How many types of tokens allowed in Python?

Ans: Tokens are the smallest unit of the program. There are following tokens in Python:

  •  Reserved words or Keywords
  •  Identifiers
  •  Literals Definition of all tokens may come. Which is not given
  •  Operators in this question bank.
  •  Punctuators

Qus. What are data types? What are Python‟s built-in core data types?

Ans. Every value in Python has a datatype. Since everything is an object in Python programming, data types are actually classes and variables are instance (object) of these classes. There are various data types in Python. Some of the important types are listed below.

(i) Numbers (ii) String (iii) List (iv) Tuple (v) Dictionary

Qus. Which data types of Python handle Numbers?

Ans. It is cleared by name that Number data types are used to store numeric value in Python. The Numbers in Python have following core data types:

(i) Integers

a. Integers (signed)

b. Booleans

(ii) Floating-Point Numbers

(iii) Complex Numbers

Qus. Why is Boolean considered a subtype of Integers?

Ans: Because Boolean Values False and True behave like the values 0 and 1, respectively. So Boolean type is a subtype of plain integers.

Qus. What do you understand by term "immutable"?

Ans: Immutable types are those data types that can never change their value in place. In Python the following types are immutable:

(i) integers

(ii) floating-point numbers

(iii) Booleans

(iv) Strings

(v) Tuples

Qus. What are mutable and immutable types in Python? List both of them.

Ans. Mutable types means those data types whose values can be changed at the time of execution.

They are as follows:

  • Lists
  • Dictionaries
  • Sets

Immutable types are those data types that can never change their value in place. In Python the following types are immutable:

  • integers
  • floating-point numbers
  • Booleans
  • Strings
  • Tuples


python program to input a welcome message and print it

View Solution

python program to print number of seconds in year

View Solution

python program that display a joke but display the punch line only when the user presses enter key

View Solution

python program to generate the following output 5 10 9

View Solution

python program to print 5@10@9

View Solution

CCC Online Test 2021 CCC Practice Test Hindi Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level NIELIT Study material and Quiz Bank SSC Railway TET UPTET Question Bank career counselling in allahabad Sarkari Naukari Notification Best Website and Software Company in Allahabad Website development Company in Allahabad