פונקציותFunctions
def, פרמטרים, ערכי ברירת מחדל ולמה מפרקים קוד.
def, parameters, default values and why we split code.
פונקציית ברכה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.
הפתרון המלא והפלט שלוFull solution and its output
def greet(name):
return f"Hello, {name}!"
print(greet("Noa"))Hello, Noa!
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
שטח מלבן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).
הפתרון המלא והפלט שלוFull solution and its output
def area(width, height=1):
return width * height
print(area(4, 5))
print(area(7))20 7
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
המרת מטבע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).
הפתרון המלא והפלט שלוFull solution and its output
def convert(amount, rate=3.7):
return round(amount * rate, 2)
usd = float(input())
print(convert(usd))
print(convert(usd, rate=3.5))100 370.0 350.0
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
בודק סיסמה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".
הפתרון המלא והפלט שלוFull solution and its output
def is_strong(pw):
return len(pw) >= 8 and any(c.isdigit() for c in pw)
print(is_strong("python"))
print(is_strong("python2026"))False True
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
מדף, צעד 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.
הפתרון המלא והפלט שלוFull solution and its output
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))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.
מחשבון 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.
הפתרון המלא והפלט שלוFull solution and its output
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))22.9 normal
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
ספירת תנועות כללית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".
הפתרון המלא והפלט שלוFull solution and its output
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"))5 3
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.