# (1/2)Understanding Data types in Python

**1️⃣** 데이터 타입의 개념(The concept of data types)  
**2️⃣** 숫자형 (Numeric type)  
**3️⃣** 문자열 자료형 (String type)  
**4️⃣** 리스트 자료형 (List type)

---

## **1️⃣** 데이터 타입의 개념  
(The concept of data types)

<mark>데이터를 저장하는 방식</mark>이다. 변수의 데이터 타입에 따라 데이터를 메모리에 어떻게 저장하는 방식이 각기 다르다. 자동차, 기차, 트럭등 운송 목적과 방법에 따라 각기 다른 운송 수단을 이용하는 것과 흡사하다.

프로그래밍을 할 때도 각각의 데이터가 어느정도의 용량을 차지하는지 고려해야한다. 숫자, 문자열 등을 담는 공간을 고려하여 메모리에 분담을 해야하는데 데이터 타입은 이러한 분담을 쉽게 돕기 위해 분류된 타입이다.

<mark>파이썬에서는 데이터 타입에 대한 명시가 없어도 변수에 입력되는 데이터에 따라 데이터 타입이 자동으로 할당된다.</mark>

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719732496101/6474b257-b210-4ff4-b14c-3ed7426d463a.png align="center")

**✅ 데이터 타입의 종류 (Type of Data type)**

* **<mark>숫자형</mark>**<mark>: Numeric type</mark>
    
    * `123`
        
* **<mark>문자열 자료형</mark>**<mark>: String type</mark>
    
    * `"hello world"`
        
* **<mark>리스트 자료형</mark>**<mark>: List type</mark>
    
    * `[1, 3, 5, 7, 9]`
        
* **<mark>딕셔너리 자료형</mark>**<mark>: Dictionary type </mark> 속성(key), 값(value)을 적어야 할때 사용하는 데이터 타입이다.
    
    * `{"name": "Kim", "phone": "010-2222-3333"}`
        
* **<mark>집합 자료형</mark>**<mark>: Set type</mark>
    
    * `set([1, 2, 3, 4, 5, 6])`
        
* **<mark>불 자료형</mark>**<mark>: Boolean </mark> type 참(true),거짓(false)만을 담는 데이터 타입이다.
    
    * `True`
        

**✅ 데이터 타입의 사용 (Usage of Data type)**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719734073023/efccd820-b885-4940-b62f-278c9b952c8d.png align="center")

* a = 숫자형, b = 문자열, c = 리스트 자료형이다.
    
* <mark>변수명 = 데이터 입력</mark> -&gt;이런식으로 데이터 타입이 활용된다.
    

---

## **2️⃣** 숫자형 (Numeric type)

숫자 형태로 이루어진 자료형이다. 모든 항목마다 메모리에 저장되는 공간이 다르다.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719742002303/626d5fec-8a68-491e-b0c4-ab1e52a16f41.png align="center")

**✅숫자형 사용법 (How to use Numeric type)**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719742047223/b19b3774-4894-4b24-a1b2-786fdcd03f5f.png align="center")

* 음수든 양수든 정수(Integer)로 취급된다.
    
* 실수(floating-point)는 더 많은 데이터를 요한다.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719744874723/fd503167-d5a8-4993-abf2-d5fae0813f61.png align="center")

* 8진수는 2진법으로 변환이 가능하다.
    
* 간판, 사인 등 on/off가 요구되는 프로그래밍할때 2진법을 활용을 많이한다.
    

**✅Operator with Numeric type**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719745035802/d2589380-692f-4eeb-a180-65a82e13b54d.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719748553186/6ab45ba6-4415-4a4b-8111-1c3b698629a0.png align="center")

---

## **3️⃣** 문자열 자료형 (String type)

**✅문자열(string) 이란?** 문자, 단어 등으로 구성된 문자들의 집합을 뜻한다.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719748731511/0332a7ec-065d-4b73-8635-1a8a1251ccce.png align="center")

**✅문자열 만드는 방법 4가지 ( 4 types of string)**

