Input and Output Statements

 In Python, the input statement is used to get user input. Here are some ways to use it:


1. _input()_: Reads a string from the user.

    - Example: `name = input("Enter your name: ")`

2. _int(input())_: Reads an integer from the user.

    - Example: `age = int(input("Enter your age: "))`

3. _float(input())_: Reads a floating-point number from the user.

    - Example: `height = float(input("Enter your height: "))`


Note:


- In Python 3.x, `input()` always returns a string.

- In Python 2.x, `input()` evaluates the input as Python code, while `raw_input()` returns a string.

You can also use various formatting options, such as:

- _prompt_: The text displayed to the user.

- _timeout_: The maximum time to wait for input.


Example: `name = input("Enter your name (or press Enter to quit): ")`


Input statements are essential for getting user input, making programs interactive, and allowing users to provide data for processing.

Additionally, you can use other functions to get specific types of input, such as:

- _sys.stdin.readline()_: Reads a line from standard input.

- _getpass.getpass()_: Reads a password from the user without echoing it.


In Python, output statements are used to display output to the screen or other devices. Here are some common output statements:

1. print(): Prints its argument to the console.

    - Example: `print("Hello, World!")`

2. print(f"{}"): Prints a formatted string.

    - Example: `print(f"Name: {name}, Age: {age}")`

3. sys.stdout.write(): Writes to the standard output stream.

    - Example: `import sys; sys.stdout.write("Hello, World!\n")`

Note:

- In Python 3.x, `print()` is a function, so you need to use parentheses.

- In Python 2.x, `print` is a statement, so you don't need parentheses.


You can also use various formatting options, such as:

- sep: Separator between arguments.

- end: Character to print at the end.

- file: Output stream.

Example: `print("Hello", "World", sep=", ", end="!\n")

Output statements are essential for displaying output, debugging, and logging in Python programs.

M.Sarulatha
Assistant Professor
Dept. of CA., JJC

Comments

Popular posts from this blog

Operating System Structure

Asymptotic Notations

ASP.NET Events Handling