程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python Basics - if statement

編輯:Python

Catalog

1. Conditions for testing

1.1 Check for equality , Use ==

2.2 When checking for equality , Consider case

2.3 The check is not equal !=

2.4 Check multiple conditions and perhaps or

2.5 Check whether the list contains specific values , Keywords available in

2.6 Check that the list does not contain specific values , Keywords available not in

3.if sentence

3.1 if Sentence structure :

3.2 if-else structure :

3.3 if-elif-else structure :

3.4 The use of multiple elif Code block :

3.5else Code blocks can be omitted

3.6 Test multiple conditions

4. Use if Statement processing list

5. Set up if Sentence format


1. Conditions for testing

1.1 Check for equality , Use ==

  Equality returns True, Unequal return False

2.2 When checking for equality , Consider case

If you want equality , Variable values can be converted to lowercase and then compared , Here's the picture

2.3 The check is not equal !=

2.4 Check multiple conditions and perhaps or

2.4.1 Use and Check multiple conditions , If every test passes , The whole expression is True ; If at least one test fails , The whole expression is False. Examples are as follows :

2.4.1 Use or Check multiple conditions , But as long as at least one condition is satisfied , You can pass the whole test . Only if both tests fail , Use or The expression of is False. Examples are as follows :

2.5 Check whether the list contains specific values , Keywords available in

2.6 Check that the list does not contain specific values , Keywords available not in

3.if sentence

3.1 if Sentence structure :

if conditional_test:         // If the condition is true, execute

        do something

3.2 if-else structure :

if conditional_test:

        do something

else:

        do something

3.3 if-elif-else structure :

if conditional_test:

        do something

elif:

        do something

else:

        do something

3.4 The use of multiple elif Code block :

And 3.3 Agreement , Don't repeat

3.5else Code blocks can be omitted

Python There is no demand for if-elif There must be... Behind the structure else Code block . In some cases ,else Code blocks are useful ; And in other cases , Use one elif Statement to deal with specific situations more clearly

3.6 Test multiple conditions

Use a series of independent if sentence . If you just want to execute a block of code , Just use if-elif-else structure

4. Use if Statement processing list

stay for You can add... To the statement if sentence , Before inspection , First judge whether the list is empty :

names = []
if names:
for names in names:
print("aaa")
else:
print("No name")

5. Set up if Sentence format

In such as == 、>= and <= Add a space on each side of the comparison operator

The learning content of this article comes from 《Python Programming : From introduction to practice 》


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved