Python/input output

input , ouput

kimble2 2024. 8. 15. 07:41
반응형

Python's basic input and output operations are performed using the input() function for input and the print() function for output.

1. Input

  • input() function: This function allows the program to receive input from the user. It always captures the input as a string.

Example:

name = input("Enter your name: ")

In this code, the user's name is stored in the variable name.

2. Output

  • print() function: This function is used to display text or variables on the screen.

Example:

print("Hello, Python!")

 

This code prints the sentence "Hello, Python!" to the screen.

3. Example Using Both Input and Output

Below is a simple example where the user is asked to input two numbers, and the program calculates and displays their sum.

 

# Getting two numbers from the user
num1 = input("Enter the first number: ")
num2 = input("Enter the second number: ")

# Converting the input from strings to integers
num1 = int(num1)
num2 = int(num2)

# Calculating the sum
sum_result = num1 + num2

# Displaying the result
print("The sum of the two numbers is:", sum_result)

 

4. Key Points

  • Input: Since the input() function always captures input as a string, you'll need to convert it using int() or float() if you want to perform numerical calculations.
  • Output: The print() function allows you to print multiple items by separating them with a comma, and it automatically adds a space between them.

By using these basic input and output methods, you can easily create programs that interact with users.

반응형

'Python > input output' 카테고리의 다른 글

FILE input , output  (0) 2024.08.15