2025-01-12T07:58:55
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
|
||||
## 조건문
|
||||
|
||||
### if
|
||||
|
||||
`if`문의 조건식 뒤에 콜론(`:`)을 붙이고 `if` 블록은 들여쓰기를 합니다.
|
||||
|
||||
```python
|
||||
@@ -24,9 +26,42 @@ elif 조건식:
|
||||
else:
|
||||
실행문
|
||||
```
|
||||
### match
|
||||
|
||||
Python 3.10에서 추가된 문법입니다.
|
||||
|
||||
```python
|
||||
match (n):
|
||||
case 1: return 'One'
|
||||
case 2: return 'Two'
|
||||
case _: return 'Many'
|
||||
```
|
||||
|
||||
```python
|
||||
case "a" | "b" : return "A or B"
|
||||
```
|
||||
|
||||
```python
|
||||
case [time, name] :
|
||||
return f"{time}, {name}!\n"
|
||||
case [time, *names] :
|
||||
msg = ''
|
||||
for name in names:
|
||||
msg += f"{time}, {name}!\n"
|
||||
return msg
|
||||
```
|
||||
|
||||
```python
|
||||
case [item, amount] if amount < 1000:
|
||||
return amount * 1.2
|
||||
case [item, amount] if amount >=1000:
|
||||
return amount * 1.5
|
||||
```
|
||||
|
||||
## 반복문
|
||||
|
||||
### for in
|
||||
|
||||
```python
|
||||
for 변수 in range(횟수):
|
||||
실행문
|
||||
@@ -47,6 +82,16 @@ for idx, val in enumerate(a):
|
||||
print(idx, val)
|
||||
```
|
||||
|
||||
```python
|
||||
for i in a:
|
||||
# 반복적으로 처리.
|
||||
else:
|
||||
# 반복문이 정상적으로 종료될 떄 한 번만 수행됩니다.
|
||||
# break 등으로 반복이 종료되면 이 블록은 실행되지 않습니다.
|
||||
```
|
||||
|
||||
### while
|
||||
|
||||
```python
|
||||
while 조건식:
|
||||
실행문
|
||||
@@ -59,6 +104,13 @@ while i <= len(a):
|
||||
i += 1
|
||||
```
|
||||
|
||||
```python
|
||||
while i<10:
|
||||
# ...
|
||||
else:
|
||||
# ...
|
||||
```
|
||||
|
||||
## 그 외
|
||||
|
||||
### break
|
||||
|
||||
Reference in New Issue
Block a user