<mark>큰 따옴표, Double quotation mark (") </mark> e.g **"Hello World"**  
<mark>작은 따옴표, Single quotation mark (') </mark> e.g **'Python is fun'**  
<mark>큰 따옴표 3개, Triple double quotation marks (""')</mark>  
e.g: **""" List is too short, you need Python """**  
<mark>작은 따옴표 3개, Triple single quotation marks (''')</mark>  
e.g: **''' List is too short, you need Python '''**

**✅ 문자열 사용법 (How to use string)**

여러 줄인 문자열을 변수에 대입하고 싶을 때는 어떻게 할까?

이스케이프 코드 **\\n 삽입** ( \\ : backward slash)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719750568085/852ea8db-2fec-433a-a8c4-72f70e24eebc.png align="center")

* \\n 기점으로 줄이 바뀐다.
    

**작은 따옴표 3개 (''')**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719750614293/2b899244-9c8e-47b7-9b4e-b1c255d3e314.png align="center")

**큰 따옴표 3개 (""")**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719750622667/44f8f5d9-8724-4a30-9e61-370edb9c8bb0.png align="center")

**✅문자열을 연산한다면? (How to calculate a string)**

문자열 더하기 (addition of string) : 연결하기, Concatenation를 사용한다.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719750721776/6c24bc8c-7e26-44d0-aa7e-15f2f5e86e9f.png align="center")

* result = head + tail 의 결과값으로 python is fun 이 나온다.
    

문자열을 곱하기 (multiply of string) : 곱하기 연산자 (\*)를 사용한다.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719750836005/69f71aeb-45e0-4bb0-bc1b-0023ba839358.png align="center")

문자열의 길이 구하기 (length of string) : 파이썬의 기본 내장 함수인 len()을 사용한다.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719750900616/2a3d59ed-47c4-4296-8124-fc0e200e1037.png align="center")

**✅문자열 인덱싱(indexing)이란?**

'가르킨다'는 의미이다. 인덱스는 0부터 시작한다. (프로그래밍의 기본은 0부터 시작한다)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719751107681/b0521b4f-7975-401a-8f7f-934d60ac6ad0.png align="center")

* 문자중에 특정 문자를 추출하고 싶을 때 "인덱싱(indexing)"을 사용할 수 있다.
    
* " Life is too short, You need Python" 에서 L을 추출하고 싶다면? a\[0\]이다.
    
* s는 a\[12\]이다. 즉 a\[인덱스 번호\]는 문자열 안의 특정 값을 뽑아 낸다.
    
* 프로그래밍할 때 뒤에서 세는게 더 빠를 때가 있다. 위의 예제에선 Python이 뒤에서 더 가깝기 때문에 마이너스, - 로 인덱싱이 가능하다.
    
* n은 a\[-1\], o는 a\[-2\], h는 \[-3\]이다.
    

**✅문자열 슬라이싱(slicing)이란?**

잘라낸다는 의미이다. a\[시작 번호:끝번호\] ➡️시작 번호부터 끝 번호 전까지의 문자를 뽑아낸다.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719751504077/f6db05ac-a958-4330-bede-9954f96113a7.png align="center")

* 첫번째 박스 a\[0:4\] a의 시작번호 0은 L이다. 끝번호의 4는 e이다. 'Life'
    
* 두번째 박스의 date = a\[:8\]를 보면 시작번호가 없다. 이 뜻은 시작부터 라는 뜻이다.
    
* 또한 weather의 a\[8:\]에선 끝번호가 생략되어있는데 이 뜻은 끝까지 라는 뜻이다.
    

**✅ 문자열 formatting 이란?**

문자열의 format코드를 이용해서 문자열의 변할 수 있는 변수를 대입하는 것이다.

  
**숫자 바로 대입 ( 문자열 format 코드 %d)**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719751914846/2cad45ce-7003-4d97-947c-6cb2792b6f5d.png align="center")

* 숫자를 대입하고 싶은 곳에 %d 를 입력하면 십진법으로 대입되어 출력 된다.
    
* 두번째 예제처럼 number를 변수로 지정하면 편하다.
    

**문자열 바로 대입 (문자열 format 코드 %s)**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719752011954/b11f96d9-21ed-4fdc-a4f2-0e2d03d22ec0.png align="center")

* 문자를 대입하고 싶은 곳에 %s를 입력한다.
    

**다양한 문자열 format 코드 (Various type of string format code)**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719752072643/ed92d628-00c3-48ea-86eb-015b0b104ce7.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719752314154/3f45f3e1-5dc6-4b2e-a205-93405d648b68.png align="center")

* 문자열 format 코드를 이용하면 다양한 변수를 사용할 수 있다.
    

**문자열 format 코드 활용법 3가지 (How to use string format code - 3 ways)**

> * 정렬과 공백 : Alignment and spacing
>     
> * 소수점 표현하기 : Representing floating-point numbers
>     
> * f 문자열 formatting : f-string formatting
>     

**정렬과 공백(Alignment and spacing):** %s(문자열)를 숫자와 함께 사용하면, 공백과 정렬 표현 가능하다.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719752691150/f632ce6f-864f-4352-b8ee-15c4e2bd8852.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719752727543/16e8f720-e69a-42f0-a570-3a9549548691.png align="center")

* 마이너스 (-) 쓰게 되면 왼쪽으로 정렬된다.
    

**소수점 표현하기(Representing floating-point numbers)** %f(부동소수)를 숫자와 함께 사용하면, 소수점 뒤에 나올 숫자의 개수 조절 및 정렬 가능하다.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719752831826/031819fc-3948-422b-9f7c-291b55ea26a5.png align="center")

* 두번째 예제는 기본적으로 오른쪽으로 정렬된다. (마이너스 포함시 왼쪽 정렬)
    

**f 문자열 formatting(f-string formatting)** 파이썬 3.6 버전부터 f 문자열 format 기능을 제공한다. 문자열 앞에 f 접두사를 붙이면, f 문자열 format 기능이 사용 가능하다.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719752927479/64660ee0-29ca-4e94-a473-d489c49864af.png align="center")

* f를 붙여야 {}를 활용해 string을 입력할 수 있다.
    

**문자열 관련 함수 (String related functions)**

> count() : 문자 개수를 세는 함수
> 
> join() : 문자열 삽입
> 
> find() : 찾는 문자열이 처음 나온 위치를 반환
> 
> upper() : 소문자를 대문자로 / lower() : 대문자를 소문자로
> 
> replace() : 문자열 안의 특정 값을 다른 값으로 치환
> 
> split() : 공백 또는 특정 문자열을 구분자로 해서 문자열 분리

count() : 문자 개수를 세는 함수

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719753074022/06eeb038-bbc0-4f54-81ef-3a677e9ea389.png align="center")

join() : 문자열 삽입 - 문자마다 , (comma)가 붙게된다.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719753142956/2abcee1e-5855-4c46-b11f-0a51d8cfee28.png align="center")

find() : 찾는 문자열이 처음 나온 위치를 반환 - 위치는 인덱스로 표현된다. 공백도 센다. 마지막 예제에선 k가 포함되지 않기 때문에 -1로 반환되었다.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719753221017/c122ecb0-33e0-49ee-9877-d61dd058e05d.png align="center")

upper() : 소문자를 대문자로 / lower() : 대문자를 소문자로 변환이 된다.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719753291456/95ffaf71-0c93-4680-a692-63b39e20b511.png align="center")

  
replace() : 문자열 안의 특정 값을 다른 값으로 치환(change)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719753371263/a204831f-22e5-4b79-b4f5-c2423de08152.png align="center")

split() : 공백 또는 특정 문자열을 구분자로 해서 문자열 분리

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719753410896/b5eec37d-9bf7-4ecc-aa4f-b88d370b6742.png align="center")

---

## **4️⃣** 리스트 자료형 (List type)

자료형의 집합을 표현할 수 있는 자료형이다.  
  
**✅리스트 자료형 사용법 (How to use List type)**  
  
리스트 자료형은 리스트명 = \[data1, data2 .. \] 와 같이 사용한다. (대괄호(\[\])로 감싸고 각 요솟값은 쉼표(,)로 구분한다.)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719755356507/442af057-519d-4da6-bccb-4cc7fb79164c.png align="center")

리스트를 이용해 데이터의 삽입, 삭제, 검색이 가능하다.  
리스트 안에는 어떠한 자료형도 포함이 가능하다.  
파이썬에서는 리스트 자료형에 기본적인 함수가 내장되어 데이터의 조작이 가능하다.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719755374056/6bd9147e-af53-4427-8adc-e3740c828007.png align="center")

  
숫자와 문자열만으로 프로그래밍 하기엔 부족한 점이 많기 때문에 리스트 자료형은 기존에 데이터가 저장된 상황에서 특정 데이터를 추가, 삭제, 검색 하기에 용이하다.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719755288589/f0250119-88f3-436a-b195-9bbf658c1e67.png align="center")

**✅ 리스트 인덱싱 (List indexing)** 문자열과 같이 인덱싱이 적용 가능하다.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719755537306/e64dfc35-0932-4d81-8a44-bb5e7fd60741.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719755542223/5edb39b1-8f5d-4e38-b2aa-26da5db87d45.png align="center")

**✅ 리스트 연산하기 (List type operate)**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719755598495/861962bf-d62e-4bd1-b561-b7d023600ad1.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719755639388/76510285-c6bd-4d27-ba38-c820ac03f31e.png align="center")

**✅리스트 관련 함수 (List related functions)**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719755766989/fd7a0854-bb02-4e01-bad6-775d0c036032.png align="center")

* index() 내가 찾고자하는 데이터를 적어주면 리스트에서 찾아 인덱스에 반환한다. a.index(3)은 3을 찾는다는 뜻인데 2로 반환되었다.
    
* sort() 정렬 함수이다. 오름차순 기준이다.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719754855060/d4954fdb-8a7c-4bfa-a9f7-7aa7a65914c0.png align="center")

* insert(시작시점, 삽입할요소) ➡️ a.insert(0,4)
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719754937971/69d69b76-22f9-47b7-bea4-36b0fce17a0c.png align="center")

# Quiz

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719755019547/6b16cd85-f2bb-4fc5-b28f-964f99029f22.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719755034574/0d655051-a171-41c1-81d7-1fce7503f02f.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719755046436/f4bcb6d7-5e97-4fd4-b00f-47d3cdd4b255.png align="center")
