You are on page 1of 9

PYTHON Programming (CST310)

Lecture 3: Taking input from Users

Department of Computer Science and Engineering


National Institute of Technology, Srinagar, Jammu and Kashmir
August 14, 2023
How to get Input from Users in Python
• A Program often have a need to interact with users, either to get data
or to provide some sort of result.
• In Python, Using the input() function, we take input from a user, and
using the print() function, we display output on the screen.

• Using the input() function, users can give any information to the
application in the string format.
In Python 3, we have the input() built-in functions to accept the input
from a user.
input(prompt):
The prompt argument is optional.
The prompt argument is used to display a message to the user.
For example,
input(“Please enter your name”)
When the input() function executes, the program waits until a user
enters some value.
Next, the user enters some value on the screen using a keyboard.

Finally, the input() function reads a value from the screen, converts it
into a string, and returns it to the calling program.
>>> input()

or

>>>print(“please enter your name”)


>>>input()

is equivalent to

>>>input(“please enter your name”)


Program to Accept Inputs From a User
Let see how to accept employee information from a user.
1. First, ask employee name, salary, and company name from the user
2. Next, we will assign the input provided by the user to the variables
3. Finally, we will use the print() function to display those variables on
the screen.
Program to Accept Inputs From a User
Let see how to accept employee information from a user.
1. First, ask employee name, salary, and company name from the user
2. Next, we will assign the input provided by the user to the variables
3. Finally, we will use the print() function to display those variables on
the screen.
Python Program to take two values as input
and printing their Sum
Python Program to take two values as input
and printing their Sum
a=input("Enter no1")
b=input("Enter no 2")
sum=a+b
print(sum) // it will print the concat of a and b
print(type(a)) // type of a will be string
print(type(b)) // type of b will be string
As you know whatever you enter as input, the input() function always
converts it into a string.
Type Casting:
Take an Integer Number as input from User
• We need to convert an input string value into an integer using an int()
function.
num1=int(input(“enter the number”)

You might also like