Python conditional statements

Source: Internet
Author: User

Python conditional statements
Python conditional statements

A Python Condition Statement is a code block that is determined by the execution result (True or False) of one or more statements.

You can briefly understand the execution process of conditional statements:

The Python programming language specifies that any non-0 and non-null (null) values are true, and 0 or null values are false.

In Python programming, if statements are used to control program execution. The basic form is:

If judgment condition: execution statement ...... Else: Execute the statement ......

When the "judgment condition" is set to a non-zero value, the following statement is executed, and the execution content can be multiple rows, which are indented to indicate the same range.

Else is an optional statement. You can execute related statements when the conditions are not met. The specific example is as follows:

#! /Usr/bin/python #-*-coding: UTF-8-*-# Example 1: if basic usage flag = Falsename = 'luren' if name = 'python ': # determine whether the variable is 'python' flag = True # When the condition is set, set the flag to True print 'Welcome boss' # and output the welcome information else: print name # output the variable name when the condition is not met

Output result:

>>> Luren # output result

If statements can be judged using> (greater than), <(less than), = (equal to), >=( greater than or equal to), <= (less than or equal) to indicate its relationship.

If the conditions are multiple values, the following form can be used:

If condition 1: Execute Statement 1 ...... Elif condition 2: Statement 2 ...... Elif Condition 3: Statement 3 ...... Else: Execute Statement 4 ......

Example:

#! /Usr/bin/python #-*-coding: UTF-8-*-# Example 2: elif usage num = 5 if num = 3: # determine the num value print 'bos' elif num = 2: print 'user' elif num = 1: print 'worker' elif num <0: # print 'error' else: print 'roadman' output when the value is less than zero # output when none of the conditions are met

Output result:

>>> Roadman # output result

Because python does not support switch statements, multiple condition judgments can only be implemented using elif. If multiple conditions need to be determined at the same time, you can use or ), it indicates that two conditions are successfully judged when one condition is set. When and (and) is used, it indicates that the condition is successful only when both conditions are set.

#! /Usr/bin/python #-*-coding: UTF-8-*-# Example 3: if statement multiple conditions num = 9if num> = 0 and num <= 10: # determine whether the value is 0 ~ Between 10 print 'hello' >>> hello # output result num = 10if num <0 or num> 10: # determine whether the value is smaller than 0 or greater than 10 print 'hello' else: print 'undefine '>>> undefine # output result num = 8 # determine whether the value is 0 ~ 5 or 10 ~ Between 15 if (num> = 0 and num <= 5) or (num> = 10 and num <= 15): print 'hello' else: print 'undefine' >>> undefine # output result

When there are multiple conditions, brackets can be used to differentiate the order of judgment. The judgment in parentheses is executed first. In addition, the priority of and or is lower than> (greater than) and <(less) equal judgment symbols, that is, if the value is greater than or less than the value without parentheses, it will give priority to the comparison or comparison.

Simple statement Group

You can also use the if condition judgment Statement at the same row position, as shown in the following example:

#! /Usr/bin/python #-*-coding: UTF-8-*-var = 100 if (var = 100 ): print "The var value is 100" print "Good bye! "

The output result of the above code execution is as follows:

The value of the variable var is 100 Good bye!

Related Article

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.