SoFunction
Updated on 2024-12-17

Reviewing if Statements in Python with Laoqi

Basic statement structure

Copy Code The code is as follows.

if Judgement condition 1.
Execute statement 1 ......
elif Judgment condition 2.
Execute statement 2 ......
elif Judgement condition 3.
Execute statement 3 ......
else:
Execute statement 4 ......

The following executable statement is executed only if the value of the "Judgment Condition" is True.

So how do you know if a judgment condition is true in python? This is a question we've already explained in the dazzling array of operators for one data type: boolean. A built-in function bool() can be used to determine whether a condition results in True or False. look at the following example, is it possible to understand the rules of bool() judgment?

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

What if you forget? Look at the statement below:

if Forgotten.
Review - > eye-candy operators a lecture
It's not really necessary to put bool() in the executing statement. As in this:

Copy Code The code is as follows.

>>> x = 9

>>> if bool(x>7): # If the condition is True then 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 above two writings are equivalent, but, in actual programming, instead of using the if bool(x>7) format, we use the if x>7 style, with a special caveat that if we write it as if (x>7), isn't it okay to enclose the conditional expression in a single parenthesis? It could, but it's not something python promotes either.

Copy Code The code is as follows.

>>> if (x>7): # This is not recommended, it's not python style.
...     print "%d more than 7"%x
...
9 more than 7

pull out and skate around (idiom); to drag out a person's name

Usually there are always people in the unconvinced say "is a mule is a horse, pull out to slip", Zhao Benshan has a famous saying "walk two steps". In essence, they are all saying that "talking without practicing is a fake". Today, I received an email from a friend who also asked me if I could not remember the contents of python when I was learning it. In fact, there is no need to memorize, I have repeatedly talked about it in the previous courses. However, in the application, it will become more and more skillful.

Here's an exercise that requires it:

Receive any character and number input
Judge the input, if it's not an integer but a character, tell the user; if it's a decimal, also tell the user
If the input is an integer, determine whether the integer is odd or even, and tell the user
In this exercise, it is obviously important to make judgments about the input, and the following points need to be noted by the viewer:

Input content obtained via raw_input() are of type str
To determine whether a string is made up of pure numbers, you can use () (we recommend that the beholder check the official documentation for this built-in function)
The following code is a reference:

Copy Code The code is as follows.

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

print "Please enter the string, then press enter:"

user_input = raw_input()

result = user_input.isdigit()

if not result:
print "What you have entered is not exactly a number"

elif int(user_input)%2==0:
print "You have entered an even number."
elif int(user_input)%2!=0:
print "You have entered an odd number."
else:
print "You didn't enter anything, did you?"

This code is not perfect, there is still room for modification, can you improve it?

How about another one?

A list of integers is known, from which odd and even numbers are jumped and each is placed in a list.

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

Copy Code The code is as follows.

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

import random

numbers = [(1,100) for i in range(20)] # Get a randomized list by parsing it as a list

odd = []
even = []

for x in numbers:
    if x%2==0:
        (x)
    else:
        (x)

print numbers
print "odd:",odd
print "even:",even

Use this example to demonstrate the use of if in list parsing. Let's see if we can continue to improve it some more.

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

Copy Code The code is as follows.

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

import random

numbers = [(1,100) for i in range(20)] # get a randomized list by list parsing

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

For assignments, the viewer should be familiar with them, but for a refresher, see "[Assignments, simple and not so simple]"(. /) and the relevant parts of "[A Formal Word]" (. /).

What does the interesting assignment described here look like? See:

Copy Code 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 summarize: A = Y if X else Z

What it means can be seen in the context of the previous example:

If X is true, then execute A=Y
If X is false, execute A=Z
Look at the example above again, is it executed like this?

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