With the old Ziko Python review if statement _python

Source: Internet
Author: User
Tags in python

BASIC statement structure

Copy Code code as follows:

If Judge 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 if the value of the "Judge condition" is true.

So, in Python, how do you know if a judgment condition is true? The problem we have in the dazzling operator has been explained by a data type: Boolean type. A built-in function, bool (), can be used to determine whether the result of a condition is true or false. Looking at the following example, is it possible to understand the rule of bool ()?

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

Forget about what to do? Look at the following statement:

If forget:
Review the--> dazzling operator one speak
In the execution statement, it is not necessarily necessary to write bool (). Like this:

Copy Code code as follows:

>>> x = 9

>>> if BOOL (x>7): #条件为True则执行下面的
... 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 above two expressions are equivalent, but in actual programming, we do not use if bool (X>7) format, but rather using the if x>7 style, but also special reminders, if written as if (x>7), with a bracket to enclose the conditional expression, is it OK? That's OK, but it's not what Python advocates.

Copy Code code as follows:

>>> if (x>7): #不提倡这么写, this is not Python style
... print "%d more than 7"%x
...
9 More than 7

Pull it out and sneak out.

Usually someone in the time of not convinced that "is a mule is a horse, pull out to sneak", Zhao Benshan has a famous saying "Take two steps." Its essence is to say that "light is not practice is false." I got a message from a friend today and asked me to remember Python when I was learning python. Actually, I've talked about it over and over again in the previous course. However, in the application, will become more and more skilled.

Here's an exercise that requires:

Receive input for any character and number
To determine what is entered, if it is not a character, tell the user, and if it is a decimal, tell the user
If you enter an integer, determine if the integer is odd or even, and tell the user
In this exercise, it is obvious to judge the content of the input, the following points need reader attention:

The input from Raw_input () is the STR type
To determine whether a string is composed of pure digits, you can use Str.isdigit () (Recommended reader View the official document of the built-in function)
The following code is a reference:

Copy Code code as follows:

#! /usr/bin/env python
#coding: Utf-8

Print, enter the string, and then press ENTER: "

User_input = Raw_input ()

result = User_input.isdigit ()

If not result:
Print "You're not exactly the number you entered"

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're not typing anything."

Special reminders listed, this code is not very perfect, but also can modify the place, reader can be perfect?

How about another one?

A list of integers is known to jump out of odd and even numbers, and each is placed in a list.

Please reader write yourself before reading the following reference code.

Copy Code code as follows:

#!/usr/bin/env python
#coding: Utf-8

Import Random

numbers = [Random.randint (1,100) for I in range] #以list解析的方式得到随机的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 illustrate the application of if in list parsing. See if you can keep improving.

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

Copy Code code as follows:

#!/usr/bin/env python
#coding: Utf-8

Import Random

numbers = [Random.randint (1,100) for I in range] #以list解析的方式得到随机的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

On the assignment, reader should be more familiar with it, if you want to review, please see "[Assign value, simple nor simple]" (./127.md) and "[Formal say]" (./201.md) related content.

What is the interesting value of the assignment here? Please see:

Copy Code code 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 that mean, combined with the preceding example, you can see:

If x is true, then execute a=y
If x is False, execute a=z
Take a look at the above example, is this the implementation of this?

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.