All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Python Read Keyboard Input

Last Updated : Mar 11, 2024

Python Read Keyboard Input

In this article we will show you the solution of python read keyboard input, receiving input from users is a means to communicate with them or gather information to produce a certain outcome.

Two built-in methods are available in Python to read data from the keyboard.

In all of the most recent iterations of Python, the input function is used.

After receiving user input, it evaluates the expression. The Python interpreter can tell whether a user entered a string, a number, or a list automatically.

The input() function takes input as a string by default, thus if we need to submit an input of an integer or float type, we must type cast the input() function.

We will now discuss the idea of how to read keyboard input with an example.

Step By Step Guide On Python Read Keyboard Input :-

Code 1

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

Code 2

value1 = input("Please enter first integer:\n")
value2 = input("Please enter second integer:\n")
v1 = int(value1)
v2 = int(value2)
choice = input("Enter 1 for addition.\nEnter 2 for subtraction.\nEnter 3 for Multiplication.:\n")
choice = int(choice)
if choice == 1:
    print(f'You entered {v1} and {v2} and their addition is {v1 + v2}')
elif choice == 2:
    print(f'You entered {v1} and {v2} and their subtraction is {v1 - v2}')
elif choice == 3:
    print(f'You entered {v1} and {v2} and their multiplication is {v1 * v2}')
else:
    print("Wrong Choice, terminating the program.")
  1. The following variables are first defined by the program: "name," "age," and "marks."
  2. User input is accepted using the "input()" method. The user's name is an input for the first "input()" function.
  3. The user's age is transformed into an integer type using the 'int()' method.
  4. The user's marks are transformed into a floating-point type using the 'float()' method.
  5. The output is shown using the "print()" method. The user's name, age, and marks are shown in the output along with the proper labels.

Conclusion :-

As a result, we have successfully learned how to read keyboard input with an example.

In this post, we learned how to retrieve user input in a number of formats using the built-in Python input() function.

We also looked at how to deal with mistakes and exceptions that might arise while gathering user input.

I hope this article on python read keyboard input helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