Forget about if-elif-else with this Useful Python Tip

preview_player
Показать описание

I use something like the case operator but in Python. Python tips and tricks.
#shorts #python #programming #coding
Рекомендации по теме
Комментарии
Автор

You can.
Match x:
case y:
Code
Case z:
Code

mohsensali
Автор

Yes the case is possible in python 3.10

duttybwoy
Автор

you can use lists in this case, it saves place and easier to read

skoowshot
Автор

U can just do
x = int(input('Enter X: '))
print(f'x = {x}')

Mr_Ioannis
Автор

[PEP 636] That implements match pattern in Python 3.10, but it was design to proposed syntax for pattern matching.

It is good for finding string patterns but does not make it faster than the if statement, in fact is slower then if statment.

Unlike [switch/case] that is O(1), the [match/case] is O(n) where n is the cases you have.

Using a hash function its not the ideal thing to do, can use up resources like ram unnecessarily. can be useful in cases that you have a huge amount of check that you can avoid by using a hash function, but in cases that are only few check, you will only waste your resources.

martim
Автор

You can use match-case conditions after 3.10

match varName:
case valueOfVar:
Code
case valueOfVar:
code

And so on..

anshulbadhani
Автор

if x in xrange(1, 3):
print(f"x = {x}")
else:
print("another value")

# lol

thegate
Автор

PEP364 defines Structural Pattern Matching which can implement a switch statement in python 3.10 +

bryanhutson
Автор

Match != switch!!!! Match uses structural pattern matching, not hasing

GabrielsEpicLifeofGoals
Автор

Python 3.10 now has match case (switch case equivalent)

taranaditya
Автор

First time seeing this. Is this similar to switch cases in C#?

santiagocanepa
Автор

It's like object literals of the python world

FS-yqef
Автор

The original if statement was much easier to read tho

petrlaskevic
Автор

It is possible in Python 3.10+ to use switch

svgaming
Автор

Bruh 🙄 . U the answer came from if else condition, not from cases 🙄

lastwish
visit shbcf.ru