Praktikum Data Mining #2 : IF, ELIF, ELSE, WHILE, FOR, dan Fungsi pada Python

3 min read 3 days ago
Published on Sep 03, 2025 This response is partially generated with the help of AI. It may contain inaccuracies.

Table of Contents

Introduction

This tutorial provides a comprehensive guide to using conditional statements and loops in Python, specifically focusing on if, elif, else, while, and for statements, as well as functions. Understanding these concepts is essential for data mining and programming in Python, enabling you to create dynamic and efficient code.

Step 1: Understanding Conditional Statements

Conditional statements allow you to execute different pieces of code based on certain conditions.

Key Components:

  • if statement: Executes a block of code if a specified condition is true.
  • elif statement: Checks another condition if the previous if condition is false.
  • else statement: Executes a block of code if none of the preceding conditions are true.

Example Code:

x = 10
if x > 0:
    print("x is positive")
elif x == 0:
    print("x is zero")
else:
    print("x is negative")

Practical Tips:

  • Always ensure conditions are mutually exclusive when using elif and else.
  • Use parentheses for complex conditions to improve readability.

Step 2: Implementing Loops

Loops allow you to execute a block of code multiple times. There are two main types: while and for loops.

While Loop:

  • Continues until a specified condition becomes false.

Example Code:

count = 0
while count < 5:
    print(count)
    count += 1

For Loop:

  • Iterates over a sequence (like a list or a range).

Example Code:

for i in range(5):
    print(i)

Practical Tips:

  • Use break to exit a loop prematurely.
  • Use continue to skip the current iteration and move to the next.

Step 3: Defining and Using Functions

Functions are reusable blocks of code that perform a specific task. They help organize your code and avoid repetition.

Defining a Function:

  • Use the def keyword followed by the function name and parentheses.

Example Code:

def greet(name):
    print(f"Hello, {name}!")

greet("Alice")

Practical Tips:

  • Aim to keep functions small and focused on a single task.
  • Use descriptive names for better readability.

Step 4: Combining Concepts

You can combine conditional statements, loops, and functions to create more complex and powerful programs.

Example Code:

def check_numbers(nums):
    for num in nums:
        if num > 0:
            print(f"{num} is positive")
        elif num < 0:
            print(f"{num} is negative")
        else:
            print(f"{num} is zero")

check_numbers([-1, 0, 1, 2, -2])

Practical Tips:

  • Test your functions with different inputs to ensure they work as expected.
  • Use comments to document your code for future reference.

Conclusion

In this tutorial, you learned how to use conditional statements, loops, and functions in Python. These concepts are essential for writing efficient and effective data mining scripts. To further enhance your skills, try building small projects that incorporate these elements or explore more advanced topics like list comprehensions and error handling. Happy coding!