Chapter 3.0 Basic Input-Output in Python - Technology369kk

 Introduction Basic I/O  Python

In Python, I/O stands for Input and Output, which are essential for interacting with users or other systems. Here's a simple overview of how Python handles these operations:

1. Input in Python: 

The input() function is used to take input from the user during program execution. It always returns the input as a string.

Example : Basic Input : 


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


Example : Taking Numeric Input:  

age = int(input("Enter your age: "))
print("You are", age, "years old.")


2. Output in Python:  

The print() function is used to display output on the screen. It can print strings, numbers, or any other object.

print("Welcome to Python Programming!")


Printing Multiple Items: 


name = "Alice"
age = 25
print("Name:", name, "Age:", age)


3. String Formatting for Output:  

You can format your output using different methods.

  • Example :  f-strings (Python 3.6+):

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


  • Example : str.format() method:

print("My name is {} and I am {} years old.".format(name, age))


  • Example: % formatting:


print("My name is %s and I am %d years old." % (name, age))


4. File Input and Output: 

Python also allows you to read from and write to files.

  • Example:  Write a to file 


    with open("example.txt", "w") as file:
    file.write("Hello, this is a test file.\n")
    file.write("This is the second line.")


  • Example : Reading from a file 
with open("example.txt", "r") as file:
    content = file.read()
    print(content)


5. Common Use Cases: 

  • Taking multiple inputs in one line: 


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


  • Reading and writing lists:

numbers = list(map(int, input("Enter numbers separated by space: ").split()))
print("Numbers:", numbers)



Conclusion: 

  • Input (input()): Takes input from the user.
  • Output (print()): Displays output to the screen.
  • File I/O: Reads from and writes to files for persistent storage.
  •  

    Hope this chapter is helpful for you , now i want to talking about next chapter is really very important chapter, all the best for your next chapter. 






    Post a Comment

    0 Comments