Lesson 1 4
Python Level Assessment
Lesson 2 2
Lesson 3 3
Lesson 4 3
Lesson 05 0No items in this section
Lesson 06 1
LESSON 1…PYTHON INTERMEDIATE
Python Input and Output to Handle User and File Input
This guide on Python input and output lets you know how to get input from the user, files, and display output on the screen, console, or write it into the file.
Goals of this article:
- Learn how to take input from a userand system In Python.
- Accept an integer, float, character, and string input from a user.
- Convert the user input to a different data type.
- Learn fancier output formatting.
Python Input function to accept input from a user
In Python, we have the following two functions to handle input from a user and system.
- input(prompt)to accept input from a user.
- print()to display output on the console.
Python 3 has a built-in function input() to accept user input.
In Python 2, to accept user input we can use the following two functions: –
The input() function reads a line entered on a console by an input device such as a keyboard and convert it into a string and returns it. As a new developer, It is essential to understand what is input in Python.
What is the input?
The Input is nothing but some value from a system or user. For example, if you want to perform an addition of two numbers on the calculator you need to provide two number to the calculator, those two number is nothing but an input provided by the user to a calculator program.
There are different types of Input, and that comes in various ways. For example: –
- Input stems from the keyboard. i.e., the user entered some value using a keyboard.
- Input Using Mouse Click or movement, i.e. you clicked on the radio button or some drop-down list and chosen an option from it.
In Python, there are various ways for reading input from the user from the command line environment or through the user interface. In both cases, the user is sending input from Keyboard or mouse.
Python example to accept input from a user
Let see how to accept employee data from a user using the input() function and display it using the print() function.
name = input(“Enter Employee Name “)
salary = input(“Enter salary “)
company = input(“Enter Company name “)
print(“Printing Employee Details”)
print(“Name”, “Salary”, “Company”)
print(name, salary, company)
Enter Employee Name Jon
Enter salary 12000
Enter Company name Google
Printing Employee Details
Name Salary Company
Jon 12000 Google
How input() function works in Python
Python input() function syntax
Here the prompt argument is optional. The prompt argument is used to display a message to the user. For example, the prompt is, “Please enter a value.” When input() function executes, program flow stops until a user enters some value.
Whatever you enter as input, the input() function converts it into a string. If you enter an integer value, still it will convert it into a string. If you want to number input from a user, you need to perform type conversion on the input value.
Let’s understand this with an example.
Program to check input type in Python
number = input(“Enter number “)
name = input(“Enter name “)
print(“Printing type of a input value”)
print(“type of number”, type(number))
print(“type of name”, type(name))
Enter number 26
Enter name Jessa
Printing type of a input value
type of number <class ‘str’>
type of name <class ‘str’>
As you know whatever you enter as input, the input() function always converts it into a string.
Accept an Integer input from User
Let’s see how to accept an integer value from a user in Python. We need to convert an input string value into an integer using a int() function.
# program to calculate addition of two input numbers
first_number = int(input(“Enter first number “))
second_number = int(input(“Enter second number “))
print(“First Number:”, first_number)
print(“Second Number:”, second_number)
sum1 = first_number + second_number
print(“Addition of two number is: “, sum1)
Enter first number 20
Enter second number 40
First Number: 20
Second Number: 40
Addition of two number is: 60
Note: As you can see, we explicitly added a cast of an integer type to an input function to convert an input value to the integer type.
Now if you print the type of first_number you should get integer type. type(first_number ) will return <class ‘int’>
Accept float input from User
Let’s see how to accept float value from a user in Python. You need to convert user input to the float number using the float() function as we did for the integer value.
float_number = float(input(“Enter float number “))
print(“input float number is: “, float_number)
print(“type is:”, type(float_number))
Enter float number 29.5
input float number is: 29.5
type is: <class ‘float’>
Simple Practice Problem
Accept one integer and one float number from the user and calculate the addition of both the numbers.
Exercise Question 1: Accept two numbers from the user and calculate multiplication
Exercise Question 2: Display “My Name Is James” as “My**Name**Is**James” using output formatting of a print() function
Use print() statement formatting to display ** separator between each word.
For example: print(‘My’, ‘Name’, ‘Is’, ‘James’) will display MyNameIsJames
So use one of the formatting argument of print() to turn the output into My**Name**Is**James