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!