# Python Conditions and If statements

**1️⃣** 조건문 (Conditional statement)  
**2️⃣** 조건문 연습 (Practice conditional statement)

if 키워드를 사용하여 조건문을 사용한다.  
들여쓰기를 이용하여 코드블록을 표현한다.  
elif 키워드를 사용하여 그 외 조건을 사용한다.  
if, elif 조건 모두 부합하지 않는 조건을 else 키워드를 사용하여 처리한다.

---

## **1️⃣** 조건문  
(Conditional statement)

**조건문(if)은 왜 필요할까? (Why do we need if statement?)**

조건에 부합하면 프로그램을 수행, 안하면 수행하지 않는 것을 기본으로 한다. 이를 위해 if를 사용한다. 그 뒤로는 조건이 온다.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1720503065006/c6155a2c-53d5-4015-9547-4f68b1a885f3.png align="center")

> 위의 이미지는 조건문(If 문)이 왜 필요한지, 특히 실시간 혈압 데이터를 처리할 때의 필요성을 설명하고 있다. 한 사람의 혈압 데이터를 JSON 형식으로 제공하고, 이를 나이대와 성별에 따른 참고 혈압 값과 비교하고 있다. 주어진 데이터 (`age: 25, sex: 'M', maxBP: 130, minBP: 90`)를 사용하여, 25세 남성의 혈압 수치가 정상 범위 내에 있는지 확인하기 위해 조건문을 사용할 수 있다.  
> 즉 실시간 데이터 처리 및 미리 정의된 기준에 따라 결정을 내리기 위해 조건문이 필요함을 보여주고 있다.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1720503149139/9eea80ce-cc8a-4e79-acfd-e01f319bb3b9.png align="center")

---

**if문과 else의 기본 구조 (Basic structure of If & else statement)**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1720504336183/fa5e81ad-0335-4340-8aa8-395fa2bc9289.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1720504356288/8e963c5d-16d3-4018-a5ab-3250c8c36e11.png align="center")

> * **Flowchart Explanation**:
>     
>     1. **시작 Start**
>         
>     2. **입력 Input**
>         
>     3. **조건문 Condition**: The program checks the condition.
>         
>         * If the condition is **true**, it executes `statement1` and `statement2`.
>             
>         * If the condition is **false**, it executes `statementA` and `statementB`.
>             
>     4. **종료 End**
>         
> * **Key Points**:
>     
>     * If the condition is true, the `if` block is executed.
>         
>     * If the condition is false, the `else` block is executed.
>         
>     * 들여쓰기로 코드블록을 표현 Indentation is used to represent code blocks.
>         

---

**If문 사용 (example of If statement)**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1720504480358/e7eebacb-06f7-4f37-a2f5-8f38d1d4c85c.png align="center")

* x는 y보다 큰것이 참이기 때문에 "if 문 수행!" 이 실행되게 된다.
    

**If, else를 활용한 조건문 사용 (example of If & else statement)**

&lt;만약 3000원 이상의 돈을 가지고 있으면 택시를 타고 그렇지 않으면 걸어가라&gt;

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1720504561267/237abfa7-d2d5-4b64-a105-ae91cf956257.png align="center")

* if money &gt;=3000: 에서 콜론(:)를 꼭 붙여야한다.
    
* 위의 statement는 false이기 때문에 "걸어 가라"가 프린트될 것 이다.
    

**elif 사용법 (example of elif)**  
  
elif는 이전 조건문이 거짓일때 수행된다.  
elif는 개수에 제한 없이 사용 가능하다.

&lt;주머니에 돈이 있으면 택시를 타고, 주머니에 돈은 없지만 카드가 있으면 택시를 타고, 돈도 없고, 카드도 없으면 걸어 가라.&gt;

if와 else만으로 코드를 짜게되면 이해하기 어렵고 산만할 수 있다. 위의 예제에서 조건을 판단하는 부분은 2가지이다.

✅ 주머니에 돈(money)이 있는지 판단한다. 참이면, "택시를 타고 가라"를 출력한다.  
✅ 주머니에 돈(money)이 없으면(false) 카드가 있는지 판단한다. 참이면, "택시를 타고 가라"를 출력한다. 거짓이면, "걸어 가라"를 출력한다.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1720505037033/a2734e2f-c76e-4de8-9b43-2f4be7345319.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1720505058221/4efd47be-0484-4c66-817a-2b14f7a5d4ed.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1720505233225/9e309ccc-94c0-4c8c-8bb8-4fc46633feb1.png align="center")

위의 예제에서는 주머니에 돈이나 카드가 있는지 여부에 따라 교통 수단을 결정하였다. elif를 사용하지 않고 중첩된 if 문을 사용하면 코드의 가독성이 떨어지고 복잡해질 수 있다.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1720505409303/007f27a2-3a44-43ee-a119-586267418f16.png align="center")

```apache
print("당신의 나이를 입력하세요")
bloodPressure['age'] = int(input())
```

* int(input()) 에서 입력될 값들은 기본적으로 문자열로 인지를 하기 때문에 숫자열도 인지를 할 수 있도록 설정을 바꿔주는 것이다.
    

---

## QUIZ

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1720526849307/26236738-91ea-4d80-a942-41ef7c35f35e.png align="center")
