Docsity

Python Input Methods Tutorial, Lecture notes of Computer Programming

Toggle sidebar
bg1
Python Program Tutorial
University of the Philippines
How to take input in Python
Certain input is a way of interacting with the user or retrieving data to provide results. Python provides
two built-in methods for reading data from the keyboard. These methods are shown below.
- input(prompt)
- raw_input(prompt)
input()
Input functions are used in all recent versions of Python. Takes input from the user and evaluates
expressions. The Python interpreter automatically detects whether the user has entered a string,
number, or list. Let's understand the following example.
Example 1
Output:
The Python interpreter will not execute further lines until the user enters the input.
Let's understand another example.
Example 2
pf3
pf4

Partial preview of the text

Download Python Input Methods Tutorial and more Lecture notes Computer Programming in PDF only on Docsity!

Python Program Tutorial University of the Philippines How to take input in Python Certain input is a way of interacting with the user or retrieving data to provide results. Python provides two built-in methods for reading data from the keyboard. These methods are shown below.

  • input(prompt)
  • raw_input(prompt) input() Input functions are used in all recent versions of Python. Takes input from the user and evaluates expressions. The Python interpreter automatically detects whether the user has entered a string, number, or list. Let's understand the following example. Example 1 Output: The Python interpreter will not execute further lines until the user enters the input. Let's understand another example. Example 2

Output: Explanation: By default, the input() function accepts input as a string. So if you need to provide an input of type Integer or Float, you have to cast the input() function to that type. How input() function works?

  • Program flow stops until the user provides input.
  • Text statements, also known as prompts, can optionally appear in the input() function. This prompt - prints a message to the console.
  • The input() function automatically converts user input to a string. You have to explicitly convert the input using type casting.
  • raw_input() - The raw_input function is used in older versions of Python such as Python 2.x. Receives input from the keyboard and returns it as a string. Python 2.x is not widely used in the industry. Let's understand the following example. Example Output: How to check Python version? To check your Python version, open a command line (Windows), shell (Mac), or terminal (Linux/Ubuntu) and run python - version. The corresponding Python version is displayed.

Let's have a look at following image.