[Week 4] Bringing Data to Life: Control Flow and Functions πŸ‘¨β€πŸ’»

English translation

Hello! Welcome to Week 4.

Last time, we learned to store data using Python variables and data structures.

Today, we will go a step further and learn how to 'process' and 'manipulate' stored data.

Use if statements to select data meeting particular conditions,

Process hundreds or tens of thousands of data items using for loops,

Learn how to turn this entire process into a reusable component called a function (def).

🚦 Module 1: Code That Follows Conditions β€” if Statements

# Example: Decide whether to recommend a movie based on its rating
movie_rating = 9.1

# 'If' movie_rating is greater than or equal to 9.0
if movie_rating >= 9.0:
    print("✨ κ°•λ ₯ μΆ”μ²œν•˜λŠ” μ˜ν™”μž…λ‹ˆλ‹€!")
# 'Otherwise'
else:
    print("λ³Όλ§Œν•œ μ˜ν™”μž…λ‹ˆλ‹€.")
✨ κ°•λ ₯ μΆ”μ²œν•˜λŠ” μ˜ν™”μž…λ‹ˆλ‹€!
# Example: Decide whether to recommend a movie based on its rating
movie_rating = 8.5

# 'If' movie_rating is greater than or equal to 9.0
if movie_rating >= 9.0:
    print("✨ κ°•λ ₯ μΆ”μ²œν•˜λŠ” μ˜ν™”μž…λ‹ˆλ‹€!")
# 'Otherwise'
else:
    print("λ³Όλ§Œν•œ μ˜ν™”μž…λ‹ˆλ‹€.")

# Change movie_rating to 8.5 and run again!
λ³Όλ§Œν•œ μ˜ν™”μž…λ‹ˆλ‹€.
# A more detailed recommendation system
movie_rating = 8.8

if movie_rating >= 9.0:
    print("✨ κ°•λ ₯ μΆ”μ²œ! (9.0점 이상)")
# 'Else if' the rating is 8.5 or higher
elif movie_rating >= 8.5:
    print("πŸ‘ μΆ”μ²œ! (8.5점 이상)")
# 'In all other cases'
else:
    print("πŸ€” 보톡. (8.5점 미만)")
πŸ‘ μΆ”μ²œ! (8.5점 이상)

πŸ” Module 2: All the Data at Once! for Loops

# The movie dataset created in Week 3
my_movie_dataset = [
    { "title": "μΈμ…‰μ…˜",
     "director": "ν¬λ¦¬μŠ€ν† νΌ λ†€λž€",
      "rating": 9.29 },
    { "title": "기생좩",
     "director": "λ΄‰μ€€ν˜Έ",
      "rating": 9.07 },
    { "title": "어바웃 νƒ€μž„",
     "director": "λ¦¬μ°¨λ“œ μ»€ν‹°μŠ€",
      "rating": 9.27 }
]

# Iterate through the dataset (list), placing each movie's information (dictionary) in movie
print("--- λͺ¨λ“  μ˜ν™” 제λͺ© 좜λ ₯ ---")
for movie in my_movie_dataset:
    # Retrieve and print the value of the 'title' key from each movie (dictionary)
    print(movie['title'])
--- λͺ¨λ“  μ˜ν™” 제λͺ© 좜λ ₯ ---
μΈμ…‰μ…˜
기생좩
어바웃 νƒ€μž„
# Combine loops and conditionals to select only 'movies rated 9.2 or higher'

print("\n--- 평점 9.2 이상인 μΆ”μ²œ μ˜ν™” ---")
for movie in my_movie_dataset:
    # 'If' the current movie's rating is greater than or equal to 9.2
    if movie['rating'] >= 9.2:
        # Print that movie's title and rating
        print(f"제λͺ©: {movie['title']}, 평점: {movie['rating']}")
--- 평점 9.2 이상인 μΆ”μ²œ μ˜ν™” ---
제λͺ©: μΈμ…‰μ…˜, 평점: 9.29
제λͺ©: 어바웃 νƒ€μž„, 평점: 9.27

βš™οΈ Module 3: Reusable Code Components β€” Functions (def)

# A simple function that adds two numbers
def add_numbers(x, y):
    """
    이 ν•¨μˆ˜λŠ” 두 개의 숫자 x와 yλ₯Ό μž…λ ₯λ°›μ•„,
    κ·Έ 합을 κ³„μ‚°ν•˜μ—¬ κ²°κ³Όκ°’μœΌλ‘œ λŒλ €μ€λ‹ˆλ‹€.
    """
    result = x + y
    return result

# Call (use) the function
sum_result = add_numbers(5, 3)
print(f"5와 3의 합은? {sum_result}")

another_sum = add_numbers(100, 200)
print(f"100κ³Ό 200의 합은? {another_sum}")
5와 3의 합은? 8
100κ³Ό 200의 합은? 300
# Turn Module 2's 'rating-based movie recommendation' logic into a function.

def recommend_movies(dataset, min_rating):
    """
    μ˜ν™” 데이터셋과 μ΅œμ†Œ 평점을 μž…λ ₯λ°›μ•„,
    쑰건에 λ§žλŠ” μ˜ν™” 제λͺ© 리슀트λ₯Ό 결과둜 λŒλ €μ€λ‹ˆλ‹€.
    """
    recommended_list = [] # An empty list for recommended movies
    for movie in dataset:
        if movie['rating'] >= min_rating:
            recommended_list.append(movie['title']) # Append to the list
    return recommended_list


# Use the function to recommend movies rated 9.0 or higher
my_best_movies = recommend_movies(my_movie_dataset, 9.0)
print(f"평점 9.0 이상 μΆ”μ²œ μ˜ν™”: {my_best_movies}")

# Reuse the same function to recommend movies rated 9.1 or higher
my_good_movies = recommend_movies(my_movie_dataset, 9.1)
print(f"평점 9.1 이상 μΆ”μ²œ μ˜ν™”: {my_good_movies}")
평점 9.0 이상 μΆ”μ²œ μ˜ν™”: ['μΈμ…‰μ…˜', '기생좩', '어바웃 νƒ€μž„']
평점 9.1 이상 μΆ”μ²œ μ˜ν™”: ['μΈμ…‰μ…˜', '어바웃 νƒ€μž„']