Conditions
Boolean value True and False Belong to type bool
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 ).
It is called a clause because else Not a stand-alone statement , It is if Part of the statement
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 .
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')
(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 .
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
author : Programming workshop
Game programming , A game development favorite ~
If the picture is not displayed for a long time , Please use Chrome Kernel browser .