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

Python Programming Notes (5) conditions and Python code implementation

編輯:Python

Conditions  

Boolean value

Boolean value True and False Belong to type bool

if sentence

if sentence , Enables you to execute code conditionally . This means that if the conditions ( if Expression between and colon ) For the previously defined true , Execute subsequent blocks of code ( Here is a print sentence ); If the condition is false , No execution ( You should have guessed ).

else Clause

It is called a clause because else Not a stand-alone statement , It is if Part of the statement

elif Clause

To check multiple conditions , You can use elif . elif yes else if Abbreviation , By a if Clause and a else Clauses are combined into , That is, including conditions else Clause .

Code block nesting

if Put statements in other if In the block

name = input('What is your name? ')
if name.endswith('Gumby'):
if name.startswith('Mr.'):
print('Hello, Mr. Gumby')
elif name.startswith('Mrs.'):
print('Hello, Mrs. Gumby')
else:
print('Hello, Gumby')
else:
print('Hello, stranger')

More complex conditions

(1) Comparison operator

  Comparison operator

 

-- Equality operator

To determine whether two objects are equal , You can use the comparison operator , Use two equal signs ( == ) Express .

--is : Same operator

== Used to check whether two objects are equal , and is Used to check whether two objects are the same ( Is the same object ).

--in : Membership operators

It can be used in conditional expressions .

-- Comparison of strings and sequences

Strings are compared according to the alphabetical order of characters . The characters are arranged according to the sequence value . To learn the alphabetical values , You can use functions ord . The function is the same as the function chr contrary .

(2) Boolean operator

Operator and It's a Boolean operator . It accepts two truth values , And returns true when both values are true , Otherwise return false . There are two other Boolean operators : or and not . By using these three operators , Truth can be combined in any way .

Assertion

If you know that certain conditions must be met , The program can run correctly , You can add... To the program assert Statement acts as a checkpoint , It helps .

  Code example

Friends who want complete code , can toutiao Search for “ Programming workshop ” After attention s Believe in me , reply “Python Programming Notes 5“ obtain  


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