Learn python with little hands # no programming without turning points, learn python with snowball [source code attached]

Programmer Didi 2022-06-23 18:00:33 阅读数:80

learnpythonlittlehandsprogramming

No turning point, no programming

If the program is executed from top to bottom , The program will lack a lot of fun and transition , And it will lead to the programming boring , In this case , In order to achieve some effect , Need code to implement transition , This transition in the program is Process control , from A You can go to B, You can also go to C.

In process control , You're going to learn Relational operator And Logical operators .

This blog has less content , Because the next article is going to talk about lists , That's a lot of knowledge , Let's learn a little bit of simple bedding first .

Relational operator

stay Python In fact, the relational operator is the concept of ratio size , So what we have to learn is greater than 、 Less than 、 Equal to the content of .

The specific relational operators are as follows :

Relational operator

meaning

>

Greater than

<

Less than

>=

Greater than or equal to

<=

Less than or equal to

\==

be equal to

!=

It's not equal to

If the conclusion is true , After the operation of the relational operator, it returns True, Instead of False.

Write a code test :

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

The left and right sides of a logical operator symbol can be variables , It can also be any value , The result return is a Boolean data type . Although this knowledge point is small , But very important .

Logical operators

Logical operators are in Python There is 3 individual , Namely andornot.

Expressions containing logical operators , The final result returned is also a Boolean value . Specific can refer to the following code :

a = (1 > 2) and (2 > 1)
print(a) # False
b = 2 > 1 or 1 < 2
print(b) # True

The first formula uses and Logical operators , On the left 1&gt;2, The right side is 2&gt;1. The final result of this formula is False.

There is an important point here as follows :

  • and Operator , Need to be left and right for True, It turns out to be True, Otherwise, it's all for False.
  • or Operator , You need at least one for True, The final result is True.
  • not Operator signing is a negation operation , The original formula is False, After taking the reverse, it is True.

The above is the same as the relational operator , It doesn't make sense to just look at it , What is needed is practice in code and cases , In order to master .

The turning point in programming - Process control

Process control statements are also called branch statements , So when you look for information , Notice the name .

if sentence

First you need to take a look if The syntax of the statement , If you have an impression .

if ( conditional ):
Code block 

The meaning is if conditional The end result is True, perform Code block Content , If conditional The end result is False, Don't execute Code block Content .

Here you have to learn Python How to indent , stay Python How to judge whether a code block is if The code block of the statement , It's dependence Tab Or the key 4 A space To judge . Besides, don't leave out if There is a colon at the end of the line : There is ~.

Let's take a look at the actual code , for example , Judge a person older than 18 year , The output code for adults is as follows :

age = 20
if (age >= 18):
print(" adults ")

In the above contents age = 20 , Be sure to be greater than 18, that age>=18 The result returned is True, Will execute if The code block inside the statement , And how to judge whether a piece of code belongs to if Inside the sentence , It is judged by indenting , See the picture for details :

The red box above shows Tab Indent . Indented is if Internal code blocks , At the bottom print And if sentence irrelevant . Pay attention to the indented “ distance ” It has to be consistent , For example, the figure below is in Python Will make mistakes .

Indentation is Python An important basis for judging code blocks , from if Here you are first exposed to , It's going to be all over the back Python The whole learning career .

In the code just now if The following space can be omitted , Modify the code as follows :

age = 20
if age >= 18:
print(" adults ")
print(" I belong to the above if sentence ")
print(" I and if It doesn't matter ")

if ... else ... sentence

As a flow control statement , As long as there should be two possibilities , One is true , One is false , It can be called a process , Otherwise, it's done from top to bottom . So in addition to if Beyond the sentence , also else sentence , It can be understood that when the condition is true if The content of the code block in the statement , Execute when the condition is false else Statement code block content .

The syntax is as follows :

if ( conditional ):
if Code block for
else:
else Code block for 

Complete a small example , When you are older than or equal to 18 Suggesting adulthood , Less than 18 It's suggestive of underage .

age = int(input(" Please enter your age :"))
if age >= 18:
print(" adults ")
print(" I belong to the above if sentence ")
else:
print(" A minor ")
print(" I belong to the above else sentence ")

if ... elif ... else ... sentence

When there are multiple possibilities for process control , It needs to be used if ... elif ... else ... Statement , The syntax is as follows :

if ( conditional ):
if Code block
elif ( conditional ):
elif Code block
else:
else Code block 

The most typical content is based on grades A、B、C... Grade , But we don't need this example , Find a fresh one , Calculation CSDN Author tax .

800 There's no tax paid inside
800 To 4000 The total amount paid minus 800 And then multiplied by the 20%
4000 To 20000 Of , Direct total amount of 16%

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 amount of tax paid is :",(money-800)*0.2)
elif money>4000 and money<20000:
print(" The amount of tax paid is :", money * 0.16)
else:
print(" You earn too much , It's all buttoned up ")

Be sure to pay attention to , Indent 、 Indent 、 Indents are the same .

if Nesting of statements

The content can be easily understood by the code , To put it bluntly if Statement set if sentence , As long as you can pay attention to indentation , You can go on and on , It's useless to say more , 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've made money ")
else:
print(" Lost money ")
elif money > 800 and money <=4000:
print(" The amount of tax paid is :",(money-800)*0.2)
elif money>4000 and money<20000:
print(" The amount of tax paid is :", money * 0.16)
else:
if money > 100000000:
print(" It's more than 100 million ")
else:
print(" Not making a hundred million ")

The summary of this blog

Relational and logical operators are often associated with if Statement with , Little knowledge , But very important .if The whole sentence is not difficult , But from this blog, you've been exposed to Python Is dependent on indentation for code segment control , This is a very small fulcrum , follow-up Python The style of grammar comes from it , Equally important , So this blog is all about the point .

The last bowl of poisonous chicken soup

Study hard Every day there is a new blow O(∩_∩)O ha-ha ~

版权声明:本文为[Programmer Didi]所创,转载请带上原文链接,感谢。 https://pythonmana.com/2021/12/202112312251561690.html