Home Backend Development Python Tutorial The most concise way to write conditional judgment statements in Python

The most concise way to write conditional judgment statements in Python

Oct 17, 2016 am 11:54 AM

This article mainly introduces tips for returning true or false values ​​​​(True or False) in Python. This article discusses the most concise way to write conditional judgment statements. This article gives two concise writing methods. Friends who need it can refer to it

As follows A piece of code:

def isLen(strString):
    if len(strString)>6:
        return True
    else:
        return False
Copy after login

Maybe you have discovered that in Python 3 there is actually a way to complete the function in just one line:

The code is as follows:

>>> def isLen(strString):
       return True if len(strString)>6 else False
Copy after login

But. . . Could it be simpler?

How to use Python to express conditional statements more easily, just for fun :)

One way is to use list index:

The code is as follows:

>>> def isLen(strString):
       #这里注意false和true的位置, 多谢网友@小王的指正
       return [False,True][len(strString)>6]
Copy after login

The principle is very simple, the Boolean value True is evaluated as 1 by the index, and False is equal to 0. Can it be simpler

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article

Hot Article

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How Do I Use Beautiful Soup to Parse HTML? How Do I Use Beautiful Soup to Parse HTML? Mar 10, 2025 pm 06:54 PM

How Do I Use Beautiful Soup to Parse HTML?

Image Filtering in Python Image Filtering in Python Mar 03, 2025 am 09:44 AM

Image Filtering in Python

How to Download Files in Python How to Download Files in Python Mar 01, 2025 am 10:03 AM

How to Download Files in Python

How to Use Python to Find the Zipf Distribution of a Text File How to Use Python to Find the Zipf Distribution of a Text File Mar 05, 2025 am 09:58 AM

How to Use Python to Find the Zipf Distribution of a Text File

Intro to Flask: Adding a Contact Page Intro to Flask: Adding a Contact Page Feb 28, 2025 am 10:03 AM

Intro to Flask: Adding a Contact Page

How to Work With PDF Documents Using Python How to Work With PDF Documents Using Python Mar 02, 2025 am 09:54 AM

How to Work With PDF Documents Using Python

How to Cache Using Redis in Django Applications How to Cache Using Redis in Django Applications Mar 02, 2025 am 10:10 AM

How to Cache Using Redis in Django Applications

How to Perform Deep Learning with TensorFlow or PyTorch? How to Perform Deep Learning with TensorFlow or PyTorch? Mar 10, 2025 pm 06:52 PM

How to Perform Deep Learning with TensorFlow or PyTorch?

See all articles