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