Input and output in python

 



Here’s a clear and simple explanation of Input and Output in Python 👍


🔹 Input in Python

To take input from the user, Python uses the input() function.

Syntax:

variable = input("Enter something: ")

Example:

name = input("Enter your name: ")
print("Hello,", name)

👉 Important:

input() always takes input as string.
If you want an integer or float, you must convert it:

age = int(input("Enter your age: "))
salary = float(input("Enter your salary: "))

🔹 Output in Python

To display output, Python uses the print() function.

Basic example:

print("Hello World")

Printing multiple values

name = "Meenu"
age = 22
print("Name:", name, "Age:", age)

Formatted Output (f-strings) – Best method

name = "Meenu"
age = 22
print(f"My name is {name} and I am {age} years old.")

🔹 Example Program (Input + Output)

name = input("Enter your name: ")
age = int(input("Enter your age: "))

print(f"Hello {name}, you are {age} years old.")

🔹 Summary

Task Function Example
Take input input() x = input("Enter: ")
Convert input int(), float() age = int(input())




Show output print() print("Hello")
Format output f-string print(f"Value = {x}")



Sure! Here is a more detailed explanation of Input and Output in Python with examples, tips, variations, and common interview questions.


INPUT & OUTPUT IN PYTHON – DETAILED NOTES

-------------------------------------------------

🔵 INPUT IN PYTHON

Python uses the input() function to take input from the user.


1. Basic Input

name = input("Enter your name: ")
print("Hello", name)

✔ What happens?

  • The message inside quotes is displayed to the user.
  • User types something and presses Enter.
  • That value is stored in name.

2. Input is Always a String

x = input("Enter a number: ")
print(type(x))   # str

Even if you enter 10, it is stored as "10" (string).


3. Converting Input

To integer

num = int(input("Enter an integer: "))

To float

price = float(input("Enter price: "))

4. Taking Multiple Inputs at Once

(i) Using split()

a, b = input("Enter two numbers: ").split()
print(a, b)

With integer conversion:

a, b = map(int, input("Enter two numbers: ").split())
print(a + b)

5. Taking Multiple Values in a List

numbers = list(map(int, input("Enter numbers: ").split()))
print(numbers)

-------------------------------------------------

🔵 OUTPUT IN PYTHON

Python uses the print() function for output.


1. Basic Output

print("Hello Python")

2. Printing Multiple Values

print("Name:", "Meenu", "Age:", 22)

3. Changing Separator Between Values

print("A", "B", "C", sep="-")
# Output: A-B-C

4. Changing End Character

print("Hello", end=" ")
print("World")
# Output: Hello World

5. Formatted Output (f-strings)

Recommended method

name = "Meenu"
age = 22
print(f"My name is {name} and my age is {age}.")

6. Using format()

print("Name: {} Age: {}".format("Meenu", 22))

7. Printing a variable with text

x = 50
print("Value of x =", x)

-------------------------------------------------

REAL EXAMPLES (Input + Output)

Example 1: Add Two Numbers

a = int(input("Enter first number: "))
b = int(input("Enter second number: "))

print("Sum =", a + b)

Example 2: Check Even or Odd

num = int(input("Enter a number: "))

if num % 2 == 0:
    print("Even")
else:
    print("Odd")

Example 3: Salary Slip Output

name = input("Enter your name: ")
salary = float(input("Enter Salary: "))

print(f"Employee {name} has a salary of ₹{salary}")

-------------------------------------------------

IMPORTANT NOTES

  • input() → always string
  • Use int() / float() to convert
  • print() can use:
    • sep=
    • end=
    • f-string
    • format()

INTERVIEW QUESTIONS

  1. What is the default data type of input()?
  2. How do you take multiple inputs in one line?
  3. Explain sep and end in print().
  4. Write a program to read 5 integers and store in a list.
  5. Difference between:
    • input()
    • raw_input() (Python 2 only)



Post a Comment

0 Comments