If statement of python3

Source: Internet
Author: User

If statement of python3
For more information, please visit my website or csdn or blog

This section describes the conditions and usage of pythonif. The following is a previous article;


Introduction to assignment statements in python3
Introduction to print, import, and input in python3
Python dictionary
Python entry string
Python entry list and metadata
Software Installation for getting started with python

Quick introduction to statements

A fast statement is a set of statements formed by indentation;
The indentation character that can be used: TAb creation (equivalent to four spaces), four spaces, and eight spaces, which can be multiplied to form nested statements quickly;
Generally, four spaces are the most standard.
Function: a statement is a group of statements that are executed (if) or multiple times (loops) when the condition is true;

# Pseudocode this is a linethis is a condition: this is a block... there wo escaped the inner block
Introduction to boolean conditions and condition statements

False: the value of false:
FALSE, None, 0, ''(no space)," "(no space), (), [], {}
True: all other values are considered true:
The test is as follows:

# False >>> bool (False) False >>>> bool (None) False >>> bool ('') False >>>> bool () False >>> bool () false >>> bool ([]) False >>>> bool () False >>> bool ({}) False >>># True >>> bool (True) true >>> bool ('ddd ') True >>> bool (1) True >>> bool ([1]) True >>> bool (1 )) true >>> bool ({1}) True >>>
Conditional execution and if statement

If statements can implement conditional execution. if the condition is true, the subsequent statement blocks are executed. if the condition is false, the statement blocks are not executed.

# If the user input is 0-9, print True >>> x = int (input (Please enter an integer in 0-9) Please enter an integer in 0-9 6if 0
  
Else statement

Else clause (called a clause because it is not an independent statement and can only be part of an if statement) is executed when the condition is not met;

# If the user input is 0-9, True is printed. If the input is not False, >>> x = int (input (Please enter an integer in 0-9 )) please enter an integer in 0-9 11 >>> if 0 
Elif statement

If you need to check multiple conditions, you can use elif. It is short for "else if" and is also used together by the if and else clauses -- that is, the conditional else clause.

# Print in 0-9 if the user input is 0-9:; otherwise, print> 9 If the output is greater than 9:; otherwise, print: <0 >>> x = int (input (Please enter an integer in 0-9) Please enter an integer in 0-9-1 >>> if 0  9:... print (> 9)... else:... print (<0)... <0> 
Nested if statement

The if statement can be nested with the if statement:

x=int(input(Please enter an integer in 0-9 ))if x>=0: if x>9: print(x>9) elif x==0: print(x==0)else: print('x<0')Please enter an integer in 0-9 0x==0
More complex conditions: List of conditional operators:
# Comparison operator x = y x equals yx  Y x is greater than yx> = y x is greater than or equal to yx <= y x is less than or equal to yx! = Y x is not equal to ya  

Description of identity operators:
It seems like = is actually different. Only two objects bound to the same object are true (of course, when it is a numeric variable, it is the same as = ):

# A and B point to the same object from time to time >>>> a = [1, 2] >>> a is bFalse # a and B point to the same object >>> a = B = [1, 2] >>> a is bTrue

In member qualification operator description

# Members return true in the sequence> a = [1, 2, 3] >>> B = 1 >>> c = 4 >>> B in aTrue >>> c in aFalse >>>
Logical operators:

And: The result returned by x and y determines the value of the expression result. If x is true, y determines the result and returns y. If x is false, x determines that the result is false and returns x.
Or: x or y returns the value that determines the expression result like and.
Not: returns the opposite value of the expression result ". If the expression result is true, false is returned. If the expression result is false, true is returned.

>>> a=1>>> b=2>>> a and b2>>> a=1>>> b=0>>> a and b0>>> a or b1>>> not bTrue>>> 
Introduction to assertions

The if statement has a very useful close relation, and its working method is somewhat like the following (pseudo code ):
If not condition:
Crash program
This is because it is better to let the program crash later than to let it crash when the error condition appears. In general, you can require certain conditions to be true. The keyword used in the statement is assert.
If you need to make sure that a certain condition in the program is true for the program to work normally, the assert statement is useful. You can set the checkpoint in the program:
After the condition, you can add a string (separated by a comma) to explain the assertion:

>>> age=-1>>> assert 0, line 1, in  AssertionError: The age must be realistic>>> >>> age=10>>> assert 0>> 

 

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.