נבנה על ידיBuilt by Ariel Yohimovich

PythonPython
9
חלקPart 2: פונקציות וקוד אמיתיFunctions & real code כ-3 שעות (כולל הפסקה)About 3 hours (with a break)

פונקציותFunctions

def, פרמטרים, ערכי ברירת מחדל ולמה מפרקים קוד.

def, parameters, default values and why we split code.

נסו לפתור לבד לפני שמציצים. בנגן: חיצים במקלדת או הכפתורים. כל צעד מראה איפה (קובץ ושורה), מה כותבים ולמה.Try on your own before peeking. In the player use the arrow keys or the buttons. Each step shows where (file and line), what to write, and why.
תרגילExercise 1

פונקציית ברכהA greeting function

תזכורת: מה ביקשוReminder: the task

כתבו פונקציה greet(name) שמחזירה (return) את המחרוזת Hello, <name>!. קראו לה עם "Noa" והדפיסו.

Write greet(name) that returns the string Hello, <name>!. Call it with "Noa" and print it.

סרטון מונפש: על מה לוחצים, מה כותבים ולמה, צעד אחר צעד.An animated movie: what to click, what to type and why, step by step.
איפהWhere
מהWhat
למהWhy
הפתרון המלא והפלט שלוFull solution and its output
main.py
def greet(name):
    return f"Hello, {name}!"

print(greet("Noa"))
פלטOutput
Hello, Noa!

יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.

תרגילExercise 2

שטח מלבןRectangle area

תזכורת: מה ביקשוReminder: the task

כתבו area(width, height=1) שמחזירה את השטח. הדפיסו את area(4, 5) ואת area(7).

Write area(width, height=1) returning the area. Print area(4, 5) and area(7).

סרטון מונפש: על מה לוחצים, מה כותבים ולמה, צעד אחר צעד.An animated movie: what to click, what to type and why, step by step.
איפהWhere
מהWhat
למהWhy
הפתרון המלא והפלט שלוFull solution and its output
main.py
def area(width, height=1):
    return width * height

print(area(4, 5))
print(area(7))
פלטOutput
20
7

יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.

תרגילExercise 3

המרת מטבעCurrency converter

תזכורת: מה ביקשוReminder: the task

כתבו convert(amount, rate=3.7) שמחזירה סכום בשקלים מעוגל ל-2 ספרות. קלטו סכום בדולרים והדפיסו את התוצאה, פעם בשער ברירת המחדל ופעם עם rate=3.5 (לפי שם).

Write convert(amount, rate=3.7) returning shekels rounded to 2 places. Read a dollar amount and print the result, once with the default rate and once with rate=3.5 (by name).

סרטון מונפש: על מה לוחצים, מה כותבים ולמה, צעד אחר צעד.An animated movie: what to click, what to type and why, step by step.
איפהWhere
מהWhat
למהWhy
הפתרון המלא והפלט שלוFull solution and its output
main.py
def convert(amount, rate=3.7):
    return round(amount * rate, 2)

usd = float(input())
print(convert(usd))
print(convert(usd, rate=3.5))
פלטOutput
100
370.0
350.0

יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.

תרגילExercise 4

בודק סיסמהPassword checker

תזכורת: מה ביקשוReminder: the task

כתבו is_strong(pw) שמחזירה True אם הסיסמה באורך 8 לפחות ויש בה גם ספרה (any(c.isdigit() for c in pw)). הדפיסו את התוצאה עבור "python" ועבור "python2026".

Write is_strong(pw) returning True if the password is at least 8 long and contains a digit (any(c.isdigit() for c in pw)). Print the result for "python" and "python2026".

סרטון מונפש: על מה לוחצים, מה כותבים ולמה, צעד אחר צעד.An animated movie: what to click, what to type and why, step by step.
איפהWhere
מהWhat
למהWhy
הפתרון המלא והפלט שלוFull solution and its output
main.py
def is_strong(pw):
    return len(pw) >= 8 and any(c.isdigit() for c in pw)

