PYTHON PROGRAMMING
P R E S E N TAT I O N
YIASCM
•
• Submitted By,
• Nihal P Jamal
• 22MBA47
• 23623
• Submitted To,
• Saloni Rafeeq
• Dept of MBA
W H AT I S I N P U T A N D O U T P U T ?
• In Python, input and output (I/O) refer to the processes of getting
information into a program (input) and producing results or displaying
information from a program (output).
• Input in Python:
• The input() function is used to take input from the user. It allows the
program to wait for the user to enter data, which is then stored as a
string.
• Example :-
• # Taking input from the user
• user_input = input("Enter something: ")
• # Displaying the input
• print("You entered:", user_input)
• In this example, the input() function prompts the user to enter something, and
the entered data is stored in the variable user_input. The print() function is
then used to display the entered data.
• It's important to note that the input() function returns a string. If you need to
work with numerical values, you might need to convert the input using functions
like int() or float().
• # Taking numerical input
• num = int(input("Enter a number: ")) print("You entered:", num)
• Output in Python:
• The print() function is used to produce output in Python. It can take one or more
arguments, which can be variables, strings, or expressions, and displays them on
the screen.
• # Printing a string
• print("Hello, World!")
• # Printing variables
• name = "Alice"
• age = 30
• print("Name:", name, "Age:", age)
• # Printing the result of an expression
• result = 5 + 7
• print("Result:", result)
• You can format output using different techniques
• These are basic concepts of input and output in Python. They are fundamental for
creating interactive programs where users can provide input, and the program can
produce meaningful output based on that input.
• Explain different types of input and outputs using exception handling ?
• Exception handling in Python allows you to manage errors and unexpected
situations gracefully. There are various types of input and output operations where
exception handling can be applied to enhance the robustness of your code.
• Input with Exception Handling:
• 1. Numeric Input:
• try:
• num = int(input("Enter a number: "))
• print("You entered:", num)
• except ValueError:
• print("Invalid input. Please enter a valid number.")
• Here, the int() function might raise a ValueError if the user enters something that
cannot be converted to an integer. The try block attempts to convert the input, and if
an exception occurs, it's caught in the except block.
• 2. File Input:
• try:
• with open("file.txt", "r") as file:
• content = file.read()
• print("File content:", content)
• except FileNotFoundError:
• print("File not found. Please provide a valid file name.")
• except IOError as e:
• print(f"Error reading the file: {e}")
• This example uses a try block to attempt reading content from a file. If the file is
not found or there is an IO error, the respective exceptions (FileNotFoundError and
IOError) are caught and handled.
• Output with Exception Handling:
• 1. Division Output:
• ry:
t
• result = 10 / 0
• print("Result:", result)
• except ZeroDivisionError:
• print("Error: Division by zero is not allowed.")
• Here, a division by zero operation in the try block raises a ZeroDivisionError. The
except block catches this error and provides a meaningful error message.
• 2.Writing to a File:
• data = "Some data to write to a file."
• try:
• with open("output.txt", "w") as file:
• file.write(data)
• print("Data written to the file successfully.")
• except IOError as e:
• print(f"Error writing to the file: {e}")
• This example demonstrates writing data to a file. If there's an error (e.g., due to lack
of write permissions), it is caught and handled.
• General Exception Handling:
• You can also catch a more general Exception if you want to handle multiple types of
errors in a similar way. However, it's generally better to be specific about the
exceptions you expect.
• try:
• # Some code that might raise an exception
• except Exception as e:
• # Handle the exception
• print(f"An error occurred: {e}")
• By using exception handling, you can make your code more resilient to errors and
provide better user experiences by gracefully handling unexpected situations. It also
helps in debugging and understanding the reasons behind failures.
Thank You