Skip to main content

Variables and Data Types

What are Variables?โ€‹

Variables are containers for storing values. Python uses dynamic typing to automatically infer types.

# ๋ณ€์ˆ˜ ์„ ์–ธ๊ณผ ํ• ๋‹น
name = "ํ™๊ธธ๋™"
age = 25
height = 175.5
is_student = True

print(name) # ํ™๊ธธ๋™
print(age) # 25

Variable Naming Rulesโ€‹

# โœ… ์˜ฌ๋ฐ”๋ฅธ ๋ณ€์ˆ˜๋ช…
user_name = "๊น€์ฒ ์ˆ˜"
age2 = 30
_private = "๋‚ด๋ถ€์šฉ"
total_price = 10000

# โŒ ์ž˜๋ชป๋œ ๋ณ€์ˆ˜๋ช…
2age = 30 # ์ˆซ์ž๋กœ ์‹œ์ž‘ ๋ถˆ๊ฐ€
user-name = "" # ํ•˜์ดํ”ˆ ๋ถˆ๊ฐ€
for = 10 # ์˜ˆ์•ฝ์–ด ๋ถˆ๊ฐ€

Naming Conventions

- Use snake_case (Python standard)
- Start with lowercase letters
- Use meaningful names
- Avoid reserved keywords (if, for, class, etc.)

Basic Data Typesโ€‹

1. Numbersโ€‹

Integers (int)

# ์ •์ˆ˜
x = 10
y = -5
big_number = 1000000

