נבנה על ידי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.

כותבים בעורך שבכל תרגיל, לוחצים הרצה כדי לראות פלט, ובדיקה כדי שהבודק האוטומטי יבדוק. אפשר גם לעבוד ב-VS Code. ההתקדמות נשמרת.Write in each exercise's editor, press Run to see output and Check for the automatic checker. You can also work in VS Code. Progress is saved.

חימום: 3 שאלותWarm-up: 3 questions

1. מה ידפיס הקוד?What does this print?
python
def hi():
    print("hi")

2. מה תחזיר פונקציה בלי return?What does a function without return return?

3. מה הקריאה f(2, b=5) מעבירה ל-a בפונקציה def f(a, b=1)?In def f(a, b=1), what does f(2, b=5) pass to a?

תרגיליםExercises

תרגילExercise 1

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

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

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

איך הפלט אמור להיראות?What should the output look like?
Hello, Noa!
main.py
רמזHint 1: def greet(name):def greet(name):
רמזHint 2: return f"Hello, {name}!"return f"Hello, {name}!"
לפתרוןSolution
תרגילExercise 2

שטח מלבןRectangle area

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

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

איך הפלט אמור להיראות?What should the output look like?
20
7
main.py
רמזHint 1: ברירת מחדל: height=1.Default: height=1.
לפתרוןSolution
תרגילExercise 3

המרת מטבעCurrency converter

כתבו 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).

איך הפלט אמור להיראות?What should the output look like?
100
370.0
350.0
main.py
רמזHint 1: return round(amount * rate, 2)return round(amount * rate, 2)
רמזHint 2: convert(x, rate=3.5)convert(x, rate=3.5)
לפתרוןSolution
תרגילExercise 4

בודק סיסמהPassword checker

כתבו 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".

איך הפלט אמור להיראות?What should the output look like?
False
True
main.py
רמזHint 1: len(pw) >= 8 and ...len(pw) >= 8 and ...
רמזHint 2: מחזירים את הביטוי עצמו.Return the expression itself.
לפתרוןSolution
תרגילExercise 5

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

פרויקט מלווהCompanion project

ארגנו את המדף בפונקציות: 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.

איך הפלט אמור להיראות?What should the output look like?
1. Dune (Frank Herbert)
2. Matilda (Roald Dahl)
2
shelf.py
רמזHint 1: הרשימה עוברת כפרמטר.The list is passed as a parameter.
רמזHint 2: append משנה את הרשימה המקורית (mutable!).append changes the original list (mutable!).
לפתרוןSolution

אתגרים לחזקיםChallenges for the brave

אתגרChallenge 1

מחשבון BMIBMI calculator

כתבו 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.

איך הפלט אמור להיראות?What should the output look like?
22.9 normal
main.py
רמזHint 1: BMI = משקל / (גובה במטרים)².BMI = weight / (height in metres)².
רמזHint 2: פונקציה אחת קוראת לשנייה בקוד הראשי.One function feeds the other in the main code.
לפתרוןSolution
אתגרChallenge 2

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

כתבו 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".

איך הפלט אמור להיראות?What should the output look like?
5
3
main.py
רמזHint 1: sum(1 for c in text.lower() if c in letters)sum(1 for c in text.lower() if c in letters)
לפתרוןSolution

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