print(is_strong("python"))
print(is_strong("python2026"))
פלטOutput
False
True

יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.

תרגילExercise 5

מדף, צעד 9: פונקציותShelf, step 9: functions

תזכורת: מה ביקשוReminder: the task

ארגנו את המדף בפונקציות: add_book(shelf, title, author) מוסיפה מילון, list_books(shelf) מדפיסה 1. Dune (Frank Herbert), ו-count_books(shelf) מחזירה את המספר. הוסיפו שני ספרים, הדפיסו, והדפיסו את הספירה.

Organise the shelf into functions: add_book(shelf, title, author) appends a dict, list_books(shelf) prints 1. Dune (Frank Herbert), and count_books(shelf) returns the count. Add two books, list them, and print the count.

סרטון מונפש: על מה לוחצים, מה כותבים ולמה, צעד אחר צעד.An animated movie: what to click, what to type and why, step by step.
איפהWhere
מהWhat
למהWhy
הפתרון המלא והפלט שלוFull solution and its output
shelf.py
def add_book(shelf, title, author):
    shelf.append({"title": title, "author": author})

def list_books(shelf):
    for i, b in enumerate(shelf, start=1):
        print(f"{i}. {b['title']} ({b['author']})")

def count_books(shelf):
    return len(shelf)

shelf = []
add_book(shelf, "Dune", "Frank Herbert")
add_book(shelf, "Matilda", "Roald Dahl")
list_books(shelf)
print(count_books(shelf))
פלטOutput
1. Dune (Frank Herbert)
2. Matilda (Roald Dahl)
2

יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.

אתגרChallenge 1

מחשבון BMIBMI calculator

תזכורת: מה ביקשוReminder: the task

כתבו bmi(weight, height_cm) שמחזירה BMI מעוגל לספרה אחת, ו-category(b) שמחזירה under (<18.5), normal (<25), over (<30) או obese. הדפיסו את שתיהן עבור 70 ק"ג ו-175 ס"מ.

Write bmi(weight, height_cm) returning BMI rounded to 1 decimal, and category(b) returning under (<18.5), normal (<25), over (<30) or obese. Print both for 70 kg and 175 cm.

סרטון מונפש: על מה לוחצים, מה כותבים ולמה, צעד אחר צעד.An animated movie: what to click, what to type and why, step by step.
איפהWhere
מהWhat
למהWhy
הפתרון המלא והפלט שלוFull solution and its output
main.py
def bmi(weight, height_cm):
    m = height_cm / 100
    return round(weight / m ** 2, 1)

def category(b):
    if b < 18.5:
        return "under"
    if b < 25:
        return "normal"
    if b < 30:
        return "over"
    return "obese"

b = bmi(70, 175)
print(b, category(b))
פלטOutput
22.9 normal

יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.

אתגרChallenge 2

ספירת תנועות כלליתGeneral letter counter

תזכורת: מה ביקשוReminder: the task

כתבו count_letters(text, letters="aeiou") שמחזירה כמה תווים ב-text שייכים ל-letters (בלי רגישות לאותיות גדולות). הדפיסו את התוצאה עבור "Programming in Python" עם ברירת המחדל, ועם letters="py".

Write count_letters(text, letters="aeiou") returning how many characters in text belong to letters (case-insensitive). Print it for "Programming in Python" with the default and with letters="py".

סרטון מונפש: על מה לוחצים, מה כותבים ולמה, צעד אחר צעד.An animated movie: what to click, what to type and why, step by step.
איפהWhere
מהWhat
למהWhy
הפתרון המלא והפלט שלוFull solution and its output
main.py
def count_letters(text, letters="aeiou"):
    return sum(1 for c in text.lower() if c in letters)

print(count_letters("Programming in Python"))
print(count_letters("Programming in Python", letters="py"))
פלטOutput
5
3

יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.

נבנה על ידיBuilt by Ariel Yohimovich