# ์—ฐ์‚ฐ
print(10 + 5) # 15 (๋ง์…ˆ)
print(10 - 5) # 5 (๋บ„์…ˆ)
print(10 * 5) # 50 (๊ณฑ์…ˆ)
print(10 / 5) # 2.0 (๋‚˜๋ˆ—์…ˆ - ์‹ค์ˆ˜ ๋ฐ˜ํ™˜)
print(10 // 5) # 2 (์ •์ˆ˜ ๋‚˜๋ˆ—์…ˆ)
print(10 % 3) # 1 (๋‚˜๋จธ์ง€)
print(2 ** 3) # 8 (๊ฑฐ๋“ญ์ œ๊ณฑ)

Floating Point (float)

# ์‹ค์ˆ˜
pi = 3.14
temperature = -15.5
scientific = 1.5e3 # 1500.0

# ์ •๋ฐ€๋„ ์ฃผ์˜
print(0.1 + 0.2) # 0.30000000000000004 (๋ถ€๋™์†Œ์ˆ˜์  ์˜ค์ฐจ)

# ๋ฐ˜์˜ฌ๋ฆผ
result = round(0.1 + 0.2, 1)
print(result) # 0.3

Complex Numbers (complex)

z = 3 + 4j
print(z.real) # 3.0 (์‹ค์ˆ˜๋ถ€)
print(z.imag) # 4.0 (ํ—ˆ์ˆ˜๋ถ€)

2. Stringsโ€‹

# ๋ฌธ์ž์—ด ์ƒ์„ฑ
name = "ํ™๊ธธ๋™"
message = '์•ˆ๋…•ํ•˜์„ธ์š”'
multi_line = """์—ฌ๋Ÿฌ ์ค„
๋ฌธ์ž์—ด
์ž‘์„ฑ"""

# ๋ฌธ์ž์—ด ์—ฐ์‚ฐ
greeting = "์•ˆ๋…•" + "ํ•˜์„ธ์š”" # ์•ˆ๋…•ํ•˜์„ธ์š” (์—ฐ๊ฒฐ)
repeat = "!" * 3 # !!! (๋ฐ˜๋ณต)

# ์ธ๋ฑ์‹ฑ๊ณผ ์Šฌ๋ผ์ด์‹ฑ
text = "Python"
print(text[0]) # P (์ฒซ ๊ธ€์ž)
print(text[-1]) # n (๋งˆ์ง€๋ง‰ ๊ธ€์ž)
print(text[0:3]) # Pyt (0๋ถ€ํ„ฐ 3 ์ „๊นŒ์ง€)
print(text[2:]) # thon (2๋ถ€ํ„ฐ ๋๊นŒ์ง€)
print(text[:4]) # Pyth (์ฒ˜์Œ๋ถ€ํ„ฐ 4 ์ „๊นŒ์ง€)

String Methods

text = "Hello, Python!"

# ๋Œ€์†Œ๋ฌธ์ž ๋ณ€ํ™˜
print(text.upper()) # HELLO, PYTHON!
print(text.lower()) # hello, python!
print(text.capitalize()) # Hello, python!

# ๊ฒ€์ƒ‰
print(text.find("Python")) # 7 (์œ„์น˜)
print(text.count("l")) # 2 (๊ฐœ์ˆ˜)
print("Python" in text) # True (ํฌํ•จ ์—ฌ๋ถ€)

# ๋ณ€ํ™˜
print(text.replace("Python", "World")) # Hello, World!
print(text.split(", ")) # ['Hello', 'Python!']

# ๊ณต๋ฐฑ ์ œ๊ฑฐ
text = " hello "
print(text.strip()) # "hello"
print(text.lstrip()) # "hello "
print(text.rstrip()) # " hello"

String Formatting

name = "ํ™๊ธธ๋™"
age = 25

# f-string (Python 3.6+, ์ถ”์ฒœ)
message = f"์ด๋ฆ„: {name}, ๋‚˜์ด: {age}"
print(message) # ์ด๋ฆ„: ํ™๊ธธ๋™, ๋‚˜์ด: 25

# ๊ณ„์‚ฐ๋„ ๊ฐ€๋Šฅ
print(f"๋‚ด๋…„ ๋‚˜์ด: {age + 1}") # ๋‚ด๋…„ ๋‚˜์ด: 26

# ํฌ๋งท ์ง€์ •
price = 1234.5678
print(f"๊ฐ€๊ฒฉ: {price:.2f}์›") # ๊ฐ€๊ฒฉ: 1234.57์›

# format() ๋ฉ”์„œ๋“œ
message = "์ด๋ฆ„: {}, ๋‚˜์ด: {}".format(name, age)

# % ํฌ๋งคํŒ… (์˜ค๋ž˜๋œ ๋ฐฉ์‹)
message = "์ด๋ฆ„: %s, ๋‚˜์ด: %d" % (name, age)

3. Booleanโ€‹

# ๋ถˆ๋ฆฐ ๊ฐ’
is_active = True
is_deleted = False

# ๋น„๊ต ์—ฐ์‚ฐ
print(5 > 3) # True
print(5 == 5) # True
print(5 != 3) # True

# ๋…ผ๋ฆฌ ์—ฐ์‚ฐ
print(True and False) # False
print(True or False) # True
print(not True) # False

# ์‹ค์ œ ํ™œ์šฉ
age = 20
is_adult = age >= 18
print(is_adult) # True

Truthy and Falsy Values

# False๋กœ ๊ฐ„์ฃผ๋˜๋Š” ๊ฐ’๋“ค
print(bool(0)) # False
print(bool("")) # False
print(bool(None)) # False
print(bool([])) # False
print(bool({})) # False

# True๋กœ ๊ฐ„์ฃผ๋˜๋Š” ๊ฐ’๋“ค
print(bool(1)) # True
print(bool("hello")) # True
print(bool([1, 2])) # True
print(bool({"a": 1})) # True

# ์กฐ๊ฑด๋ฌธ์—์„œ ํ™œ์šฉ
if "hello": # "hello"๋Š” True
print("์‹คํ–‰๋จ")

4. None Typeโ€‹

# None์€ '๊ฐ’์ด ์—†์Œ'์„ ๋‚˜ํƒ€๋ƒ„
result = None

if result is None:
print("๊ฒฐ๊ณผ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค")

# ํ•จ์ˆ˜ ๊ธฐ๋ณธ๊ฐ’
def greet(name=None):
if name is None:
print("์•ˆ๋…•ํ•˜์„ธ์š”!")
else:
print(f"์•ˆ๋…•ํ•˜์„ธ์š”, {name}๋‹˜!")

greet() # ์•ˆ๋…•ํ•˜์„ธ์š”!
greet("ํ™๊ธธ๋™") # ์•ˆ๋…•ํ•˜์„ธ์š”, ํ™๊ธธ๋™๋‹˜!

Type Conversionโ€‹

Explicit Conversionโ€‹

# ๋ฌธ์ž์—ด โ†’ ์ˆซ์ž
age = int("25") # 25
price = float("19.99") # 19.99

# ์ˆซ์ž โ†’ ๋ฌธ์ž์—ด
age_str = str(25) # "25"

# ๋ถˆ๋ฆฐ ๋ณ€ํ™˜
print(bool(1)) # True
print(bool(0)) # False
print(bool("hello")) # True
print(bool("")) # False

# ๋ฆฌ์ŠคํŠธ โ†’ ํŠœํ”Œ
numbers = [1, 2, 3]
numbers_tuple = tuple(numbers) # (1, 2, 3)

Type Checkingโ€‹

age = 25
name = "ํ™๊ธธ๋™"
height = 175.5

# type() ํ•จ์ˆ˜
print(type(age)) # <class 'int'>
print(type(name)) # <class 'str'>
print(type(height)) # <class 'float'>

# isinstance() ํ•จ์ˆ˜
print(isinstance(age, int)) # True
print(isinstance(name, str)) # True
print(isinstance(height, int)) # False

Operatorsโ€‹

Arithmetic Operatorsโ€‹

a = 10
b = 3

print(a + b) # 13 (๋ง์…ˆ)
print(a - b) # 7 (๋บ„์…ˆ)
print(a * b) # 30 (๊ณฑ์…ˆ)
print(a / b) # 3.333... (๋‚˜๋ˆ—์…ˆ)
print(a // b) # 3 (๋ชซ)
print(a % b) # 1 (๋‚˜๋จธ์ง€)
print(a ** b) # 1000 (๊ฑฐ๋“ญ์ œ๊ณฑ)

Comparison Operatorsโ€‹

print(5 == 5)  # True  (๊ฐ™์Œ)
print(5 != 3) # True (๋‹ค๋ฆ„)
print(5 > 3) # True (ํผ)
print(5 < 3) # False (์ž‘์Œ)
print(5 >= 5) # True (ํฌ๊ฑฐ๋‚˜ ๊ฐ™์Œ)
print(5 <= 3) # False (์ž‘๊ฑฐ๋‚˜ ๊ฐ™์Œ)

# ๋ฌธ์ž์—ด ๋น„๊ต
print("a" < "b") # True (์‚ฌ์ „์ˆœ)

Logical Operatorsโ€‹

age = 20
has_license = True

# and: ๋ชจ๋‘ True์—ฌ์•ผ True
can_drive = age >= 18 and has_license
print(can_drive) # True

# or: ํ•˜๋‚˜๋งŒ True๋ฉด True
is_free = age < 7 or age >= 65
print(is_free) # False

# not: ๋ฐ˜๋Œ€
is_not_adult = not (age >= 18)
print(is_not_adult) # False

Assignment Operatorsโ€‹

x = 10

x += 5 # x = x + 5 โ†’ 15
x -= 3 # x = x - 3 โ†’ 12
x *= 2 # x = x * 2 โ†’ 24
x /= 4 # x = x / 4 โ†’ 6.0
x //= 2 # x = x // 2 โ†’ 3.0
x %= 2 # x = x % 2 โ†’ 1.0
x **= 3 # x = x ** 3 โ†’ 1.0

Practical Examplesโ€‹

Temperature Converterโ€‹

def celsius_to_fahrenheit(celsius):
"""์„ญ์”จ๋ฅผ ํ™”์”จ๋กœ ๋ณ€ํ™˜"""
fahrenheit = (celsius * 9/5) + 32
return fahrenheit

def fahrenheit_to_celsius(fahrenheit):
"""ํ™”์”จ๋ฅผ ์„ญ์”จ๋กœ ๋ณ€ํ™˜"""
celsius = (fahrenheit - 32) * 5/9
return celsius

# ์‚ฌ์šฉ
c = 25
f = celsius_to_fahrenheit(c)
print(f"{c}ยฐC = {f:.1f}ยฐF") # 25ยฐC = 77.0ยฐF

f = 77
c = fahrenheit_to_celsius(f)
print(f"{f}ยฐF = {c:.1f}ยฐC") # 77ยฐF = 25.0ยฐC

BMI Calculatorโ€‹

def calculate_bmi(weight, height):
"""BMI ๊ณ„์‚ฐ (์ฒด์ค‘kg, ํ‚คm)"""
bmi = weight / (height ** 2)
return round(bmi, 1)

def get_bmi_category(bmi):
"""BMI ์นดํ…Œ๊ณ ๋ฆฌ ๋ฐ˜ํ™˜"""
if bmi < 18.5:
return "์ €์ฒด์ค‘"
elif bmi < 23:
return "์ •์ƒ"
elif bmi < 25:
return "๊ณผ์ฒด์ค‘"
else:
return "๋น„๋งŒ"

# ์‚ฌ์šฉ
weight = 70 # kg
height = 1.75 # m

bmi = calculate_bmi(weight, height)
category = get_bmi_category(bmi)

print(f"BMI: {bmi}")
print(f"๋ถ„๋ฅ˜: {category}")

Text Analyzerโ€‹

def analyze_text(text):
"""ํ…์ŠคํŠธ ๋ถ„์„"""
length = len(text)
words = len(text.split())
uppercase = sum(1 for c in text if c.isupper())
lowercase = sum(1 for c in text if c.islower())
digits = sum(1 for c in text if c.isdigit())

print(f"=== ํ…์ŠคํŠธ ๋ถ„์„ ===")
print(f"์ „์ฒด ๊ธธ์ด: {length}")
print(f"๋‹จ์–ด ์ˆ˜: {words}")
print(f"๋Œ€๋ฌธ์ž: {uppercase}")
print(f"์†Œ๋ฌธ์ž: {lowercase}")
print(f"์ˆซ์ž: {digits}")

# ์‚ฌ์šฉ
text = "Hello Python 2024!"
analyze_text(text)

Getting Inputโ€‹

# ๊ธฐ๋ณธ ์ž…๋ ฅ
name = input("์ด๋ฆ„์„ ์ž…๋ ฅํ•˜์„ธ์š”: ")
print(f"์•ˆ๋…•ํ•˜์„ธ์š”, {name}๋‹˜!")

# ์ˆซ์ž ์ž…๋ ฅ (๋ณ€ํ™˜ ํ•„์š”)
age = int(input("๋‚˜์ด๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”: "))
next_year_age = age + 1
print(f"๋‚ด๋…„ ๋‚˜์ด: {next_year_age}")

# ์—ฌ๋Ÿฌ ๊ฐ’ ์ž…๋ ฅ
text = input("๋‘ ์ˆซ์ž๋ฅผ ๋„์–ด์“ฐ๊ธฐ๋กœ ์ž…๋ ฅ: ")
a, b = map(int, text.split())
print(f"ํ•ฉ: {a + b}")

Frequently Asked Questionsโ€‹

Q1. Do I need to specify types when declaring variables?โ€‹

A: Python uses dynamic typing, so types are determined automatically.

# ํƒ€์ž… ์ž๋™ ์ถ”๋ก 
x = 10 # int
x = "hello" # ์ด์ œ str (์žฌํ• ๋‹น ๊ฐ€๋Šฅ)

# ํƒ€์ž… ํžŒํŠธ๋Š” ๊ฐ€๋Šฅ (๊ฒ€์ฆ์€ ์•ˆ ๋จ)
age: int = 25
name: str = "ํ™๊ธธ๋™"

Q2. What's the difference between (/) and (//) division?โ€‹

A: / returns a float, // returns an integer

print(7 / 2)   # 3.5 (์‹ค์ˆ˜)
print(7 // 2) # 3 (์ •์ˆ˜)

# ์Œ์ˆ˜๋„ ๋งˆ์ฐฌ๊ฐ€์ง€
print(-7 // 2) # -4 (๋‚ด๋ฆผ)

Q3. What does immutable mean for strings?โ€‹

A: Strings cannot be modified after creation.

text = "hello"
# text[0] = "H" # โŒ ์—๋Ÿฌ!

# ์ƒˆ ๋ฌธ์ž์—ด ์ƒ์„ฑํ•ด์•ผ ํ•จ
text = "H" + text[1:] # "Hello"

Q4. What's the difference between is and ==?โ€‹

A: is checks object identity, == checks value equality

a = [1, 2, 3]
b = [1, 2, 3]
c = a

print(a == b) # True (๊ฐ’์ด ๊ฐ™์Œ)
print(a is b) # False (๋‹ค๋ฅธ ๊ฐ์ฒด)
print(a is c) # True (๊ฐ™์€ ๊ฐ์ฒด)

# None ๋น„๊ต๋Š” is ์‚ฌ์šฉ
x = None
print(x is None) # โœ… ๊ถŒ์žฅ
print(x == None) # ๊ฐ€๋Šฅํ•˜์ง€๋งŒ ๋น„๊ถŒ์žฅ

Next Stepsโ€‹

You've learned about variables and basic types!

Key Takeaways:
โœ… Variable declaration and naming conventions
โœ… Numbers, strings, booleans, None
โœ… Type conversion and checking
โœ… Various operators
โœ… Getting input

Next Step: Learn more about strings in String Processing!