Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Dùng Python Ternary Operator không có else

Muốn viết gọn if condition: some-code thành 1 dòng:

  1. Single-line if: if condition: some-code
  2. Short-circuit and: condition and some-code. Nếu condition False, RHS không evaluate. Nếu True, RHS chạy.