Cara menggunakan switch case python

Until version 3.10, Python never had a feature that implemented what the switch statement does in other programming languages.

So, if you wanted to execute multiple conditional statements, you would've had to use the elif keyword like this:

age = 120

if age > 90:
    print["You are too old to party, granny."]
elif age < 0:
    print["You're yet to be born"]
elif age >= 18:
    print["You are allowed to party"]
else: 
    "You're too young to party"

# Output: You are too old to party, granny.

From version 3.10 upwards, Python has implemented a switch case feature called “structural pattern matching”. You can implement this feature with the match and case keywords.

Some people debate whether or not the match and case are keywords in Python. This is because you can use both of them as variable and function names. But that’s another story for another day.

You can refer to both keywords as "soft keywords" if you like.

In this article, I will show you how to write a switch statement in Python using the match and case keywords.

But before that, I have to show you how Python programmers used to simulate a switch statement back in the day.

How Python Programmers Used to Simulate Switch Case

There were multiple ways Pythonistas simulated switch statements back in the day.

Using a function and the elif keyword was one of them and you can do it this way:

def switch[lang]:
    if lang == "JavaScript":
        return "You can become a web developer."
    elif lang == "PHP":
        return "You can become a backend developer."
    elif lang == "Python":
        return "You can become a Data Scientist"
    elif lang == "Solidity":
        return "You can become a Blockchain developer."
    elif lang == "Java":
        return "You can become a mobile app developer"

print[switch["JavaScript"]]   
print[switch["PHP"]]   
print[switch["Java"]]  

"""
Output: 
You can become a web developer.
You can become a backend developer.
You can become a mobile app developer
"""

How to Implement Switch Statements with the match and case Keywords in Python 3.10

To write switch statements with the structural pattern matching feature, you can use the syntax below:

match term:
    case pattern-1:
         action-1
    case pattern-2:
         action-2
    case pattern-3:
         action-3
    case _:
        action-default

Note that the underscore symbol is what you use to define a default case for the switch statement in Python.

An example of a switch statement written with the match case syntax is shown below. It is a program that prints what you can become when you learn various programming languages:

lang = input["What's the programming language you want to learn? "]

match lang:
    case "JavaScript":
        print["You can become a web developer."]

    case "Python":
        print["You can become a Data Scientist"]

    case "PHP":
        print["You can become a backend developer"]
    
    case "Solidity":
        print["You can become a Blockchain developer"]

    case "Java":
        print["You can become a mobile app developer"]
    case _:
        print["The language doesn't matter, what matters is solving problems."]

That’s a much cleaner syntax than multiple elif statements and simulating the switch statement with a function.

You probably noticed I did not add a break keyword to each of the cases, as it is done in other programming languages. That’s the advantage Python’s native switch statement has over those of other languages. The break keyword's functionality is done for you behind the scenes.

Conclusion

This article showed you how to write switch statements with the “match” and “case” keywords. You also learned how Python programmers used to write it before version 3.10.

The Python match and case statements were implemented to provide the functionality that the switch statement feature in other programming languages such as JavaScript, PHP, C++, and others give us.

Thank you for reading.

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

Kolade Chris

Web developer and technical writer focusing on frontend technologies.

If you read this far, tweet to the author to show them you care. Tweet a thanks

Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started

Pada Python instruksi Case dan Switch tidak tersedia. Sebagai gantinya Python menyediakan instruksi ELIF yang dapat digabungkan penggunaannya dengan instruksi atau fungsi IF, sehingga dapat digunakan untuk pengambilan keputusan dalam kondisi dimana opsi yang tersedia banyak.


Sintaks Elif adalah sebagai berikut:


elif kondisi: instruksi


Elif adalah instruksi pelengkap If maka perhatikan penggunaannya ditempatkan setelah If. Perhatikan pada contoh berikut ini:

Apa itu Switch Case python?

2. Switch Case Pernyataan switch adalah pernyataan pilihan ganda. Setelah Sahabat DQ memberikan pilihan dan ekspresi yang relevan untuk setiap pilihan, Itu melihat melalui pilihan sampai menemukan pilihan yang cocok dengan ekspresi dan mengeksekusinya.

Apa fungsi dari switch case?

FUNGSI SWITCH CASE 1. Untuk membuat suatu menu pada sebuah bahasa pemrograman 2. Pengambilan keputusan yang melibatkan sejumlah atau banyak alternative 3. Sebagai penyederhana dari statement if – else. III. KEGUNAAN SWITCH CASE Statement switch-case digunakan sebagai intruksi pemilihan aksi yang akan dikerjakan.

Kapan kita memerlukan switch case?

Dari segi use case, apabila yang di cek hanya satu variabel dan hanya melakukan pengecekan apakah nilai variabel sama atau tidak dengan value tertentu, maka cocok menggunakan switch case.

Apakah IF dan Switch Case sama?

Perbedaan utama dari IF statement dengan switch-case adalah kondisi yang digunakan oleh IF dapat berupa range dan object sedangkan switch-case memiliki kondisi yang tetap.

Bài mới nhất

Chủ Đề