Review the if statement from Python

  • 2020-04-02 14:12:15
  • OfStack

Basic statement structure


if Judge conditions 1:
    Execute the statement 1 ...
elif Judge conditions 2:
    Execute the statement 2 ...
elif Judge conditions 3:
    Execute the statement 3 ...
else:
    Execute the statement 4 ...

The following execution statement is executed only if the value of the judgment condition is True.

So, in python, how do you know if a condition is true? We've covered one data type in a dizzying array of operators: the Boolean type. A built-in function bool() can be used to determine whether a condition results in True or False. Take a look at the following example to see if you can understand the bool() judgment rule.


>>> 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">"a")
True
>>> bool(not "")
True
>>> bool(not True)
False

What if I forget? Look at the following statement:

If forget:
      Review - > Dazzling operators
You don't have to put a bool() in the execution statement. Like this:


>>> x = 9 >>> if bool(x>7):       # Conditions for True Executes 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

Both of these are equivalent, but in real programming we don't use if bool(x) > 7) instead use if x > If I write if (x) > 7), is it ok to enclose the conditional expression with a parenthesis? Yes, but that's not what python advocates.


>>> if (x>7):       # I'm not advocating it. It's not python style
...     print "%d more than 7"%x
...
9 more than 7

Pull out

Usually there is always someone in unconvinced when said "is a mule is a horse, pull out yo", zhao benshan has a famous saying "walk two steps". Its essence is to say that "speak without practice is a false handle". Today, I received an email from a friend who also asked me that I could not remember the content of python when I was learning python. You don't have to remember, I've done this over and over again in the previous lectures. However, in the application, will become more and more skilled.

Here's an exercise that requires:

Receive input for any characters and Numbers
Determine the input content, if not an integer is a character, tell the user; If it's a decimal, tell the user
If an integer is entered, determine whether the integer is odd or even, and tell the user
In this exercise, it is obvious to judge what is being typed. Here are some things to watch out for:

The input you get from raw_input() is of type STR
To determine whether a string is composed of pure Numbers, use str.isdigit() (it is recommended that you check the official documentation for this built-in function)
The following code is a reference:


#! /usr/bin/env python
#coding:utf-8 print " Please enter a string , Then press enter: " user_input = raw_input() result = user_input.isdigit() if not result:
    print " What you enter is not exactly a number " 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 didn't type anything, did you "

Special reminder list, this code is not very perfect, and can be modified, see whether it can be improved?

How about another one?

Given a list of integers, jump out of odd and even Numbers, and put each in a list.

Please write your own code before looking at the following reference code.


#!/usr/bin/env python
#coding:utf-8 import random numbers = [random.randint(1,100) for i in range(20)] # In order to list The way you parse it is you get random list 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

Use this example to demonstrate the application of if in list parsing. See if you can work on it?

You can replace that part of the loop with the following list parsing


#!/usr/bin/env python
#coding:utf-8 import random numbers = [random.randint(1,100) for i in range(20)] # In order to list The way you parse it is you get random list 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

To assign a value, the reader should be familiar with, if want to review, see "[assign a value, simple also not simple]" (./127.md) and "[say a sentence formally]" (./201.md) relevant content.

So what does this interesting assignment look like? Please look at:


>>> name = "qiwsir" if "laoqi" else "github"
>>> name
'qiwsir'
>>> name = 'qiwsir' if "" else "python"
>>> name
'python'
>>> name = "qiwsir" if "github" else ""
>>> name
'qiwsir'

To summarize: A = Y if X else Z

What do you mean? Combined with the previous examples, we can see that:

If X is true, then A is equal to Y
If X is false, then A is equal to Z
Let's take a look at the above example. Is this how it works?

The if statement seems simple, but it is often used in programming time. Practice.


Related articles: