Mangcoding

icon chat
Yayan Kurnia Akbar - Thursday, 13 March 2025 - 7 months ago

Python Learning : An Easy Way to Understand Input and Output with Practical Examples

single image

Python is one of the most popular programming languages thanks to its simple syntax and ease of use. When learning Python, beginners need to understand several core concepts.

One of the most important is input and output (I/O) processing, which allows programs to interact with users or external systems.

Link Mangcoding - Python Input and Output

What Is Input in Python?

First of all, input refers to data provided by the user or other sources. In Python, the input() function is used to capture user input from the keyboard. As a result, the data received is always returned as a string.

# Asking for input from the user
name= input("Enter your name: ")


# Display a message with the user input
print("Hello,", name, "! Welcome to the Python program.")

Output program:

 Pembelajaran Bahasa Pemrograman Python hasil_input - Python Input and Output - Mangcoding

In the example above, the user is prompted to enter their name. The input is stored in the variable name and then used to display a welcome message.

Link Mangcoding - Python Input and Output

Output in Python

Output in Python refers to the information or results displayed by the program to the user or stored in a medium, such as a text file or database. The primary function for printing output in Python is print(). This function is used to display text or variable values on the screen or other output sources.

# Display simple text
 print("Hello, world!")


# Display the value of the variable
age = 25 
print("My age is", age)

Output program:

Pembelajaran Bahasa Pemrograman Python hasil_output - Python Input and Output - Mangcoding

As a result, the program print the message ‘Hello, world!’ and the value of the variable age.

Link Mangcoding - Python Input and Output

Using Files in Input and Output

In addition to direct interaction with users, Python also supports I/O operations with files. To open a file, the open() function is used with the appropriate mode for reading or writing purposes.

# Open a file for writing
 with open("data.txt", "w") as file:
    file.write("This is an example of what is written in the file.") 


# Open a file for reading
with open("data.txt", "r") as file:
    isi_file = file.read()


print("File contents: ")
print(isi_file)

Pembelajaran Bahasa Pemrograman Python hasil_file - Python Input and Output - Mangcoding

As a result, the program writes text to the file ‘data.txt’ and then reads it back to display on the screen.

Link Mangcoding - Python Input and Output

Conclusion

Input and output are essential aspects of software development using Python. By using the input() and print() functions for user interaction, as well as the open() function for working with files, developers can create interactive programs that can handle various external data sources.

By understanding the fundamentals of input and output in Python, developers can build more useful and functional applications for their users.

That’s the explanation of Understanding Input and Output in Learning the Python Programming Language that Mangcoding can share. Hopefully, this article is useful and provides new insights for you. If you have constructive feedback or suggestions, feel free to leave a comment or contact us via email and Mangcoding’s social media.

Link Copied to Clipboard