Process control is mainly divided into judgment and cycle, here we first look at if condition judgment.
If statement
If expression:
Statements (s)
It is important to note that Python uses indentation as a method of grouping statements, so we recommend using 4 spaces as indentation, in the same indentation, all belong to the same block of code, of course, if you use the compiler, after writing the code, you can use the compiler to fine-tune the code format.
Now let's write a simple if condition as follows:
#!/usr/local/python3/bin/pythonif 0<1: #如果0小于1,则运行下面缩进里的代码块内容 print("Hello World !") # print("True") print("False")
Since 0 is less than 1 correct, the code will output the following after it is run:
[[email protected] ~]# python 2.py
Hello World!
False
Now it's a bit more complicated to add the operator precedence judgment:
#!/usr/local/python3/bin/pythonif not 0>1 and 1==1: ‘‘‘这里先判断逻辑非“not”,然后再判断“and”,这里结果为真,所以将运行缩进代码块的内容‘‘‘ print("Hello World !")# print("True") print("False")
We write a simple script that judges the grade of exam scores as follows:
#!/usr/locscorel/python3/bin/pythonscore=int(input("Please input score number : ")) #int()是把输入的str字符串转换成int数值if score <= 100: #首先判断这个数是否小于等于100,符合条件则进入下一个判断 if score >= 90: #判断这个数是否大于等于90 print("You got A.") elif score >= 75: #判断这个数是否大于等于75 print("You got B.") elif score >=60: #判断这个数是否大于等于60 print("You got C") else: #如果以上条件都不符合,则输出下面缩进的内容 print("You got D,and not pass.")else: #如果输入的数大于100的输出结果 print("Please input a correct score.")
A logical value (BOOL) is a Boolean value that contains two values (True or False):
True indicates a non-empty volume, such as: string,tuple,list,set,dict, all non-zero
False indicates 0,none, empty amount, etc.
Python's Process Control-if condition