SoFunction
Updated on 2024-11-13

Examples of Python Learning Notes on the use of if statements

preamble

Conditional statements in the actual development we have used a few times, here we need to once again grand to introduce it, the following words do not say much, come together to see the details of the introduction.

if statement

As the name suggests, this statement is a judgment statement, so let's start with a simple example

cars=['audi','bmw','subaru',toyota]
for car in cars:
 if car == 'audi':
 print()
 else:
 print()
#-->AUDI Bmw Subaru Toyota
#You can see that the first element is all capitalized,While other elements only have initial capitalization

conditioned test

At the heart of every if statement is the fact that there is a True or False judgment on the

The following cases are for checking if two elements are exactly equal

car = 'bmw'
print(car == 'bmw')#-->True check for exact equality
print(car == 'Bmw')#-->False is case sensitive, so it will not be equivalent
print(() == 'Bmw')#-->True This will equalize,should betitle()commander-in-chief (military)carThe first letter of the alphabet is capitalized

The following cases are for detecting whether two elements do not want to be equal or not

car = 'bmw'
print(car != 'audi')#-->True 

The following cases are for comparing two numbers:

age= 18
print(age == 18)#-->True equals
print(age != 18)#-->False not equal to
print(age == 30)#-->False is equal to
print(age < 30)#-->True Less than
print(age <= 30)#-->True Less than or equal to
print(age > 30)#-->False Greater than
print(age >= 30)#-->False greater than or equal to

The following cases check for multiple conditions

age_0= 18
age_1=30
print(age_0==18 and age_1==30)#-->True Both judgments are True
print(age_0!=18 and age_1==30)#-->Flase One judgment is all True One judgment is all False
print(age_0!=18 and age_1!=30)#-->Flase Both judgments are False

print(age_0==18 or age_1==30)#-->True Both judgments are True
print(age_0!=18 or age_1==30)#-->True One judgment is all True One judgment is all False
print(age_0!=18 and age_1!=30)#-->Flase Both judgments are False

#Conclusion:
#and:both must be True then True
#or:As long as one side is for theTrueimitateTrue

Determines whether a particular value is included in the list:

age=[12,13,14,15,16,17]
print(12 in age)#-->True
print(0 in age)#-->False

Determines whether a particular value is included or excluded from the list:

age=[12,13,14,15,16,17]
print(12 not in age)#-->False
print(0 not in age)#-->True

Bool expressions

isShow=True
isGood=False
print(isShow)#-->True
print(isGood)#-->False

Combined judgment:

car = 'bmw'

if car=='bmw':
 print("Good")#-->Good 
else:#Execute the following non-statement here with the condition car=='bmw', i.e. car!='bmw'
 print("Bad")

if car=='audi':
 print("Good")
else:#Execute the following non-statement here with the condition car=='bmw', i.e. car!='bmw'
 print("Bad")#-->Bad

if statement

(1)if

sample code (computing)

isShow=True
if isShow:
 print("It's showing")
#-->It's showing

(2)if-else

sample code (computing)

isShow=False
if isShow:
 print("It's showing")
else:
 print("It's not showing")
#-->It's not howing

(3)if-elif-else

sample code (computing)

age=18
if age<22:
 print("You can't get married.")
elif age<30:
 print("You're not married.")
else:
 print("Single.")
#-->You can't get married.
age=28
if age<22:
 print("You can't get married.")
elif age<30:#Does not match age<22 but does match age<30
 print("You're not married.")
else:
 print("Single.")
#-->You're not married.
age=50
if age<22:
 print("You can't get married.")
elif age<30:
 print("You're not married.")
else:# Does not meet age<22 and age<30
 print("Single.")
#-->lone
# Sometimes it's clearer to use elif instead of else:
if age<22:
 print("You can't get married.")
elif age<30:
 print("You're not married.")
 elif age>=30:
 print("Single.")

Handling lists with if

request_toppings=['mushrooms','extra cheese']
topings_none=['mushrooms']
for request_topping in request_toppings:
 if request_topping in topings_none:
  print('There's no such thing as pizza')
 else:
  print("Adding " + request_topping + ".")
print("Finish making your pizza!")
#--> there is no such pizza
#-->Adding extra cheese.
#-->Finish making your pizza!

Make sure the list is not empty:

request_toppings=[]
if request_toppings:#Returns True if the list contains at least one element, False otherwise.
 print('Have')
else:
 print('Not Have')
#-->Not Have

summarize

Above is the entire content of this article, I hope that the content of this article on your learning or work has a certain reference learning value, if there are questions you can leave a message to exchange, thank you for my support.