mirror of
https://github.com/apachecn/ailearning.git
synced 2026-04-24 10:34:08 +08:00
2020-10-19 21:08:55
This commit is contained in:
@@ -6,7 +6,7 @@
|
||||
|
||||
In [1]:
|
||||
|
||||
```
|
||||
```py
|
||||
x = 0.5
|
||||
if x > 0:
|
||||
print "Hey!"
|
||||
@@ -14,7 +14,7 @@ if x > 0:
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
Hey!
|
||||
x is positive
|
||||
|
||||
@@ -26,7 +26,7 @@ x is positive
|
||||
|
||||
上面例子中的这两条语句:
|
||||
|
||||
```
|
||||
```py
|
||||
print "Hey!"
|
||||
print "x is positive"
|
||||
|
||||
@@ -40,7 +40,7 @@ print "Hey!"
|
||||
|
||||
In [2]:
|
||||
|
||||
```
|
||||
```py
|
||||
x = 0.5
|
||||
if x > 0:
|
||||
print "Hey!"
|
||||
@@ -50,7 +50,7 @@ print "This isn't part of the block, and will always print."
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
Hey!
|
||||
x is positive
|
||||
This is still part of the block
|
||||
@@ -62,7 +62,7 @@ This isn't part of the block, and will always print.
|
||||
|
||||
In [3]:
|
||||
|
||||
```
|
||||
```py
|
||||
x = -0.5
|
||||
if x > 0:
|
||||
print "Hey!"
|
||||
@@ -72,7 +72,7 @@ print "This isn't part of the block, and will always print."
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
This isn't part of the block, and will always print.
|
||||
|
||||
```
|
||||
@@ -81,7 +81,7 @@ This isn't part of the block, and will always print.
|
||||
|
||||
一个完整的 `if` 结构通常如下所示(注意:条件后的 `:` 是必须要的,缩进值需要一样):
|
||||
|
||||
```
|
||||
```py
|
||||
if <condition 1>:
|
||||
<statement 1>
|
||||
<statement 2>
|
||||
@@ -97,7 +97,7 @@ else:
|
||||
|
||||
In [4]:
|
||||
|
||||
```
|
||||
```py
|
||||
x = 0
|
||||
if x > 0:
|
||||
print "x is positive"
|
||||
@@ -108,7 +108,7 @@ else:
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
x is zero
|
||||
|
||||
```
|
||||
@@ -121,7 +121,7 @@ x is zero
|
||||
|
||||
In [5]:
|
||||
|
||||
```
|
||||
```py
|
||||
x = 10
|
||||
y = -5
|
||||
x > 0 and y < 0
|
||||
@@ -130,33 +130,33 @@ x > 0 and y < 0
|
||||
|
||||
Out[5]:
|
||||
|
||||
```
|
||||
```py
|
||||
True
|
||||
```
|
||||
|
||||
In [6]:
|
||||
|
||||
```
|
||||
```py
|
||||
not x > 0
|
||||
|
||||
```
|
||||
|
||||
Out[6]:
|
||||
|
||||
```
|
||||
```py
|
||||
False
|
||||
```
|
||||
|
||||
In [7]:
|
||||
|
||||
```
|
||||
```py
|
||||
x < 0 or y < 0
|
||||
|
||||
```
|
||||
|
||||
Out[7]:
|
||||
|
||||
```
|
||||
```py
|
||||
True
|
||||
```
|
||||
|
||||
@@ -164,7 +164,7 @@ True
|
||||
|
||||
In [8]:
|
||||
|
||||
```
|
||||
```py
|
||||
year = 1900
|
||||
if year % 400 == 0:
|
||||
print "This is a leap year!"
|
||||
@@ -176,7 +176,7 @@ else:
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
This is not a leap year.
|
||||
|
||||
```
|
||||
@@ -194,7 +194,7 @@ This is not a leap year.
|
||||
|
||||
In [9]:
|
||||
|
||||
```
|
||||
```py
|
||||
mylist = [3, 1, 4, 1, 5, 9]
|
||||
if mylist:
|
||||
print "The first element is:", mylist[0]
|
||||
@@ -203,7 +203,7 @@ else:
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
The first element is: 3
|
||||
|
||||
```
|
||||
@@ -212,7 +212,7 @@ The first element is: 3
|
||||
|
||||
In [10]:
|
||||
|
||||
```
|
||||
```py
|
||||
mylist = []
|
||||
if mylist:
|
||||
print "The first element is:", mylist[0]
|
||||
@@ -221,7 +221,7 @@ else:
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
There is no first element.
|
||||
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user