# Variable and operator in Python

Contents

**1️⃣**변수의 개념 및 사용 (Concept and use of variables)  
**2️⃣**파이썬 연산자 (Python operator)  
**3️⃣**변수 및 연산자 실습 (Variable and operator practice

---

## **1️⃣**변수의 개념 및 사용

## (Concept and use of variables)

**✅ 변수란?( What is variables?)**

<mark>데이터를 저장하는 메모리 공간을 뜻</mark>한다. <mark>변수의 데이터는 언제든 변경이 가능</mark>하다. <mark>변수 명으로 키워드는 사용 불가능</mark>하다. <mark>변수명은 숫자로 시작할 수 없다</mark>. 변수를 선언하고 데이터가 들어가는 일련의 과정들을 그림으로 표현하면 아래와 같다.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719466879182/c46f7fca-eedd-47cc-8a2e-42dcad998ddd.png align="center")

* 호텔에서 예약을 할 때는 어느 호수에서 묵는지는 처음부터 정해져 있지 않는다. 방의 모양, 넓이는 정해져 있지만 어느 호수에서 묵을지는 체크인 할 때 그날 상황에 따라서 달라진다. 직원은 투숙객의 상황 (가족, 아이여부, 단체손님, 커플 등)에 따라서 최적의 방을 배정한다. 이 상황은 프로그래밍에서의 변수 선언과 흡사하다.
    
* 사진에서 보이는 RAM은 프로그램이 바로 접근할 수 있는 권한이 없다. 파이썬(프로그램)은 운영체제(OS)에 요청하고 OS는 메모리 공간을 확인 후 파이썬 프로그램에서 요청한 최적의 메모리 공간을 확보해준다. 이에 대한 결과로 변수가 선언이 된다.
    

**✅ 파이썬에서의 변수 선언 방법(The method of declaring variables in Python)**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719467383933/2f226fe8-2852-45bc-a1f0-c2b9f83d4db5.png align="center")

* 1, "python", 1.34라는 데이터가 존재할 때 " = "를 사용한다. assignment라고도 한다.
    
* 변수 이름을 먼저 지정하고 (a, b, c) 변수에 저장할 값을 저장한다.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719467695664/fc29aae6-d2b8-4556-b548-532de17924e8.png align="center")

* 파이썬은 변수의 저장된 값을 확인하고 자동으로 메모리에 할당한다. ( 변수 a가 301에 있는 이유)
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719467791404/5203eed0-0299-4a28-ad09-9ef0d3a5c6c5.png align="center")

* a, b = b, a 는 스와핑(swapping)이라고도 한다.
    
* 변수는 메모리 공간의 주소를 가르키는 역할을 하는데 프로그래머가 일일이 주소를 외우지 못한다. 스와핑을 통해 연산 최적화가 가능하다.
    

**✅ 변수 선언의 규칙(Rules to Declare a Variable)**

영문자, 숫자, 언더바(\_)를 사용할 수 있다.  
첫글자는 소문자를 사용한다. (권장하는 사항이다. 대문자로 입력해도 오류가 나지 않지만 헷갈릴 수 있다.)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719573492087/b3ffe997-d6f3-4379-b9c9-d8c10acef1b0.png align="center")

첫 자리에는 숫자를 사용할 수 없다.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719573008842/f3453d0d-56b4-46e6-bd49-bb3290985229.png align="center")

  
파이썬의 키워드는 변수 명으로 사용할 수 없다. (False, None, True, and, as, assert, break, class, continue, def, del, elif, else, except, finally, for, from, global, if, import, in, is, lambda, nonlocal, not, or, pass, raise, return, try, while, with, yield)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719573069751/ba97b3c4-87aa-4c2e-a499-330a777ca621.png align="center")

---

## **2️⃣**파이썬 연산자 5가지  
(5 Types of Python operator)

**✅ 산술 연산자(Arithmetic Operators)**

* 더하기, 빼기, 곱하기,나누기 연산은 쉽게 알아볼수 있으므로 생략
    
* 정수 몫(//, Integer Division): 10을 3으로 나누었을 때의 결과 값을 //로 계산
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719485865849/f2272801-2f0b-4d17-a92a-468c3aa7f1b1.png align="center")

* 나머지 연산(%, Modulus Operation): 10을 3으로 나누고 나머지 값을 %로 계산
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719485956410/7d188d69-b438-47a8-baba-8c44534b1052.png align="center")

* 누승 연산(\*\*, Exponentiation): a^b의 값을 구한다 (제곱)
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719486002651/d7864458-3a12-4c2b-a72e-9b5df47b80fb.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719486180879/6bf834ad-ca9f-43e8-939a-10b704e2234e.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719486241196/961e00d0-e820-4d02-9826-737e57645a05.png align="center")

