C
Python/Intro/Lesson 03

Data Types

30min·theory
This chapter
2/7
Python

Data Types

🎯 By the end of this lesson

After reading this lesson, you will be able to confidently do the following three things.

  • ✅ 8 types: int · float · str · bool · list · tuple · dict · set
  • ✅ mutable vs immutable (list ↔ tuple, dict ↔ frozenset)
  • ✅ Type hint notation (int, str, list[int])

Keep the learning objectives as a checklist, and close the lesson once you can answer all of them.

5 Python Data Types — Code + Output

In Python, every value is an object. You can check the type of any value with type().


1. int — Integer (unbounded)

python
age = 28
large_number = 10 ** 100      # 10 to the power of 100 — Python has *arbitrary precision*

print(type(age))      # <class 'int'>
print(age + 1)        # 29
print(large_number)           # 10000...000 (100 zeros)

Other languages like Java and C limit int to 32-bit or 64-bit, but Python integers are unlimited up to available memory.


2. float — Floating-point number

python
height = 175.5
pi = 3.14
print(0.1 + 0.2)       # 0.30000000000000004 ← ⚠️ Floating-point trap!

# To compare accurately
import math
print(math.isclose(0.1 + 0.2, 0.3))   # True

Why 0.1 + 0.2 ≠ 0.3: computers use binary, so they cannot represent decimal fractions exactly. Use the Decimal module for financial calculations.


3. str — String (Unicode)

python
name = "Gildong Hong"
greeting = 'Hello'              # Both single and double quotes are OK
long_text = """Multiple
lines"""                  # Triple quotes

print(len(name))          # 12 (character count)
print(name + " " + greeting)  # Gildong Hong Hello
print(name * 3)           # Gildong HongGildong HongGildong Hong
print(f"Name is {name}")   # Name is Gildong Hong (f-string)

+ is for concatenation, is for repetition*.


4. bool — True / False

python
is_student = True
graduated = False

print(is_student and graduated)     # False (only when both are True)
print(is_student or graduated)      # True (if at least one is True)
print(not is_student)          # False (opposite)

# Hidden fact: bool is a subclass of int
print(True + True)       # 2
print(sum([True, False, True]))   # 2

5. None — The absence of a value

python
result = None              # Represents "no value yet"

if result is None:         # ⚠️ Use `is`, not `==`
    print("No value")

If a function has no return statement, it automatically returns None. Similar to null in JavaScript.


6. Type Conversion (Casting)

```python
# String → Number
age_str = "28"
age = int(age_str) # 28
print(age + 1) # 29

# Number → String
score = 95
print("Score: " + str(score)) # "Score: 95"

# Trap — Decimal string cannot be directly converted to int()
# int("3.14") → ValueError
print(int(float(

💻 Data Types Practice
# 4 basic data types
age = 25              # int (integer)
height = 175.5        # float (decimal)
name = "Kim Coding"        # str (string)
is_student = True     # bool (true/false)

# Print data types
print(type(age))       # <class 'int'>
print(type(height))    # <class 'float'>
print(type(name))      # <class 'str'>
print(type(is_student))# <class 'bool'>

# Combining string + number (requires str() conversion!)
print("Age: " + str(age))     # ✅
# print("Age: " + age)        # ❌ Error! Cannot directly add string + number

🐍 Run It — Data Types

Run the concepts above as actual code. The fastest way to learn is to change the values and see for yourself how it behaves.
✏️ Python 코드
📟 Console output
▶ Press the Run button
🐍 Real Python via Pyodide — first run takes 3–5s to load

🤖 Try asking AI like this

Knowing the concepts in this lesson lets you give AI specific instructions. Instead of a vague 'fix this,' you make vocabulary-driven requests — and that's where token savings start.

  • 'Refactor this dict logic into a dataclass'
  • 'Add type hints to these variables'

Why this reduces tokens

Without knowing the concepts, you have to ask 'What does that mean?' again after receiving the AI's answer. That follow-up question is what burns tokens. Learn the concept once, and the conversation ends in one round.

Read this first: What is Python?
Data Types - Python