Python-based if statement

Source: Internet
Author: User

Python's if Statement general format
if <test1>:    <do something>elif:    <do something>else:    <do something>
    • The condition of the IF statement can be expressed by > (greater than), < (less than), = = (equals), >= (greater than or equal), <= (less than or equal) to indicate its relationship

Some examples

Example 1:

# -*- coding: UTF-8 -*-flag = Falsename = ‘luren‘if name == ‘python‘:            flag = True              print ‘welcome boss‘    else:    print name 输出结果为:>>> luren

Example 2:

# -*- coding: UTF-8 -*-num = 5     if num == 3:                print ‘boss‘        elif num == 2:    print ‘user‘elif num == 1:    print ‘worker‘elif num < 0:               print ‘error‘else:    print ‘roadman‘输出结果为:>>> roadman

Example 3:

# -*- coding: UTF-8 -*-num = 9if num >= 0 and num <= 10:        print ‘hello‘else:    print ‘undefine‘输出结果为:>>> hello
If-else Multi-wording

Cases:

a, b = 1, 21.常规if a>b:    c = aelse:    c = bprint c2.表达式c = a if a>b else bprint c3. 二维列表c = [b,a][a>b]print c

Python-based if statement

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.