The goal of snowball Learning Python is to make Python bigger and bigger.

Completed articles

The title link
1. This is the right way to start learning Python Juejin. Cn/post / 691636…
2. Learn data types and input and output functions without barriers, and snowball into Python Juejin. Cn/post / 691668…

This series of articles will be completed before the Spring Festival of 2021, welcome to follow, like, comment -- dream eraser

No programming without turning point

If procedures are carried out in accordance with the order from top to bottom, the program will lack A lot of fun and transforming, and can lead to dry, write for this kind of situation, in order to achieve certain effects, needs code for twist, the turning point in the program is to process control, from A to B, can also be to C. In flow control, you will learn about relational and logical operators simultaneously.

This blog will be light, because the next one will be about lists, which is a lot of knowledge, so let’s learn some simple foreshadowing.

3.1 Relational operators

In Python, relational operators are simply ratios of size, so all you need to learn is greater than, less than, equal, etc.

The specific relational operators are as follows:

Relational operator meaning
> Is greater than
< Less than
> = Greater than or equal to
< = Less than or equal to
= = Is equal to the
! = Is not equal to

The relational operator returns True if the conclusion is True, False otherwise. Write a code to test it:

a = 1 > 2
print(a) # False
b = 2 > 1
print(b) # True
Copy the code

The logical operator symbol can be left and right of variables or arbitrary values, and the result is a Boolean data type. This is a small but important point.

3.2 Logical operators

There are three logical operators in Python: and, OR, and not.

An expression containing a logical operator returns a Boolean value. For details, please refer to the following code:

a = (1 > 2) and (2 > 1)
print(a) # False

b = 2 > 1 or 1 < 2
print(b) # True
Copy the code

The first formula uses the and logical operator, with 1>2 on the left and 2>1 on the right. The result of this formula is False.

There is an important point here:

  • andOperator, which needs both sides to be True for its final result to be True, otherwise both are False.
  • orOperator, at least one of the left and right sides must be True for the final result to be True.
  • notThe checkmarked operator is an inverse operation, which is False and True.

The above content is the same as the relational operators, it is not meaningful to simply look at it, it is necessary to practice in code and cases, to master.

3.3 Programming transition – flow control

Flow control statements are also called branch statements, so pay attention to the names when looking up information.

3.3.1 the if statement

First you need to take a look at the syntax of the if statement to get an impression.

if(Conditional judgment): code blockCopy the code

If the final result of the conditional is True, the code block content is executed. If the final result of the conditional is False, the code block content is not executed.

You’ll also learn about indentation in Python, and how to determine if a block of code is an if statement, depending on the Tab key or four Spaces. Don’t omit a colon at the end of the if line: exists.

We can look at the actual code below. For example, if a person is older than 18, output the following code for the adult:

age = 20
if (age >= 18) :print("Adult")
Copy the code

If (age>=18) {age>=18; if (age>=18) {age>=18;

The red box in the figure above is Tab indentation. The indent is the code block inside if, and the bottom print has nothing to do with the if statement. Note that the indentation “distance” must be consistent, as shown in the following figure will cause errors in Python.

Indentation is one of Python’s most important rules for determining blocks of code, and you’ll be introduced to it first here and throughout your Python career.

The space after if can be omitted from the previous code:

age = 20
if age >= 18:
    print("Adult")
    print("I belong to the if statement above.")

print("I have nothing to do with the if statement.")
Copy the code

3.3.2 rainfall distribution on 10-12 if… else … statements

As a process control statement, as long as there should be two possibilities, one is true, one is false, can be called a process, otherwise the execution from top to bottom is done. So in addition to the if statement, we have an else statement, which you can think of as executing the code block content in the if statement when the condition is true, and else statement code block content when the condition is false. The syntax is as follows:

if(Conditional judgment):ifThe block of codeelse:
	elseThe block of codeCopy the code

Complete a small example, when the age is greater than or equal to 18 prompt adult, less than 18 prompt minor.

age = int(input(Please enter your age:))
if age >= 18:
    print("Adult")
    print("I belong to the if statement above.")
else:
    print("Minor")
    print("I belong to the else statement above.")
Copy the code

3.3.3 the if… elif … else … statements

If… is used when there are multiple possibilities for process control. elif … else … The syntax is as follows:

if(Conditional judgment):ifThe code blockelif(Conditional judgment):elifThe code blockelse:
	elseThe code blockCopy the code

Calculate A’s, B’s, C’s… Class, but let’s not use this example, let’s find a new one, calculate the CSDN author tax.

No tax within 800 800 to 4000 pay the total amount minus 800 and then multiplied by 20% 4000 to 20000, directly 16% of the total amountCopy the code

The code is as follows:

money = int(input(Please enter your income:))

if money <= 800:
    print("No tax.")

elif money > 800 and money <=4000:
    print("The tax amount shall be:",(money-800) *0.2)

elif money>4000 and money<20000:
    print("The tax amount shall be:", money * 0.16)

else:
    print("You make too much money. Take it all away.")

Copy the code

Be careful, indent, indent, indent all the same.

3.3.4 Nesting of if statements

This content can be easily understood when you see the code, which is the if statement within the if statement, as long as you pay attention to the indentation, you can continue to loop indefinitely, no more use, imitate the following code, try to run it.

money = int(input(Please enter your income:))

if money <= 800:
    print("No tax.")
    if money > 0:
        print("I can't believe I made money.")
    else:
        print("Lost money.")

elif money > 800 and money <=4000:
    print("The tax amount shall be:",(money-800) *0.2)

elif money>4000 and money<20000:
    print("The tax amount shall be:", money * 0.16)
else:
    if money > 100000000:
        print("Made over a hundred million dollars.")
    else:
        print("Didn't make $100 million.")
Copy the code

3.4 Summary of this blog

Relational and logical operators are often used in conjunction with if statements, and this is a small but important point. If statements are not difficult overall, but from this blog you’ve learned that Python relies on indentation for section control. This is a very small fulcrum that will lead to the subsequent style of Python syntax, which is equally important.

The last bowl of chicken soup

Study hard and there will be new blows every day