EZ

Eduzan

Learning Hub

Eduzan
Eduzan / Python

Python Basic Input and Output

Input and output operations are fundamental to Python programming, allowing programs to interact with users. The print() function displays information on the console, while the input() function captures user input.

Displaying Output in Python

The print() function in Python is the primary method to display output, including text, variables, and expressions.

Example:

print("Hello, World!")

Output:

Hello, World!

Printing Variables

You can print single or multiple variables, adding descriptive labels:

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

Output:

Name: Alice Age: 30

Format Output Handling in Python

Python offers several ways to format output, including the format() method, the sep and end parameters in print(), f-strings, and the % operator. Each method provides control over data display for enhanced readability.

  • Using format() method:
amount = 150.75
print("Amount: ${:.2f}".format(amount))

Output:

Amount: $150.75
  • Using sep and end parameters:
# Using 'end' to connect lines
print("Python", end='@')
print("Programming")

# Using 'sep' for separator
print('G', 'F', 'G', sep='')

# Date formatting example
print('09', '12', '2023', sep='-')

Output:

Python@Programming
GFG
09-12-2023
  • Using f-string:
name = 'Sam'
age = 25
print(f"Hello, My name is {name} and I'm {age} years old.")

Output:

Hello, My name is Sam and I'm 25 years old.
  • Using % operator for formatting:
num = int(input("Enter a value: "))
add = num + 5
print("The sum is %d" % add)

Output:

Enter a value: 10
The sum is 15

Taking Multiple Inputs

The split() method helps take multiple inputs in a single line, dividing the inputs into separate variables.

# Taking two inputs at a time
x, y = input("Enter two values: ").split()
print("Number of apples:", x)
print("Number of oranges:", y)

Output:

Enter two values: 3 5
Number of apples: 3
Number of oranges: 5

Conditional Input Handling

You can prompt users for input, convert it to a specific data type, and handle conditions based on that input.

age = int(input("Enter your age: "))
if age < 18:
    print("You are a minor.")
elif age < 65:
    print("You are an adult.")
else:
    print("You are a senior citizen.")

Output:

Enter your age: 22
You are an adult.

Converting Input Types

By default, the input() function reads user input as a string. Convert it to other types like int or float if needed.

  • Example to take string input:
color = input("What color is the sky?: ")
print(color)
  • Example to take integer input:
count = int(input("How many stars?: "))
print(count)
  • Example to take floating-point input:
price = float(input("Enter the price: "))
print(price)

Finding Data Type of a Variable

To determine the data type of a variable, use type().

Exanple:

a = "Hello"
b = 10
c = 12.5
d = ["apple", "banana"]
print(type(a))  # str
print(type(b))  # int
print(type(c))  # float
print(type(d))  # list

Output:

<class 'str'>
<class 'int'>
<class 'float'>
<class 'list'>
End of lesson.