Review if statements in Python

Source: Internet
Author: User
Remember, in the previous section, we introduced the if statement specifically: the process of starting the if statement. When learning the if statement, I do not know much about the basic knowledge of python Programming. maybe I have not done anything that is too complicated. This article will review it and improve the basic statement structure through review.

The 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?

The 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:

The 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.

The 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:

The 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.

The 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.

The 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:

The 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.

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.