Zero basic python nanny tutorial -- if statement

Keywords: Python Back-end

In the last article, we talked about flashbacks, and then we talked about tuples, about lists and characters. Variables can be viewed by clicking my home page. Remember to click follow.

if statement

if keyword or condition judgment:

Condition (i.e. expression):

Conditions can also be output statements

name = 'Qing'an'
if name == 'Qing'an':
    print('It's Qing'an')
# The result is:
# It's Qing'an
 Copy code

else:

name = 'There is nothing else in Qing'an'
if name == 'There is nothing else in Qing'an':
    print('It's Qing'an')
else:
    print('Not Qing'an')
# The result is:
# It's Qing'an
 Copy code

Look at this interesting example:

# Create an animal list
animal = ['cat','dog','duck','fish']
#Use the for loop to traverse the list and define a new variable for the loop animal
for animals in animal:
    #Use if to make a judgment, judge whether the newly defined variable has been traversed, and judge whether there is cat in the list
    if animals == 'cat':
        # If so, print in full character uppercase
        print(animals.upper())
    else: #otherwise
        # Print first character uppercase
        print(animals.title())

# CAT
# Dog
# Duck
# Fish
 Copy code

Here we see a new knowledge, that is, the comparison symbol = =, which is a comparison operator to judge whether it is equal or not. We will slowly tell you here, and there will be more later= For the judgment of inequality, there are such judgments as greater than >, less than <, equal to =, etc.

Next, let's look at inequality=

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

Here, we define a string whose variable is' dog ', and then we use the if statement to judge that it is equal to the string' cat '. If not, print a result to tell us. Here we can also judge that the final conclusion of this procedure is inequality.

After reading the above, are you absolutely or very fascinated? Let's talk about it in detail! By the way, compare the symbols together. Oh, learn well, watch well and practice more. If you don't understand, you can understand it twice!!!

age = 1
if age > 19:
    print('Yes')
else:
    print('No')
Copy code

Explanation: Here we define an age variable, age, and use if else to judge it. Let age compare with 19. If the number of age is greater than 19, we will output yes, otherwise we will output no. pyhton will make a judgment in the background, and then give us the desired results. If we do not print here, the program will report an error!

elif

Next, let's learn the if elif else statement:

Take a very easy topic and define an age =?, If you are older than 18, you will charge 20 yuan, if you are older than 10, you will charge 10 yuan, and if you are younger than 10, you will be free!

Let's look at the program:

age = 13
if age < 10:
    print('Less than ten years old, free')
elif age < 18:
    print('10 yuan for those over 10 but under 18')
else:
    print('Over 18 years old, charge 20 yuan')
Copy code

Explanation: here, we don't repeatedly judge whether we are more than 10 years old but less than 18 years old. You can also judge 10 < age < 18 here. We used elif to make a judgment in front, so we don't need to judge again here. This writing also brings us convenience. Since the latter two conditions have been met, it is good to print directly at the tail else. Whether else means, there is no need to judge the condition again.

We can also improve the program by omitting else code, using only elif, and using multiple elifs

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

Explanation: the previous example is also adopted here, and 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 oh")
    else:
        print(f"sorry,{new_animals}: There are no pets")

# cat: pets and oh
# fish: and pets
# duck: and pets
 Copy code

Explanation: Here we see a new content: in. This is to judge whether it is included and whether the animals the user wants to buy are included in the pet store. Use the for loop to traverse the pets the user needs, and then use if to make a judgment. If the pet store has the pets the user needs, print a result. Otherwise, print, sorry, These pets you need are gone.

python super full database installation package learning route project source code free sharing

Posted by webv on Mon, 08 Nov 2021 20:28:33 -0800