Python path (4) conditional statements, python path conditional statements

Source: Internet
Author: User

Python path (4) conditional statements, python path conditional statements

1. Here is an example of the Python conditional statement:



If there are multiple conditions, use elif (that is, else if). Let's take a look at the nested if block:




2. Comparison is indispensable for conditional statements. Let's take a look at the identity operator is:


The result is as follows:


Let's look at a similar example:



X is y indicates that x and y are the same object, and x = y indicates that x and y are the same.


3. in: Membership Operator



4. String comparison

"alpha" < "beta"

Returns True.


5. Comparison of Sequences

[1, 2] < [2, 1]

Returns True.

Further:

[2, [1, 4]] < [2, [1, 5]]

Returns True.


6. boolean operators and, or, not


Let's take a look at the short-circuit logic:



This means that if the statement before or is true (that is, user input), the name will return the value of the preceding statement or the name will be assigned as a string <unknown>.





Python statement del a [2: 4]

The python slicing operation starts from 0, and 0 is the first. You can regard [0] as the first.
>>> Word = 'heiipa'
>>> Word [0]
'H' # Here word [0] is the first place to display 'helpa'
>>> Word [0: 2]
>>> 'Hl '# [0: 2] indicates the first to second digits of 'helpa', that is, all characters from the first digit to the third digit are displayed, word [] so on'
Del is delete. You will understand it after careful consideration.

Python input statement

Input () supports expressions and numbers, but does not support strings. Raw_input () supports strings, such:
>>> X = input ("x = ")
X = 1 + 1
>>> Print x
2
>>> X = raw_input ("x = ")
X = 1 + 1
>>> Print x
1 + 1

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.