Python Dasar Part 2 Statemen dan Komentar(Untuk Mapel Informatika Kelas XI SMA/MA Kurikulum Merdeka)
Table of Contents
Introduction
This tutorial provides a foundational understanding of statements and comments in Python, as presented in the second part of the Python Basics series. It is particularly relevant for high school students studying Informatics under the Merdeka Curriculum. By the end of this guide, you will have a clear grasp of how to write Python statements and use comments effectively.
Step 1: Understanding Python Statements
Python statements are the building blocks of your code. They perform actions and control the flow of your program.
-
Types of Statements:
- Expression Statements: Execute an expression and return a value.
- Example:
print("Hello, World!")
- Example:
- Assignment Statements: Assign values to variables.
- Example:
x = 5
- Example:
- Control Flow Statements: Direct the flow of execution.
- Examples:
if
,for
,while
- Examples:
- Expression Statements: Execute an expression and return a value.
-
Practical Advice:
- Use clear and descriptive variable names for better code readability.
- Keep your statements simple to avoid confusion.
Step 2: Using Comments in Python
Comments are crucial for documenting your code and explaining its purpose to others (or yourself in the future).
-
Types of Comments:
- Single-Line Comments: Start with the
#
symbol.- Example:
# This is a single-line comment
- Example:
- Multi-Line Comments: Use triple quotes (
'''
or"""
).- Example:
""" This is a multi-line comment that spans multiple lines. """
- Example:
- Single-Line Comments: Start with the
-
Practical Tips:
- Use comments to explain complex logic or important sections of your code.
- Avoid over-commenting; it should enhance clarity, not clutter your code.
Step 3: Practice with Python Playground
You can experiment with Python code using the provided online playground.
-
Access the Playground:
- Go to the following link: Python Playground
-
Suggested Exercises:
- Write a simple program using print statements and comments.
- Create variables and assign values, then print them using formatted strings.
Conclusion
In this tutorial, you learned about Python statements and the importance of comments. These concepts are essential for writing clear and effective code. As you continue practicing, try to incorporate comments in your projects to enhance their readability and maintainability. For your next steps, explore more complex control flow statements and continue experimenting in the Python Playground. Happy coding!