**✅ 관계 연산자(Relational Operators)**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719725356099/bcbf1c73-6508-4318-960e-46c1affb70a2.png align="center")

관계 연산자의 결과는 True or False로 출력된다.  
\&gt;:크다, &gt;=: 크거나 같다, &lt;=: 작거나 같다, ==: 같다, != : 같지 않다  
기본적인 내용이므로 추가설명은 생략하고 같지 않다만 추가 설명

> != 같지 않다
> 
> ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719486582387/4f923f08-ab2c-4ac7-8ffd-2310041ab3d7.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719486638645/e5eb73ea-771a-412c-a4b1-616ac8ab90f1.png align="center")

> 생년월일 (year\_of\_birth)을 10으로 나눈 값의 나머지(%)는 == 7과 같나?

**  
✅ 논리 연산자(Logical Operators)**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719725551128/cec58d71-230c-40cd-a27e-263abd844c06.png align="center")

논리 값을 판단해 주는 연산자이다. a and b일때 a와 b 모두가 true여야 결과도 true 이다.

and: 논리곱, or: 논리합, not: 논리 부정

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719488066752/5804887d-90cb-4aed-a6cc-e86276a9e791.png align="center")

> and, or 설명 생략하고 not만 설명
> 
> 논리부정(Logical Negation)은 논리값을 바꿔주는 것이다. not a에서는 a가 False 여야 true 가 뜬다.
> 
> ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719488262391/2039fec3-0d55-4029-b9d3-b5122e573caa.png align="center")
> 
> 생년 끝자리가 2, 7만 구매 가능한 경우는 or 논리 연산자로 표현이 가능하다.

**✅복합 대입 연산자(Compound Assignment Operators)**

복합대입 연산자는 연산과 할당을 합쳐 놓은 것이다. 사용하면 식을 간결하게 사용이 가능 하다. 변수가 이전에 가졌던 값을 수정하여 할당하는 경우에 사용한다. 과도한 사용은 가독성을 떨어뜨릴 수 있다.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719558879581/8696904d-7ccc-47ae-bd4f-70bcd73691e3.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719723189874/5bf1f54b-ce27-4212-9e85-8193a4e772d4.png align="center")

* **누승, Exponentiation (**`**`): `c = a ** b`는 `a`의 `b`승을 계산한다. 이 경우, `10 ** 3 = 1000`이다.
    
* **나머지, Modulus (**`%`): `c = a % b`는 `a`를 `b`로 나누었을 때의 나머지를 계산한다. 이 경우, `10 % 3 = 1`이다.
    
* **정수 몫, Integer division (**`//`): `c = a // b`는 `a`를 `b`로 나누어 나눈 값의 정수 몫을 계산한다. 이 경우, `10 // 3 = 3`이다.
    
    > 동치란 같다는 뜻이다.
    > 
    > ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719569960190/c01bf817-efc9-4b1a-8332-cd0446bde700.png align="center")
    > 
    > total = total + cola 와 비교시 total += cola 가 훨씬 간결하다.  
    > total = total + hamburger 와 비교했을 때 total +=hamburger가 간결하다.
    > 
    > 프로그래밍에선 가독성이 더 높은 복합 대입 연산자를 사용한다.
    

**✅연산자 우선 순위(Operator Precedence)**

연산의 결과가 매우 민감한 프로그램이라면 연산자 우선 순위를 확인해서 실수를 하지 않도록 해야한다. 특히 항공기, 우주비행기, 의료 관련은 중요하다.

![Uploaded image](https://files.oaiusercontent.com/file-F9uBfs7UiBPGDeWplriX5L5i?se=2024-06-28T10%3A27%3A48Z&sp=r&sv=2023-11-03&sr=b&rscc=max-age%3D299%2C%20immutable&rscd=attachment%3B%20filename%3Dimage.png&sig=SWSLNj4HkTigVSzn4e5X/4MeaANYAhJrWKHbx96qGtQ%3D align="left")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719570253185/983d10b4-a9a0-444b-a874-cb1430654ba9.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719570719831/7a7db4ac-7a69-42dd-89eb-3125c625851f.png align="center")

> cola\*nCola의 값과 Hamburger\*nHamburger의 값이 곱해지고 난뒤 + 더하기 하고, 1-discount의 결과값과 곱한다. (우선순위적용)

---

## Quiz

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719726439350/7f785fa8-183e-4dfb-a083-18c32a935979.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719726600442/096aa7d3-a27e-4a14-8618-99fcff68d56e.png align="center")
