Friday, 17 January 2025

Basic Input output operations

 1.3          Basic Input output operations

Python provides simple and intuitive ways to handle input and output (I/O) operations. These operations are essential for interacting with the user or handling data.


1. Input Operations

The input() function is used to get input from the user. By default, it returns the input as a string.

Syntax:

input(prompt)

Example:

name = input("Enter your name: ")
print(f"Hello, {name}!")
------------------------------------------------------------------------------------------------------------------------------
print('Enter your name:')
x = input()
print('Hello, ' + x)
------------------------------------------------------------------------------------------------------------------------------
# Taking input as string
x = input('Enter your name:')
print('Hello, ' + x)

Converting Input to Other Data Types:

Since input() returns a string, you often need to convert the input to another type:

# Taking input as int

# Typecasting to int

age = int(input("Enter your age: "))
# Typecasting to float
height = float(input("Enter your height: "))
print(f"You are {age} years old and {height} meters tall.")
------------------------------------------------------------------------------------------------------------------------------
Example : Taking two integers from users and adding them.

# Taking number 1 from user as int
num1 = int(input("Please Enter First Number: "))

# Taking number 2 from user as int
num2 = int(input("Please Enter Second Number: "))

# adding num1 and num2 and storing them in
# variable addition
addition = num1 + num2

# printing
print("The sum of the two given numbers is {} ".format(addition))
------------------------------------------------------------------------------------------------------------------------------
Example : Taking Two lists as input and appending them # Taking list1 input from user as list list1 = list(input("Please Enter Elements of list1: ")) # Taking list2 input from user as list list2 = list(input("Please Enter Elements of list2: ")) # appending list2 into list1 using .append function for i in list2: list1.append(i) # printing list1 print(list1)

2. Output Operations

The print() function is used to display output to the console.

Syntax:

print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
  • *objects: One or more objects to be printed.
  • sep: Separator between objects (default is a space).
  • En//;d: What to print at the end of the output (default is a newline).
  • file: Target output stream (default is sys.stdout).
  • flush: If True, forces the output to be flushed immediately.

Example:

print("Hello, World!")  # Prints Hello, World!
print("Python", "is", "fun", sep="-")  # Python-is-fun
print("This is the first line.", end=" ")
print("This is the same line.")

3. String Formatting in Output

a. Using f-strings (Python 3.6+)

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

b. Using .format() Method

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

c. Using %-Formatting (Old Style)

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

4. Reading and Writing Files

a. Reading a File

with open("example.txt", "r") as file:
    content = file.read()
    print(content)

b. Writing to a File

with open("example.txt", "w") as file:
    file.write("Hello, Python!")

c. Appending to a File

with open("example.txt", "a") as file:
    file.write("\nAppended text.")

5. Example: Basic Input and Output Program

# A simple program to calculate the square of a number
number = float(input("Enter a number: "))
square = number ** 2
print(f"The square of {number} is {square}.")

Key Points to Remember

1.    Input: Always comes in as a string, so convert it to the desired data type if necessary.

2.    Output: Use print() with optional formatting for better presentation.

3.    Files: Handle files using with to ensure proper closing of resources.

No comments:

Post a Comment

Desktop Virtualisation

Desktop Virtualization ( DV ) Desktop Virtualization ( DV ) is a technique that creates an illusion of a desktop provided to the user. It d...