0% found this document useful (0 votes)
15 views6 pages

Py Chapter 2 Topic 1

ALLADI CLOUD TRAINING

Uploaded by

actnowcloud
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views6 pages

Py Chapter 2 Topic 1

ALLADI CLOUD TRAINING

Uploaded by

actnowcloud
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

INPUT & PRINT STATEMENTS IN PYTHON

Python provides several ways to handle input and output (I/O) operations. These
operations are fundamental in programming as they allow interaction between the
user and the program.

1. Input in Python

Python has a built-in function called input() that is used to take input from the
user. By default, the input is treated as a string, so you may need to convert it to
other data types like integers or floats if necessary.

Syntax:
input(prompt)

 prompt: A message that is displayed to the user before taking input.

Example:

name = input("Enter your name: ")


print("Hello, " + name)
Converting Input:

Since input() returns a string, you may need to convert it using functions like
int(), float(), etc.

age = int(input("Enter your age: ")) # Converts input


to an integer
height = float(input("Enter your height: ")) #
Converts input to a float

2. Output in Python

Python provides the print() function to output data to the screen.

Syntax:

print(*objects, sep=' ', end='\n', file=sys.stdout,


flush=False)

 *objects: The objects to be printed, separated by spaces by default.


 sep: The separator between objects (default is space).
 end: The string appended after the last object (default is a newline).
 file: Where the output goes (default is sys.stdout).
 flush: Forces the output stream to be flushed (default is False).

Basic Usage:

print("Hello, World!")
Printing Multiple Values:

name = "Alladi"
age = 25
print("Name:", name, "Age:", age)
Changing the Separator:

By default, Python separates items with a space. You can change this using the
sep parameter.
print("apple", "banana", "cherry", sep=", ")
# Output: apple, banana, cherry
Changing the End Character:

The end parameter defines what to print at the end of the output. By default, it's a
newline (\n).

print("Hello", end=" ")


print("World!")
# Output: Hello World!

3. Formatted Output in Python

Python offers various ways to format strings for output. The most common
methods are:

 Concatenation using +: You can concatenate strings with the + operator.

name = "Bob"
age = 30
print("Name: " + name + ", Age: " + str(age))

 Using str.format(): This method is more flexible than concatenation.

name = "Alladi"
age = 25
print("Name: {}, Age: {}".format(name, age))

 f-strings (Python 3.6+): The f-strings are a concise way to embed


expressions inside string literals.
name = "Charlie"
age = 22
print(f"Name: {name}, Age: {age}")
Formatting Numbers:

pi = 3.14159
print(f"Value of pi: {pi:.2f}")

# Output: Value of pi: 3.14

4. Input and Output with Files

Python can also handle I/O with files. You can read from or write to files using the
built-in open() function.

Opening a File:

file = open('example.txt', 'r') # Open file in read


mode

 'r': Read mode (default).


 'w': Write mode (creates a new file or overwrites an existing file).
 'a': Append mode (writes data at the end of the file).
 'b': Binary mode (for reading and writing binary files).

Reading from a File:

with open('example.txt', 'r') as file:


content = file.read()
print(content)
Writing to a File:

with open('example.txt', 'w') as file:


file.write("Hello, World!")

Examples

Example 1: Basic Input and Output

name = input("What is your name? ")


age = int(input("How old are you? "))
print(f"Hello {name}, you are {age} years old.")
Example 2: Formatting Output

name = "Alice"
balance = 1234.56
print(f"{name}, your account balance is:
${balance:,.2f}")
# Output: Alice, your account balance is: $1,234.56
Example 3: Reading and Writing Files

# Writing to a file
with open('data.txt', 'w') as file:
file.write("Python is fun!\n")

# Reading from a file


with open('data.txt', 'r') as file:
content = file.read()
print(content)

You might also like