Python3 If statement

Source: Internet
Author: User

Get more information Welcome to my website or csdn or blog park

This section focuses on pythonif conditional statements, and how to use them. The following is attached to the previous article;


Python3 Introduction to the assignment statement
Python3 Introduction to the Print,import,input
A dictionary of getting started with Python
A string of getting started with Python
Python Getting Started list and tuples
Software installation for getting started with Python

Statement Quick Introduction

A statement is not a statement, it is a set of statements formed by indentation;
You can use the Indentation: tab to build (equivalent to four spaces), 4 spaces, 8 spaces, you can multiply the formation of nested statements faster;
Generally speaking, using four spaces is the most standard.
Function: A statement is fast when the condition is true (if) or a set of statements executed multiple times (loops);

#伪代码thisis a linethisis a condition:    thisis a block    .....there wo escaped the inner block
Introduction to Conditional and conditional statement Boolean type

False (FALSE): It is considered a false value:
FALSE, None, 0, ' (no spaces), "" (No spaces), (), [], {}
True (True): the others are judged to be true:
The test is as follows:

#假>>>boolFalse)False>>>boolNone)False>>>bool"')False>>>bool"")False>>>BOOL ()False>>>BOOL ([])False>>>BOOL (())False>>>BOOL ({})False>>>#真>>>boolTrue)True>>>bool' DDD ')True>>>bool1)True>>>BOOL ([1])True>>>BOOL ((1))True>>>BOOL ({1})True>>>
Conditional execution And if statement if statement

If statement can implement conditional execution, if the condition is determined to be true, then the following statement block execution, if the condition is false, the statement block will not be executed.

#如果用户输入:0-9 就打印True>>> x=int(input("Please enter an integer in 0-9 "in0-96if0<x<9:    print(True)True
Else statement

ELSE clause (a clause is called because it is not a separate statement, but only as part of the IF statement) when the condition is not satisfied;

#如果用户输入:0-9 就打印True,不在之类输出 False>>> x=int(input("Please enter an integer in 0-9 "in0-911if0<x<9:...     print(True)...else:...     print(False)... False
elif statements

If you need to examine multiple conditions, you can use Elif, which is shorthand for "else if", and is also used jointly with the IF and else clauses-that is, the ELSE clause with the condition.

#如果用户输入在0-9: Prints in 0-9, otherwise if the output is greater than 9: print >9, otherwise print: <0>>> X=int (Input ("Please enter a integer in 0-9")) Please enter an integerinch 0-9-1>>>if 0<x<9:...Print"in 0-9")...Elif x>9:...Print">9")... Else:...Print"<0")...<0>>>
Nested IF statements

If statements can be nested using 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‘in0-90x==0
More complex conditions: a list of conditional operators:
#比较运算符x==y x等于yx<y x小于yx>y x大于yx>=y x大于等于yx<=y x小于等于yx!=y x不等于ya<x<b  x大于a小于b#同一性运算符isisnot y x和y是不同的对象#成员资格运算符innotin y x不是y容器的成员

Description of the identity operator:
As is true when you see the same as = =, only two are bound to the same object (and of course, when the value variable is the same as = =):

#a与b不时指向同一对象>>> a=[1,2]>>> b=[1,2]>>> is bFalse#a与b指向同一对象>>> a=b=[1,2]>>> is bTrue

In the membership operator description

#成员在序列中返回真>>> a=[1,2,3]>>> b=1>>> c=4>>> in aTrue>>> in aFalse>>> 
Logical operators:

The result of And:x and Y returns is the value that determines the result of the expression. If x is true, Y determines the result, returns Y, and if x is False, x determines that the result is false and returns X.
Or:x or Y, like and, is the value that returns the result of the decision expression.
Not: Returns the "opposite value" of the result of the expression. Returns false if the expression result is true, or true if the expression result is false.

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

The

If statement has a very useful close relative that works somewhat like this (pseudo-code):
If not condition: the
Crash program
Does this because it crashes at a later time, It is better to crash the error condition when it occurs. In general, you can ask for certain conditions to be true. The keyword used in the statement is assert.
The Assert statement is useful if you need to make sure that a condition in the program is true to make the program work, and you can place a checkpoint in your program:
to add a string (separating the condition from the string with a comma) to interpret the assertion:

>>>  age=-1  >>>  assert  0  <age<150 ,  " The age must is realistic " Traceback (most recent call last): File " <stdin> ", line 1 , in  <module>assertionerror:the Age must is Realistic>>>  >>>  Age=10  >>>  assert  0  <age<150 ,  " The age must is realistic " >>>  

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Python3 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.