Nulis yen statement ing siji baris karo operator ternary Python (operator kondisional)

Bisnis

Python nduweni gaya nulis sing disebut operator ternary (operator kondisional) sing bisa njlèntrèhaké proses kaya statement if ing baris siji.

Ing ngisor iki diterangake ing kene, bebarengan karo kode sampel.

  • Nulis dhasar saka operator ternary
  • if ... elif ... else ...Njlèntrèhaké iki ing siji baris
  • Gabungan Daftar Notasi Komprehensif lan Operator Ternary
  • Kombinasi fungsi anonim (ekspresi lambda) lan operator ternary

Waca artikel ing ngisor iki kanggo informasi luwih lengkap babagan pernyataan normal yen.

Nulis dhasar saka operator ternary

Ing Python, operator ternary bisa ditulis kaya ing ngisor iki

Expression evaluated when the conditional expression is true if conditional expression else Expression evaluated when the conditional expression is false

Yen sampeyan pengin ngoper nilai miturut kondisi, mung nulis saben nilai minangka ana.

Value to return if conditional expression is true if conditional expression else Value to return if conditional expression is false
a = 1
result = 'even' if a % 2 == 0 else 'odd'
print(result)
# odd

a = 2
result = 'even' if a % 2 == 0 else 'odd'
print(result)
# even

Yen sampeyan pengin ngalih pangolahan miturut kahanan, jelasake saben ekspresi.

a = 1
result = a * 2 if a % 2 == 0 else a * 3
print(result)
# 3

a = 2
result = a * 2 if a % 2 == 0 else a * 3
print(result)
# 4

Ekspresi sing ora ngasilake nilai (ekspresi sing ora ana) uga bisa ditampa. Gumantung ing kondisi, salah sawijining ekspresi dievaluasi lan proses dieksekusi.

a = 1
print('even') if a % 2 == 0 else print('odd')
# odd

Setara karo kode ing ngisor iki sing ditulis nganggo pernyataan normal if.

a = 1

if a % 2 == 0:
    print('even')
else:
    print('odd')
# odd

Ekspresi kondisional pirang-pirang uga bisa digabungake nggunakake operator logis (lan, utawa, lsp.).

a = -2
result = 'negative and even' if a < 0 and a % 2 == 0 else 'positive or odd'
print(result)
# negative and even

a = -1
result = 'negative and even' if a < 0 and a % 2 == 0 else 'positive or odd'
print(result)
# positive or odd

if ... elif ... else ...Katrangan siji-baris

if ... elif ... else ...Ora ana cara khusus kanggo nulis iki ing siji baris. Nanging, bisa diwujudake kanthi nggunakake operator ternary liyane ing ekspresi sing dievaluasi nalika ekspresi kondisional operator ternary palsu. Gambar operator ternary nesting.

Nanging, luwih becik ora digunakake sacara ekstensif amarga nyuda keterbacaan.

a = 2
result = 'negative' if a < 0 else 'positive' if a > 0 else 'zero'
print(result)
# positive

a = 0
result = 'negative' if a < 0 else 'positive' if a > 0 else 'zero'
print(result)
# zero

a = -2
result = 'negative' if a < 0 else 'positive' if a > 0 else 'zero'
print(result)
# negative

Ekspresi kondisional ing ngisor iki bisa diinterpretasikake kanthi rong cara, nanging dianggep minangka mantan (1).

A if condition 1 else B if condition 2 else C
1. A if condition 1 else ( B if condition 2 else C )
2. ( A if condition 1 else B ) if condition 2 else C 

Conto konkrit kaya ing ngisor iki. Ekspresi pisanan dianggep kaya sing kapindho.

a = -2
result = 'negative' if a < 0 else 'positive' if a > 0 else 'zero'
print(result)
# negative

result = 'negative' if a < 0 else ('positive' if a > 0 else 'zero')
print(result)
# negative

result = ('negative' if a < 0 else 'positive') if a > 0 else 'zero'
print(result)
# zero

Gabungan Daftar Notasi Komprehensif lan Operator Ternary

Panggunaan operator ternary sing migunani yaiku nalika ngolah dhaptar ing notasi pangerten dhaptar.

Kanthi nggabungake operator ternary lan notasi pangerten dhaptar, bisa ngganti unsur dhaptar utawa nindakake sawetara pangolahan liyane gumantung saka kahanan.

l = ['even' if i % 2 == 0 else i for i in range(10)]
print(l)
# ['even', 1, 'even', 3, 'even', 5, 'even', 7, 'even', 9]
l = [i * 10 if i % 2 == 0 else i for i in range(10)]
print(l)
# [0, 1, 20, 3, 40, 5, 60, 7, 80, 9]

Kanggo informasi luwih lengkap babagan notasi pangerten dhaptar, deleng artikel ing ngisor iki.

Kombinasi fungsi anonim (ekspresi lambda) lan operator ternary

Operator ternary, sing bisa diterangake kanthi ringkes sanajan ing fungsi anonim (ekspresi lambda), migunani.

get_odd_even = lambda x: 'even' if x % 2 == 0 else 'odd'

print(get_odd_even(1))
# odd

print(get_odd_even(2))
# even

Elinga, sanajan ora ana hubungane karo operator ternary, conto ing ndhuwur menehi jeneng kanggo ekspresi lambda. Mula, alat pamriksa otomatis kayata konvensi koding Python PEP8 bisa uga ngasilake Peringatan.

Iki amarga PEP8 nyaranake nggunakake def nalika nemtokake jeneng kanggo fungsi.

Konsep PEP8 kaya ing ngisor iki

  • Ekspresi Lambda digunakake kanggo ngirim obyek sing bisa diarani minangka argumen, contone, tanpa menehi jeneng
  • Ing ekspresi lambda, gunakake def kanggo nemtokake jeneng
Copied title and URL