Review the if statement in Python and learn pythonif
Basic statement Structure
Copy codeThe Code is as follows:
If condition 1:
Execute Statement 1 ......
Elif judgment condition 2:
Execute Statement 2 ......
Elif judgment Condition 3:
Execute Statement 3 ......
Else:
Execute Statement 4 ......
The following execution statement is executed only when the value of "judgment condition" is True.
In python, how do I know whether a condition is true? We have explained a data type in the dazzling operators: Boolean. You can use a built-in function bool () to determine whether the result of a condition is True or False. Let's take a look at the following example. Can we understand bool () Judgment Rules?
Copy codeThe Code is as follows:
>>> Bool ("")
False
>>> Bool (0)
False
>>> Bool ('None ')
True
>>> Bool (False)
False
>>> Bool ("False ")
True
>>> Bool (True)
True
>>> Bool ("True ")
True
>>> Bool (3> 4)
False
>>> Bool ("B"> "")
True
>>> Bool (not "")
True
>>> Bool (not True)
False
What should I do if I forget it? See the following statement:
If you forget:
Review --> A dazzling introduction to operators
In the execution statement, it is not necessary to write bool. Like this:
Copy codeThe Code is as follows:
>>> X = 9
>>> If bool (x> 7): # if the condition is True, execute the following
... Print "% d more than 7" % x
... Else:
... Print "% d not more than 7" % x
...
9 more than 7
>>> If x> 7:
... Print "% d more than 7" % x
... Else:
... Print "% d not more than 7" % x
...
9 more than 7
The preceding two statements are equivalent. However, in actual programming, we do not use the if bool (x> 7) format, but use the if x> 7 style, if the expression is written as if (x> 7), can it enclose the conditional expression with a bracket? Yes, but it is not advocated by python.
Copy codeThe Code is as follows:
>>> If (x> 7): # This is not recommended. This is not a python style.
... Print "% d more than 7" % x
...
9 more than 7
Pull out and slide
At ordinary times, when people are not convinced, they say "it's a horse, pulling it out and slide". Zhao Benshan's famous saying goes "Two Steps ". In essence, they all say that "not practicing is a false strategy ". Today, I received an email from a friend asking me that I couldn't remember the python content when I was learning python. In fact, you don't need to remember. I have already mentioned it over and over again in the previous course. However, they will become increasingly proficient in applications.
The following is an exercise:
Receives input of any character or number
Judge the input content. If it is not an integer or a character, it is told to the user. If it is a decimal, it is also told to the user.
If the input is an integer, judge whether the integer is an odd or even number and tell the user
In this exercise, it is clear that you need to judge the input content. The following points should be taken into account:
The input content obtained through raw_input () is of the str type.
To determine whether a string is composed of pure numbers, you can use str. isdigit () (we recommend that you check the official documentation of this built-in function)
The following code is a reference:
Copy codeThe Code is as follows:
#! /Usr/bin/env python
# Coding: UTF-8
Print "enter a string and press the Enter key :"
User_input = raw_input ()
Result = user_input.isdigit ()
If not result:
Print "you entered not all numbers"
Elif int (user_input) % 2 = 0:
Print "you entered an even number"
Elif int (user_input) % 2! = 0:
Print "you entered an odd number"
Else:
Print "You have not entered anything"
The special reminder column shows that this code is not very complete and can be modified. Can it be improved?
How about another one?
It is known that a list composed of integers jumps out of the odd and even numbers and is placed in a list.
Please read the reference code below and write it yourself.
Copy codeThe Code is as follows:
#! /Usr/bin/env python
# Coding: UTF-8
Import random
Numbers = [random. randint (1,100) for I in range (20)] # obtain a random list by list resolution
Odd = []
Even = []
For x in numbers:
If x % 2 = 0:
Even. append (x)
Else:
Odd. append (x)
Print numbers
Print "odd:", odd
Print "even:", even
This example demonstrates the application of if in list parsing. Can you continue to improve it?
We can replace the part of the loop with the list resolution below.
Copy codeThe Code is as follows:
#! /Usr/bin/env python
# Coding: UTF-8
Import random
Numbers = [random. randint (1,100) for I in range (20)] # obtain a random list by list resolution
Odd = [x for x in numbers if x % 2! = 0]
Even = [x for x in numbers if x % 2 = 0]
Print numbers
Print "odd:", odd
Print "even:", even
An interesting assignment
You should be familiar with the assignment. If you want to review the assignment, see [assignment, simple or not] (. /127.md) and [formally speaking] (. /201.md.
What does the interesting assignment look like? See:
Copy codeThe Code is as follows:
>>> Name = "qiwsir" if "laoqi" else "github"
>>> Name
'Qiwsir'
>>> Name = 'qiwsir 'if "" else "python"
>>> Name
'Python'
>>> Name = "qiwsir" if "github" else ""
>>> Name
'Qiwsir'
To sum up, A = Y if X else Z
What does it mean? combined with the previous example, we can see that:
If X is true, execute A = Y
If X is false, run A = Z.
Let's take a look at the example above. Is it like this?
If statements seem simple, but they are often used in programming time. Work hard.
Python if statement Problems
And
Or just talk to me.
Complimentary
In addition, the = value is the same.
The memory address is the same.
Python if statement indent
The second case is easy to understand.
For n in range (2, 14 ):
For x in range (2, n ):
If n % x = 0:
Print n, 'eques', x, '*', n/x
Break
Else:
Print n, 'is a prime number'
For example, if n is a prime number, the second-order loop will be executed to the else branch each time. Because there is no break in else, the second-level loop will not be picked out and n is a prime number will be played many times, non-prime numbers, for example, 8 or 9, can be easily understood after you think about them.
For n in range (2, 14 ):
For x in range (2, n ):
If n % x = 0:
Print n, 'eques', x, '*', n/x
Break
Else:
Print n, 'is a prime number'
Here, the else should be executed after the for loop is completed, and if the break jumps out of the for loop, the corresponding else will not be executed, so the running result will be correct.