current position:Home>Daily python, Part 8 - if statement

Daily python, Part 8 - if statement

2022-01-31 05:32:52 Qing'an slag

「 This is my participation 11 The fourth of the yuegengwen challenge 8 God , Check out the activity details :2021 One last more challenge 」.

This is Qing'an , Welcome to this chapter .

if sentence

         if Keyword or conditional judgment :

                Conditions ( That's the expression ):

                     Conditions can also be output statements

name = ' Qing'an '
if name == ' Qing'an ':
    print(' It's Qing'an ')
#  As a result, :
#  It's Qing'an 
 Copy code 


name = ' Qingan has nothing else to do '
if name == ' Qingan has nothing else to do ':
    print(' It's Qing'an ')
    print(' Not Qing'an ')
#  As a result, :
#  It's Qing'an 
 Copy code 

         Look at this interesting example :

#  Create a list of animals 
animal = ['cat','dog','duck','fish']
# Use for Loop through the list , Define a new variable for the loop animal
for animals in animal:
    # Use if Make a judgment , Judge whether the newly defined variable has been traversed , And judge whether there is... In the list cat This string 
    if animals == 'cat':
        #  If there is , Print in full character uppercase 
    else: # otherwise 
        #  Print first character uppercase 

# Dog
# Duck
# Fish
 Copy code 

         Here we see a new knowledge , Is the comparison symbol ==, This is a comparison operator to determine whether it is equal , I will slowly tell my friends here , And then there's != The judgment of inequality , For numbers, there are more than >、 Less than <、 be equal to =、 Wait for such a judgment .

         Next, let's look at inequality !=

request_name = 'dog'
if request_name != 'cat':
    print(' The comparison results are not equal ')
 Copy code 

         ad locum , We define a variable as 'dog' String , And then we use if Sentence to judge , Determine when it is equal to the string 'cat', If not equal to , Print a result to tell us . Here we can also judge , The final conclusion of this procedure is inequality .

         Read the above , Are you absolutely still crazy , So let's talk about it in detail ! Along with the comparison symbols , Study hard , Take a good look at , practice . If you don't understand, you can understand it twice !!!

age = 1
if age > 19:
 Copy code 

         explain : Here we define an age variable age, use if-else Make a judgment on it , Give Way age On 19 Compare , If age This number is greater than 19, So we output yes Otherwise output no,pyhton The backstage will make a judgment , Then give us the results we want , If you don't add print Print , The program will report an error !


         Next we learn if-elif-else sentence :

         Take a very easy topic , Define an age age = ?, If you are older than 18, you will be charged 20 element , Over ten and under eighteen 10 element , Less than ten years old free !

         Let's look at the program :

age = 13
if age < 10:
    print(' Less than ten years old , free ')
elif age < 18:
    print(' Over ten but under eighteen , charge 10 element ')
    print(' Older than eighteen , charge 20 element ')
 Copy code 

         explain : Here we don't repeat to judge whether we are more than ten years old and less than eighteen years old , Here you can 10<age<18 It's OK, too , We use the elif I made a judgment before , So there is no need to judge again , Writing like this also brings us convenience . Because of the latter two conditions, we have met , So in the tail else Just print directly there ,else Is that what it means , There's no need to sit on the condition judgment again .

         We can also improve the program , That's it Omit else Code , Only elif, And you can also use multiple elif

age = 65
if age < 10:
    print(' free ')
elif age <= 18:
    print(' charge 10 element ')
elif age <= 65:
    print(' charge 20 element ')
elif age > 65:
    print(' free ')
 Copy code 

         explain : Here is also the last example , An additional judgment condition is added, that is <=

actual combat

animal = ['cat', 'dog', 'duck', 'fish', 'eagle', 'tortoise']
new_animal = ['cat', 'fish', 'duck']
for new_animals in new_animal:
    if new_animals in animal:
        print(f"{new_animals}: Pets and ")
        print(f" sorry ,{new_animals}: There are no pets ")

# cat: Pets and 
# fish: Pets and 
# duck: Pets and 
 Copy code 

         explain : Here we see a new content is in, This is to judge whether it is included , Whether the animals the user wants to buy are included in the pet store , use for Loop through the pets required by the user , Reuse if Make a judgment , If the user needs a pet , The pet shop has , Then print a result , otherwise , Print , sorry , These pets you need are gone .

copyright notice
author[Qing'an slag],Please bring the original link to reprint, thank you.

Random recommended