** * This article is from Udemy "[Introduction to Python3 taught by active Silicon Valley engineers + application + American Silicon Valley style code style](https://www.udemy.com/course/python-beginner/" Introduction to Python3 taught by active Silicon Valley engineers + application + American Silicon Valley style code style ")" It is a class notebook for myself after taking the course of. It is open to the public with permission from the instructor Jun Sakai. ** **
if
x = -10
if x < 0:
print('negative')
result
negative
You must put a `` (space) at the beginning of the line following the line that uses ʻif`. You only need one space, but Python has an implicit rule of four spaces.
◆else
else
x = 10
if x < 0:
print('negative')
else:
print('positive')
```
#### **`result`**
```python
positive
```
By using ʻelse`, the case of" otherwise "can be specified.
◆`elif`
#### **`elif`**
```python
x = 0
if x < 0:
print('negative')
elif x == 0:
print('zero')
else:
print('positive')
```
#### **`result`**
```python
zero
```
ʻElif` is an abbreviation for "else if".
By the way, ʻa == b` means "a and b are equal".
(If a = b, b will be assigned to a)
##### ◆ if statement in if statement
#### **`if_in_if`**
```python
a = 1
b = 2
if a > 0:
print('a is positive')
if b > 0:
print('b is also positive')
```
#### **`result`**
```python
a is positive
b is also positive
```
At this time, the indent (indentation) of the second if statement must be aligned.
#### **`if_in_if`**
```python
a = 1
b = 2
if a > 0:
print('a is positve')
if b > 0:
print('b is also positive')
```
#### **`result`**
```python
File "/Users/fumiya/PycharmProjects/python_programming/lesson.py", line 6
if b > 0:
^
IndentationError: unexpected indent
```
An error occurred because the indentation was incorrect.
Recommended Posts