SoFunction
Updated on 2025-04-13

Tutorial on input and output in Python

1. Print output function

1. Basic usage

print()Functions are used to output content to the console and support direct output of multiple data types.

print("Hello World")  # Output stringprint(2024)          # Output numbersprint(3.14)          # Output floating point number
# Output resultHello World
2024
3.14

2. Multi-parameter output

Multiple content can be output at the same time, separated by spaces by default

name = "Alice"
age = 25
print("Name:", name, "age:", age) 

# Output resultName: Alice age: 25

3. Format output

Format using f-string (recommended method)

price = 19.99
quantity = 3
print(f"Total price:{price * quantity:.2f}Yuan")

# Output resultTotal price:59.97Yuan

4. Line break control

Default automatic line wrapping, can be passedendParameter modification

print("First Line", end=" → ")
print("Second Line")

# Output resultFirst line → Line 2

2. Input input function

1. Basic usage

input()Functions are used to get user input and return string type

user_name = input("Please enter your name:")
print("Welcome,", user_name)
# Run example:Please enter your name:Wang Xiaoming
Welcome, Wang Xiaoming

2. Type conversion

The input content is a string by default, and the type needs to be converted explicitly.

age = int(input("Please enter age:"))
height = float(input("Please enter height (meter):"))
print(f"Type Verification:Age type{type(age)}, Height type{type(height)}")

# Enter test:Please enter your age:25
Please enter height(rice):1.75
Type Verification:Age type<class 'int'>, Height type<class 'float'>

3. Code comments

1. Single line comment

Use # for single line comments

# Calculate the area of ​​a circleradius = 5
area = 3.14 * radius ** 2  # Calculation formula

2. Multi-line comments

Use three consecutive quotes (single/double quotes are available)

'''
 This program implements functions:
 1. User login verification
 2. Show welcome message
 3. Record the login time
 '''
print("System initialization completed")

4. Best Practice Suggestions

Input and output specifications

  • Add validity verification to user input
  • Format output keeps information aligned
  • Sensitive information input is hidden using the getpass module

Comment specifications

  • Add explanatory comments in complex logic
  • Avoid writing meaningless comments (such as: a = 5 # assign a value of 5)
  • Functions/classes use document string description function
  • Update outdated comments in a timely manner

Debugging Tips

  • Add an identification prefix when debugging with print
print("[DEBUG] Current variable value:", var)
  • Remove the debugging print from the official code

V. Comprehensive application examples

# User information entry programname = input("Please enter your name:").strip()
while True:
    try:
        birth_year = int(input("Please enter your birth year:"))
        break
    except ValueError:
        print("Input error, please fill in the number year")

age = 2024 - birth_year
print(f"\nUser Information Summary:\nName:{name}\nage:{age}age")

Running example:
请输入Name:Li Fang
Please enter your year of birth:1995

User Information Summary:
Name:Li Fang
age:29age

By mastering these basic but important input and output methods and annotation specifications, you can write more readable and easy-to-maintain Python programs. In actual development, the unity of code style should be maintained according to project specifications.

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.